📄 initdialog.java
字号:
package my_cpt;
import java.awt.*;
import java.awt.image.*;
import super_cpt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import data2.Global;
import data2.Square;
public class InitDialog extends SuperDialog implements ActionListener,ItemListener{
/**
* @param args
*/
TextField gridtxt=new TextField("30");
List penlst=new List();
SuperBtn crbtn=new SuperBtn("创建");
SuperBtn upbtn=new SuperBtn("刷新");
SuperBtn okbtn=new SuperBtn("确定");
SuperBtn nobtn=new SuperBtn("取消");
MyPane pane=new MyPane();
FileDialog chooser;
Timer timer;
public int lenX=0;
public boolean isReady=false;
public int style[]={1,1,1,1};
private Square mySquare=new Square(0);
public InitDialog(Frame f)
{
super("新建文件",f,true);
setBounds(387,200,250,240);
chooser=new FileDialog(f);
add(pane);
add(gridtxt);
add(penlst);
add(crbtn);
add(upbtn);
add(okbtn);
add(nobtn);
timer=new Timer(10,this);
pane.setLocation(0,-pane.getHeight());
gridtxt.setBounds(95,36,60,16);
penlst.setBounds(158,55,86,128);
crbtn.setBounds(penlst.getX()+penlst.getWidth()/2-30,penlst.getY()+penlst.getHeight()+2,30,20);
upbtn.setBounds(penlst.getX()+penlst.getWidth()/2,penlst.getY()+penlst.getHeight()+2,30,20);
okbtn.setBounds(95,upbtn.getY()+upbtn.getHeight()+5,30,16);
nobtn.setBounds(125,okbtn.getY(),30,16);
upbtn.addActionListener(this);
okbtn.addActionListener(this);
nobtn.addActionListener(this);
crbtn.addActionListener(this);
penlst.addItemListener(this);
}
public BufferedImage image=null;
public void myPaint(Graphics2D g2)
{
g2.setColor(Color.GRAY);
if(image!=null){
int width=0,height=0,x=0,y=0;
if(image.getWidth()>image.getHeight())
{
width=150;
height=image.getHeight()*width/image.getWidth();
x=5;y=55+75-height/2;
}else
{
height=150;
width=image.getWidth()*height/image.getHeight();
x=5+75-width/2;
y=55;
}
g2.drawImage(image,x,y,width,height,this);
}
else
g2.drawString("目前没有有效的图片",22,118);
g2.drawRect(5,55,150,150);
g2.setColor(Color.BLACK);
g2.drawString("图像长度:",30,47);
g2.drawString("格子30-100",168,47);
}
public void showPane(boolean bool)
{//显示面板
isOpen=bool;
timer.start();
if(!bool)
{
updatelst();
}
}
public boolean isOpen=false;
public void Animation(boolean isopen)
{
int dy=(isopen)?8:-8,y=pane.getY();
boolean stop=false;
y+=dy;
if(y>36){
y=36;
stop=true;
}
if(y<-pane.getHeight()){
y=-pane.getHeight();
stop=true;
}
pane.setLocation(pane.getX(),y);
if(stop)timer.stop();
}
public void updatelst()
{//刷新
File file=new File(Global.PEN_PATH+"/.");
String str[]=file.list();
penlst.removeAll();
for(int i=0;i<str.length;i++)
{
if(str[i].substring(str[i].length()-4).equals(".pen"))
penlst.add(str[i]);
}
if(penlst.getItems().length!=0){
mySquare=getSquare(0);
image=mySquare.getImage();
repaint();
}
}
public void setVisible(boolean bool)
{
if(bool){
isReady=false;
updatelst();
}
super.setVisible(bool);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==timer)
{
Animation(isOpen);
}
if(e.getSource()==upbtn)
{//刷新
updatelst();
}
if(e.getSource()==okbtn)
{//确认
int i=0;
try{
i=Integer.parseInt(gridtxt.getText().trim());
}catch(Exception ex)
{
i=30;
gridtxt.setText("30");
}
if((i-30)*(100-i)>=0)
lenX=i;
if(i<30)
{
lenX=30;
gridtxt.setText("30");
}
if(i>100)
{
lenX=100;
gridtxt.setText("100");
}
if(mySquare!=null)
style=mySquare.getGrid();
isReady=true;
setVisible(false);
}
if(e.getSource()==nobtn)
{//取消
isReady=false;
setVisible(false);
}
if(e.getSource()==crbtn)
{//创建
showPane(true);
}
}
public void itemStateChanged(ItemEvent e)
{
mySquare=getSquare(penlst.getSelectedIndex());
image=mySquare.getImage();
repaint();
}
public Square getSquare(int index)
{
String path=Global.PEN_PATH+"\\"+penlst.getItem(index);
Square mtmp=null;
try
{
RandomAccessFile reader=new RandomAccessFile(path,"rw");
byte[] buf=new byte[1024];
StringBuffer strbuf=new StringBuffer();
while((reader.read(buf))>0)
{
strbuf.append(new String(buf));
}
reader.close();
mtmp=new Square(strbuf.toString());
}catch(Exception e){};
return mtmp;
}
public class MyPane extends Panel implements ActionListener
{
public BufferedImage myimage;
public Graphics2D myG;
public SuperBtn mybtn0=new SuperBtn("预览");
public SuperBtn mybtn1=new SuperBtn("保存");
public SuperBtn mybtn2=new SuperBtn("取消");
public TextField txt[]=new TextField[4];
private Square stmp;
public MyPane()
{
setSize(248,160);
setLayout(null);
add(mybtn0);
add(mybtn1);
add(mybtn2);
mybtn0.setBounds(65,140,40,16);
mybtn1.setBounds(105,140,40,16);
mybtn2.setBounds(145,140,40,16);
mybtn0.addActionListener(this);
mybtn1.addActionListener(this);
mybtn2.addActionListener(this);
for(int i=0;i<txt.length;i++)
{
txt[i]=new TextField("1");
add(txt[i]);
txt[i].setBounds(180,110-i*30,50,20);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mybtn0)
{
int s[]=new int[4];
for(int i=0;i<4;i++)
{
try
{
s[i]=Integer.parseInt(txt[i].getText().trim());
if(s[i]>6)
{
s[i]=6;
txt[i].setText("6");
}
if(s[i]<1)
{
s[i]=1;
txt[i].setText("1");
}
}catch(Exception ex)
{
s[i]=1;
txt[i].setText("1");
}
}
stmp=new Square(s);
myimg=stmp.getImage();
repaint();
}
if(e.getSource()==mybtn1)
{
int s[]=new int[4];
for(int i=0;i<4;i++)
{
try
{
s[i]=Integer.parseInt(txt[i].getText().trim());
if(s[i]>6)
{
s[i]=6;
txt[i].setText("6");
}
if(s[i]<1)
{
s[i]=1;
txt[i].setText("1");
}
}catch(Exception ex)
{
s[i]=1;
txt[i].setText("1");
}
}
stmp=new Square(s);
myimg=stmp.getImage();
repaint();
chooser.setMode(FileDialog.SAVE);
chooser.setTitle("请选择保存位置");
chooser.setDirectory(Global.PEN_PATH);
chooser.setVisible(true);
String path=chooser.getDirectory()+chooser.getFile();
if(path.substring(path.length()-4).equals(".pen"))
{
try
{
RandomAccessFile writer=new RandomAccessFile(path,"rw");
writer.write(stmp.output().getBytes());
writer.close();
}catch(Exception ex){}
}
showPane(false);
}
if(e.getSource()==mybtn2)
{
showPane(false);
}
}
public BufferedImage myimg=null;
public void update(Graphics g)
{
int w=getWidth(),h=getHeight();
if(myimage==null || myG==null)
{
myimage=this.getGraphicsConfiguration().createCompatibleImage(w,h);
myG=myimage.createGraphics();
myG.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
}
myG.setColor(Color.WHITE);
myG.fillRect(0,0,w,h);
myG.setColor(Color.BLACK);
myG.drawString("格数:",145,34);
myG.drawString("格数:",145,64);
myG.drawString("格数:",145,94);
myG.drawString("格数:",145,124);
if(myimg!=null){
int width=0,height=0,x=0,y=0;
if(myimg.getWidth()>myimg.getHeight())
{
width=130;
height=myimg.getHeight()*width/myimg.getWidth();
x=7;y=75-height/2;
}else
{
height=110;
width=myimg.getWidth()*height/myimg.getHeight();
x=65-width/2;
y=20;
}
myG.drawImage(myimg,x,y,width,height,this);
}
else
myG.drawString("目前没有有效的图片",18,getHeight()/2-6);
myG.setColor(Color.GRAY);
myG.drawRect(7,20,130,110);
myG.drawRect(0,0,w-1,h-1);
paint(g);
}
public void paint(Graphics g)
{
if(myimage==null)repaint();
g.drawImage(myimage,0,0,this);
super.paint(g);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -