自定义文件的格式,可以进行保存与打开,正在学习,下面的代码不是很难,主要是要想清楚:
保存;
package 画图板升级0516; /** *画图板的升级 */ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.swing.*; import javax.swing.GroupLayout.Group; public class DrawUI extends JFrame{ // 单选按钮组 ButtonGroup group = new ButtonGroup(); String path = "F:\\file1\\a.image";// 保存文件的路径 JPanel center; public static void main(String[] args) { DrawUI draw = new DrawUI(); draw.creatDraw(); } /** * 初始化窗体 */ public void creatDraw(){ this.setTitle("画图板"); this.setSize(500,300); //退出JFrama this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体的布局为Border BorderLayout layout = new BorderLayout(); this.setLayout(layout); //设置菜单按钮 JMenuBar bar = creatJMenuBar(); this.setJMenuBar(bar); JPanel left =this.creatleft() ; JPanel center = this.creatCenter(); JPanel bottom = this.creatBottom(); //设置边框布局 this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); this.setVisible(true); DrawListener lis = new DrawListener(group); center.addMouseListener(lis); } /** * 创建菜单条的方法 * @return */ public JMenuBar creatJMenuBar(){ JMenuBar bar = new JMenuBar(); JMenu menu = new JMenu("文件"); JMenuItem saveitem = new JMenuItem("保存"); saveitem.setActionCommand("save"); JMenuItem openitem = new JMenuItem("打开"); openitem.setActionCommand("open"); menu.add(saveitem); menu.add(openitem); bar.add(menu); //创建监听器的的对象 ActionListener alis = new MyListener(); saveitem.addActionListener(alis); openitem.addActionListener(alis); return bar; } /** * 设置左边 * @return */ public JPanel creatleft(){ //左边 JPanel left = new JPanel(); //设置背景颜色 left.setBackground(new Color(200,200,200,200)); //设置最佳大小 left.setPreferredSize(new Dimension(50,100)); //在左边添加按钮 String[] items = { "直线", "矩形", "椭圆"}; String[] commands = { "line", "rect", "oval" }; for(int i = 0 ;i<items.length;i++){ //设置单选按钮 JRadioButton btn = new JRadioButton(items[i]); //设置按钮的动作命令 btn.setActionCommand(commands[i]); // 分为同一个组 group.add(btn); if (i == 0) {//设置第一个被选中 btn.setSelected(true); } left.add(btn); } return left; } /** * 设置中间 * @return */ public JPanel creatCenter(){ //中间 center = new myJPanel(); center.setBackground(Color.WHITE); return center; } /** * 设置右边 * @return */ public JPanel creatBottom(){ //下边 JPanel bottom = new JPanel(); bottom.setBackground(new Color(200,200,200,200)); //设置大小setPreferredSize指设置最佳大小 bottom.setPreferredSize(new Dimension(50,50)); return bottom; } /** * * @author *重写paint()的方法 */ class myJPanel extends JPanel{ public void paint(Graphics g) { super.paint(g); // 遍历保存形状的队列 for (int i = 0; i < DrawListener.list.size(); i++) { // 取得一个形状对象 Shape sh = DrawListener.list.get(i); // 绘制形状 sh.draw(g); } } } //添加监听器 class MyListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //得到动作命令 String command = e.getActionCommand(); System.out.println(command); if(command.equals("save")){ try{ // 将内存中的形状队列保存到文件中 File file = new File(path); FileOutputStream fos = new FileOutputStream(file); // 包装成对象流 ObjectOutputStream oos = new ObjectOutputStream(fos); // 写对象之前先要保存形状的个数 oos.writeInt(DrawListener.list.size()); for (int i = 0; i < DrawListener.list.size(); i++) { Shape sh = DrawListener.list.get(i); // 将对象写到文件中 oos.writeObject(sh); } // 强制输出 oos.flush(); fos.close(); }catch(Exception ef){ ef.printStackTrace(); } }if(command.equals("open")){ try{ // 建立文件输入流 File file = new File(path); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); // 先读取形状的个数 int len = ois.readInt(); for (int i = 0; i < len; i++) { // 读取一个形状对象 Shape sh = (Shape) ois.readObject(); //将读取到的形状对象保存到队列中 DrawListener.list.add(sh); } fis.close(); //刷新绘制面板 center.repaint(); }catch(Exception ef){ ef.printStackTrace(); } } } } }
package 画图板升级0516; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import javax.swing.ButtonGroup; import javax.swing.JPanel; public class DrawListener extends MouseAdapter{ static ArrayList<Shape> list = new ArrayList<Shape>(); int x1,x2,y1,y2; private Graphics g; private ButtonGroup group; private String command; private Color c; public DrawListener(ButtonGroup group){ this.group = group; } public void mousePressed(MouseEvent e) { //获得获得事件源对象 JPanel center =(JPanel) e.getSource(); // 只需要绘制在center上,所以需要从Center上取画布 // 获得center组件在屏幕上占据的区域 g = center.getGraphics(); // 获得要绘制的形状 // 得到按钮组中被选中的按钮的动作命令 command =group.getSelection().getActionCommand(); x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); if(command.equals("line")){ Line line = new Line(x1, y1, x2, y2, c); list.add(line); line.draw(g); }else if(command.equals("rect")){ Rect rect =new Rect(x1, y1, x2, y2, c); list.add(rect); rect.draw(g); } } }
package 画图板升级0516; import java.awt.Color; import java.awt.Graphics; import java.io.Serializable; public abstract class Shape implements Serializable { int x1,x2,y1,y2; Color c; void draw(Graphics g){ } }
package 画图板升级0516; import java.awt.Color; import java.awt.Graphics; public class Line extends Shape { public Line(int x1, int y1, int x2, int y2, Color c) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.c = c; } public void draw(Graphics g){ g.setColor(c); g.drawLine(x1, y1, x2, y2); } }
package 画图板升级0516; import java.awt.Color; import java.awt.Graphics; public class Rect extends Shape { public Rect(int x1, int y1, int x2, int y2, Color c) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.c = c; } public void draw(Graphics g){ g.setColor(c); g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } }