`
ttsecret
  • 浏览: 3912 次
  • 性别: Icon_minigender_2
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

画板和五子棋的保存

J# 
阅读更多
画板重绘、保存的总结
重绘:
最初制作的画板在窗体缩小后再打开便没有了所画的图形,要想使改变窗体后所画的东西仍然存在,就需要对画板进行重绘。

采用自定义队列对画板窗体上的画布进行保存:形状item、颜色color,即每画一个图形,就将该图形加入到队列GraphNList中。

在给形状按钮添加动作监听器时,需要使用  按钮名.setActioncommand("sth");  命令,相当于给按钮添加一个使监听器识别的名字sth,然后在 e.getActionCommand();  得到名字与已有的名字对比,若匹配则执行相应的命令。

保存:
画板的保存采用的是DataStream流,从GraphNList中取得图形信息,通过画布对象graph得到图形的形状Graph g = nj.getindex(i);,通过读取与写入int来实现。
队列的大小
图形的坐标
图形的形状
颜色:分RGB三个颜色值读取与写入,将color类型转换为int

package cn.javanet.ttstudy2;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;


public class SaveDraw {
	/**
	 * 将图形信息写入文件
	 * 
	 * @param nj
	 *            :要写入的图形
	 * @throws Exception
	 */
	public void SaveFile(NList nj) {
		// 创建写入文件的Data对象
		String fileName1 = "src\\cn\\javanet\\ttstudy2\\Graph";
		try {
			FileOutputStream fout = new FileOutputStream(fileName1);
			DataOutputStream dout = new DataOutputStream(fout);
			// 写入文件的大小
			dout.writeInt(nj.size());
			// 写入文件
			for (int i = 0; i < nj.size(); i++) {
				// 创建Graph对象
				Graph g = nj.getindex(i);
				 //得到形状
				int item = 1;
				if (g.getItem().equals("curve")) {
					item = 1;
				}else if (g.getItem().equals("line")) {
					item = 2;
				}else if (g.getItem().equals("oval")) {
					item = 3;
				}else if (g.getItem().equals("rect")) {
					item = 4;
				}
				dout.writeInt(item);
				//得到颜色
				java.awt.Color color = g.getColor();
				int rgb = color.getRGB();
				dout.writeInt(rgb);
				
				int x1 = g.getX1();
				int y1 = g.getY1();
				int x2 = g.getX2();
				int y2 = g.getY2();
				// 写入文件
				// 写入坐标
				dout.writeInt(x1);
				dout.writeInt(y1);
				dout.writeInt(x2);
				dout.writeInt(y2);
				
			}
			// 强制写入
			dout.flush();
			// 关闭流
			dout.close();
			fout.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 打开文件
	 * 
	 * @return 文件中图形的存储信息
	 */
	public NList OpenDraw() {
		// 初始化队列对象
		NList nj = new GraphNList();
		// 创建写入文件的Data流对象
		String fileName1 = "src\\cn\\javanet\\ttstudy2\\Graph";
		try {
			FileInputStream fin = new FileInputStream(fileName1);
			DataInputStream din = new DataInputStream(fin);
			// 读取的文件大小
			int size = din.readInt();
			// 循环读取文件
			for (int i = 0; i < size; i++) {
				int item = din.readInt();
				// 读取颜色
				int rgb = din.readInt();
				// 坐标
				int x1 = din.readInt();
				int y1 = din.readInt();
				int x2 = din.readInt();
				int y2 = din.readInt();
				
				String str = "";
				if (item == 1) {
					str = "curve";
				}else if (item == 2) {
					str = "line";
				}else if (item == 3) {
					str = "oval";
				}else if (item == 4) {
					str = "rect";
				}
				
				// 实例化graph对象
				Graph g = new Graph(new java.awt.Color(rgb), str, x1, y1, x2,
						y2);
				nj.add(g);
			}
			// 关闭
			din.close();
			fin.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nj;
	}
}



五子棋的重绘、保存
重绘:
设定判断条件,让棋子只能在棋盘的相交处下
定义一个数组,默认为0,在对应的位置存入1或2,分别对应黑子和白子

保存:
仍然采用DataStream流读写文件
由于棋盘始终没有变化,每次重绘方法调用时都会对棋盘进行重绘,只需将所下棋子的信息写入文件,打开时通过重绘方法将棋子重绘即可。
将数组中的信息写入文件(没有棋子的位子要用0占位!)
//在对应的位子写入棋子
for(int i = 0;i<cp.Row;i++){
	for(int j = 0;j<cp.Clo;j++){
							dout.writeInt(array[i][j]);
	}
}


//按顺序读取对应位子的棋子
for(int i = 0;i<cp.Row;i++){
    for(int j=0;j<cp.Clo;j++){
       array[i][j] = din.readInt();
						 
	 }
}
				  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics