首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

画图板资料保存到硬盘即读取

2012-11-05 
画图板文件保存到硬盘即读取画图板文件保存到硬盘即读取一共包含2种方式,一种是队列方式的保存,另一种是所

画图板文件保存到硬盘即读取
画图板文件保存到硬盘即读取
一共包含2种方式,一种是队列方式的保存,另一种是所谓的山寨BMP保存。
队列保存:
public void saveShape(String path,Queueimp<Shape> queue) {
    //创建文件输出流
    try {
    java.io.FileOutputStream fileout = new java.io.FileOutputStream(path);
    //将文件流包装成数据输出流
    java.io.DataOutputStream dataout = new java.io.DataOutputStream(fileout);
    //保存图形队列的长度
    dataout.writeInt(queue.size());
    //遍历队列
    for (int i = 0; i < queue.size(); i++) {
    Shape sh = queue.get(i);
    // 使用基本数据流将形状对象的属性值写入文件
    Color color  = sh.getColor();
    int c = color.getRGB();
    int type = sh.type;
    //写入形状的颜色和类型
    dataout.writeInt(c);
    dataout.writeInt(type);
//判断type类型
{
.......;
}
  } catch (Exception e) {
    e.printStackTrace();
    }
    }
/**
     * 读取指定路径的文件
     * @param path : 文件的路径
     * @return 返回形状队列
     */
    public Queueimp<Shape> readFile (String path) {
    Queueimp<Shape> shape = new Queueimp<Shape>();
    try{
    //创建文件输入流
    java.io.FileInputStream filein = new java.io.FileInputStream(path);
    //创建数据输入流
    java.io.DataInputStream datain = new java.io.DataInputStream(filein);
    //读取长度
    int len = datain.readInt();
    for (int i = 0; i < len; i++) {
    //读取颜色和类型
    int cor = datain.readInt();
    Color color = new Color(cor);
    int type = datain.readInt();
//判断type类型
{
.......;
}
  datain.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
      return shape;
    }
}

在动作监听器里添加 保存和读取
/********************保存和读取**********************/ 
    if(type.equals("保存")) {//当事件是“保存”按钮时
    file.saveShape(path, queue);   //调用将已经画好的图形的所有数据存入指定的文件中
    } 
    if(type.equals("读取")) {//当事件源是“打开”在按钮时  
    Queueimp<Shape> shapes = file.readFile(path);          //调用读取文件的方法
   for (int i = 0; i < shapes.size(); i++){
   Shape  sha = shapes.get(i);
   sha.draw(g);
   queue.add(sha);
   }  
    }


山寨BMP:

import javax.swing.JPanel;
import java.awt.Point;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* 图片的保存
* @author jsyczynba
*
*/
public class Fileoperate {
static int width;
static int height;
static int[][] colors;
static java.awt.Robot robot;
public static void save (JPanel panel,String path) {
//截图
colors = getImage(panel);
//保存颜色数组
saveFile(colors,path);

//清空颜色数组
//DrawUI.colors = null;
}

/**
* 读取颜色数组
* @return
*/
public static int[][] readFile( String path) {


int[][] image = null;
try {
//创建文件输入流
java.io.FileInputStream filein = new java.io.FileInputStream(path);
//创建数据输入流
java.io.DataInputStream datain = new java.io.DataInputStream(filein);
//读取颜色数组的长和宽
int t = datain.readInt();
int t2 =datain.readInt();
//将二维数组中的颜色值读取
image = new int[t][t2];
for (int i = 0; i < t; i++) {
for (int j = 0; j < t2; j++) {

image[i][j] = datain.readInt();

}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}


/**
*
* @param panel
* @return
*/
public static int[][] getImage(JPanel panel) {
int[][] tempColors;
//创建机器人对象
try {
robot = new java.awt.Robot();
} catch (Exception e) {
e.printStackTrace();
}
width = panel.getWidth();
height = panel.getHeight();
tempColors = new int[height][width];
// 得到panel左上角的点相对于屏幕的坐标
Point p = panel.getLocationOnScreen();
//定义panel区域
java.awt.Rectangle rect = new java.awt.Rectangle(p.x, p.y, width, height);
//从屏幕上抓取一张图片
java.awt.image.BufferedImage img = robot.createScreenCapture(rect);
//得到图片各像素点的颜色值
for (int i = 0; i < tempColors.length; i++) {
for (int j = 0; j < tempColors[i].length; j++) {
tempColors[i][j] = img.getRGB(j, i);
}
}
return tempColors;
}

/**
*
* @param colors
* @param path
*/
private static void saveFile(int[][] colors, String path) {
try{
//创建文件输出流
java.io.FileOutputStream fileout = new java.io.FileOutputStream(path);
java.io.DataOutputStream dataout = new java.io.DataOutputStream(fileout);
//将数组长宽写入文件
dataout.writeInt(colors.length);
dataout.writeInt(colors[1].length);
//将二维数组中的颜色值写入文件
for (int i = 0; i < colors.length; i++) {
for (int j = 0; j < colors[i].length; j++) {
dataout.writeInt(colors[i][j]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
BMP的paint重绘
/**
*重写主界面重绘的方法
* @author jsyczynba
*
*/
class MyPanel extends JPanel {
// 重写父类绘制窗体的方法
public void paint(Graphics g) {
super.paint(g);
if(colors!=null)
for(int i=0;i<colors.length;i++) {
for(int j=0;j<colors[i].length;j++) {
Color c = new Color(colors[i][j]);
g.setColor(c);
g.drawLine(j, i, j, i);
}
}
}
}

热点排行