张小芳 发表于 2013-2-1 12:03:30

画板保存

此文章是对上次画板总结的完善。将画板总结与这篇画板保存结合起来就是一个可以保存文件的画板

1 在Hu.java中创建对象io 和 保存文件的路径path

final IOTes io=new IOTes();
final String path="E:\\小芳\\QQ\\aa.as";

2创建2个按钮,open 和save

   final JButton open = new JButton("打开");
   final JButton save = new JButton("保存");

3 添加到界面上

this.add(open);
this.add(save);

4 给open和save按钮添加监视器

   ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==open){sh=io.readFile(path);repaint();}
if(e.getSource()==save) {io.saveFile(path,sh);

}
}
};
open.addActionListener(al);
save.addActionListener(al);

5 实现文件保存和打开的类


public class IOTes {
/**
* 读取文件的方法
* 将文件中的内容存入到队列sh中
* @param path
*/
public NJListImpl<Shap> readFile(String path) {
//创建队列保存从文件读出的数据
NJListImpl<Shap> sh=new NJListImpl<Shap>();
try{
//创建文件对象的输入流
java.io.FileInputStream fi=new java.io.FileInputStream(path);
//将文件输入流包装成可读基本类型的流
java.io.DataInputStream da=new java.io.DataInputStream(fi);
//读取长度,即总共的形状个数
int len=da.readInt();
//循环读每一个形状
for(int i=0;i<len;i++){
byte type=da.readByte();//读图形类型
int rgb=da.readInt();
Color c=new Color(rgb);//读取图形颜色
if(type==0){
//如果读到的是0,表示读到的是直线,接下来创建的是直线对象   
Line li=new Line();
//将读取
li.type=type;
li.c=c;
li.x1=da.readInt();
li.y1=da.readInt();
li.x2=da.readInt();
li.y2=da.readInt();
//将形状加入队列
sh.add(li);
}
else if(type==1){
//如果读到的是1,表示读到的是圆,接下来创建的是圆对象
   
    Oval o=new Oval();
//将读取
o.type=type;
o.x1=da.readInt();
o.y1=da.readInt();
o.x2=da.readInt();
o.y2=da.readInt();
//将形状加入队列
sh.add(o);
}
else if(type==2){
//如果读到的是2,表示读到的是矩形,接下来创建的是矩形对象
    Rect r=new Rect();
//将读取
r.type=type;
r.x1=da.readInt();
r.y1=da.readInt();
r.x2=da.readInt();
r.y2=da.readInt();
//将形状加入队列
sh.add(r);
}
else if(type==3){
//如果读到的是3,表示读到的是填充的矩形,接下来创建的是填充的矩形对象
   
    FillRect fr=new FillRect();
//将读取
fr.type=type;
fr.x1=da.readInt();
fr.y1=da.readInt();
fr.x2=da.readInt();
fr.y2=da.readInt();
//将形状加入队列
sh.add(fr);
}
else if(type==4){
//如果读到的是4,表示读到的是实心圆,接下来创建的是实心圆对象
   
    FillOval fo=new FillOval();
//将读取
fo.type=type;
fo.x1=da.readInt();
fo.y1=da.readInt();
fo.x2=da.readInt();
fo.y2=da.readInt();
//将形状加入队列
sh.add(fo);
}
}
return sh;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
/**
* 将数据写到文件的方法
*
* @param path
*            :要写的文件地址
* @param content
*            :要写的数据
*/
public void saveFile(String path1,NJListImpl<Shap> sh) {
try {
// 创建一个文件输出流
java.io.FileOutputStream fos = new java.io.FileOutputStream(path1);
//将文件输出流包装成可写基本类型的流
java.io.DataOutputStream dos=new java.io.DataOutputStream(fos);
dos.writeInt(sh.size());//先写入队列中文件的个数
//用循环读取队列

for(int i=0;i<sh.size();i++){
//取出一种形状
Shap s=sh.get(i);
byte type=s.type;
dos.writeByte(type);//写形状的类型
int x1,y1,x2,y2;
if(type==0){
//判断类型如果是直线
Line li=(Line)s;
Color c=li.c;
int rgb=c.getRGB();
int x11=li.x1;
int y11=li.y1;
int x21=li.x2;
int y21=li.y2;
dos.writeInt(rgb);
dos.writeInt(x11);
dos.writeInt(y11);
dos.writeInt(x21);
dos.writeInt(y21);
}
else if(type==1){
//判断类型如果是圆
Oval o=(Oval)s;
Color c=o.c;
int rgb=c.getRGB();
int x11=o.x1;
int y11=o.y1;
int x21=o.x2;
int y21=o.y2;
dos.writeInt(rgb);
dos.writeInt(x11);
dos.writeInt(y11);
dos.writeInt(x21);
dos.writeInt(y21);
}
else if(type==2){
//判断类型如果是矩形
Rect r=(Rect)s;
Color c=r.c;
int rgb=c.getRGB();
int x11=r.x1;
int y11=r.y1;
int x21=r.x2;
int y21=r.y2;
dos.writeInt(rgb);
dos.writeInt(x11);
dos.writeInt(y11);
dos.writeInt(x21);
dos.writeInt(y21);
}
else if(type==3){
//判断类型如果是填充的矩形
FillRect fr=( FillRect)s;
Color c=fr.c;
int rgb=c.getRGB();
int x11=fr.x1;
int y11=fr.y1;
int x21=fr.x2;
int y21=fr.y2;
dos.writeInt(rgb);
dos.writeInt(x11);
dos.writeInt(y11);
dos.writeInt(x21);
dos.writeInt(y21);
}
else if(type==4){
//判断类型如果是实心圆
FillOval fo=( FillOval)s;
Color c=fo.c;
int rgb=c.getRGB();
int x11=fo.x1;
int y11=fo.y1;
int x21=fo.x2;
int y21=fo.y2;
dos.writeInt(rgb);
dos.writeInt(x11);
dos.writeInt(y11);
dos.writeInt(x21);
dos.writeInt(y21);
}
}
dos.flush();
fos.close();
} catch (Exception ef) {
ef.printStackTrace();
}
}
}
页: [1]
查看完整版本: 画板保存