实现步骤:
1. 主界面
主要方法:
继承JFrame类,并获得其内容面板
getContentPane();
获得内容面板的画布对象:
Graphic g=dPane.getGrahpics();
调用重绘方法
repaint();
自动调用内部类paint()方法。实现重绘。
2. 添加按钮监听器
继承鼠标监听器和事件监听器。
重写actionPerformed()等方法
3. 自定义保存与打开按钮时响应的方法
主界面:MainFrame类
public class MainFrame extends JFrame { JPanel cPane; private List<Graph> List = new ArrayList<Graph>(); /** * 构造方法,初始化界面 */ public MainFrame() { //窗体相关属性 super("画板_1.0"); setSize(1000, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); //获得窗体内容面板 cPane = (JPanel) getContentPane(); cPane.setLayout(new BorderLayout()); JPanel mPane = new JPanel(); JPanel bPane = new JPanel(); JPanel dPane = new DrawPanel(); mPane.setBackground(Color.orange); bPane.setBackground(Color.DARK_GRAY); dPane.setBackground(Color.WHITE); cPane.add(BorderLayout.SOUTH, mPane); cPane.add(BorderLayout.WEST, bPane); cPane.add(BorderLayout.CENTER, dPane); // 菜单以及菜单子项 MenuBar bar = new MenuBar(); Menu m_file = new Menu("File"); Menu m_help = new Menu("Help"); MenuItem mi_new = new MenuItem("New"); MenuItem mi_open = new MenuItem("Open"); MenuItem mi_save = new MenuItem("Save"); m_file.add(mi_new); m_file.add(mi_open); m_file.add(mi_save); bar.add(m_file); bar.add(m_help); // 添加菜单栏 setMenuBar(bar); // 添加按钮 JButton btn_line = new JButton("直线"); JButton btn_curve = new JButton("曲线"); JButton btn_rectangle = new JButton("矩形"); JButton btn_oval = new JButton("圆"); JButton btn_color = new JButton("颜色"); bPane.setLayout(new GridLayout(15, 1)); bPane.add(btn_line); bPane.add(btn_curve); bPane.add(btn_rectangle); bPane.add(btn_oval); bPane.add(btn_color); JButton btn_save = new JButton("保存"); JButton btn_open = new JButton("打开"); mPane.add(btn_save); mPane.add(btn_open); // 设置窗体可见 setVisible(true); // 取的画布[ Graphics g = dPane.getGraphics(); // 创建按钮监听对象,并传入画板以及当前窗体 bl = new ButtonListener(g, dPane, this); // 为按钮添加动作监听器 btn_line.addActionListener(bl); btn_curve.addActionListener(bl); btn_rectangle.addActionListener(bl); btn_oval.addActionListener(bl); btn_color.addActionListener(bl); btn_save.addActionListener(bl); btn_open.addActionListener(bl); // 为画板添加鼠标监听器的方法 dPane.addMouseListener(bl); dPane.addMouseMotionListener(bl); } class DrawPanel extends JPanel { /** * 重绘方法 */ public void paint(Graphics g) { super.paint(g); if (null != bl) { System.out.println("====-------------->" + bl.getGraphList().size()); // 取出队列中保存信息 for (int i = 0; i < bl.getGraphList().size(); i++) { Graph graph = bl.getGraphList().get(i); Color c = graph.getColor(); if (graph.getItem().equals("直线") || graph.getItem().equals("曲线")) { g.setColor(c); g.drawLine(graph.getX1(), graph.getY1(), graph.getX2(), graph.getY2()); } else if (graph.getItem().equals("矩形")) { g.setColor(c); g.fillRect(graph.getX1(), graph.getY1(), graph.getX2(), graph.getY2()); } else if (graph.getItem().equals("圆")) { g.setColor(c); g.fillOval(graph.getX1(), graph.getY1(), graph.getX2(), graph.getY2()); } } } } } /** * 主方法 * * @param args */ public static void main(String[] args) { new MainFrame(); } //自定义按钮监听器 private ButtonListener bl; }
按钮监听类:ButtonListener类
public class ButtonListener extends MouseAdapter implements ActionListener { private Graphics g; private Color color = Color.black; private JPanel dPane; private int x1, y1, x2, y2; private String item = "直线"; private MainFrame mf; // 定义计数器变量 private int count = 0; // 创建Graph类型的存储队列 private List<Graph> list = new ArrayList<Graph>(); // 定义保存的路径 String path = "src\\cn\\cjl\\study0428\\drawer\\save.txt"; // 传入画板以及要重绘的容器 public ButtonListener(Graphics g, JPanel dPane, MainFrame mf) { this.mf = mf; this.g = g; this.dPane = dPane; } public void Fresh() { // 调用MainFram的paint()方法 dPane.repaint(); } /** * 按钮监听响应方法 */ public void actionPerformed(ActionEvent e) { // 判断所选的按钮是否为颜色 if (e.getActionCommand().equals("颜色")) { color = javax.swing.JColorChooser.showDialog(null, "颜色选择", Color.black); } else { item = e.getActionCommand(); if (item.equals("保存")) {// 保存到文件 SaveOpen so = new SaveOpen(); try { so.save(path, list); } catch (IOException e1) { e1.printStackTrace(); } } else if (item.equals("打开")) {// 从文件中读取 System.out.println("====>"); SaveOpen so = new SaveOpen(); try { list = so.open(path); } catch (IOException e1) { } // 刷新 Fresh(); mf.repaint(); } } } // 鼠标按下 public void mousePressed(MouseEvent e) { // 判断为第一次点击,记录坐标 if (count == 0) { x1 = e.getX(); y1 = e.getY(); if (!item.equals("曲线")) { count++; } } // 计数器的值为1,则绘制下列图形 else if (count == 1) { // 记录第二次点击的坐标值 x2 = e.getX(); y2 = e.getY(); g.setColor(color); if (item.equals("矩形")) { g.fillRect(x1, y1, x2, y2); addGraphList(color, item, x1, y1, x2, y2); } else if (item.equals("直线")) { g.drawLine(x1, y1, x2, y2); addGraphList(color, item, x1, y1, x2, y2); } else if (item.equals("圆")) { g.fillOval(x1, y1, x2, y2); addGraphList(color, item, x1, y1, x2, y2); } count--; } } // 鼠标拖动,绘制曲线 public void mouseDragged(MouseEvent e) { if (item.equals("曲线")) { x2 = e.getX(); y2 = e.getY(); g.setColor(color); g.drawLine(x1, y1, x2, y2); addGraphList(color, item, x1, y1, x2, y2); // 将最后一个点作为起始坐标 x1 = x2; y1 = y2; } } /** * 将图形数据添加到队列中 */ public void addGraphList(Color color, String item, int x1, int y1, int x2, int y2) { Graph graph = new Graph(color, item, x1, y1, x2, y2); // 添加到队列中 list.add(graph); } /** * 返回队列的方法 */ public List<Graph> getGraphList() { return list; } }
封装图像数据类:Graph类
public class Graph { private int x1,y1,x2,y2; private Color color; private String item; public Graph(Color color,String item,int x1,int y1,int x2,int y2){ this.color=color; this.item=item; this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; } public int getX1() { return x1; } public void setX1(int x1) { this.x1 = x1; } public int getY1() { return y1; } public void setY1(int y1) { this.y1 = y1; } public int getX2() { return x2; } public void setX2(int x2) { this.x2 = x2; } public int getY2() { return y2; } public void setY2(int y2) { this.y2 = y2; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public String getItem() { return item; } public void setItem(String item) { this.item = item; } }
保存与打开按钮响应方法:SaveOpen类
public class SaveOpen { /** * 保存到文件 * @throws IOException */ public void save(String path,List<Graph> list) throws IOException{ //输出流 FileOutputStream fos=new FileOutputStream(path); //将文件输出流包装成可写基本类型的流 DataOutputStream dos=new DataOutputStream(fos); System.out.println("--------------------->"+list.size()); dos.writeInt(list.size()); for(int i=0;i<list.size();i++){ Graph graph=list.get(i); //获取选择图像 String item=graph.getItem(); //定义一个图形变量 int type=0; //得到坐标值 int x1=graph.getX1(); int y1=graph.getY1(); int x2=graph.getX2(); int y2=graph.getY2(); //判断为圆 if(item.equals("圆")){ type=2; } //判断为矩形 else if(item.equals("矩形")){ type=1; } //判断为曲线或者直线 else{ type=0; } dos.writeInt(type); dos.writeInt(x1); dos.writeInt(y1); dos.writeInt(x2); dos.writeInt(y2); //写入颜色 int rgb=graph.getColor().getRGB(); dos.writeInt(rgb); } dos.flush(); fos.close(); } /** * 读取文件 * @throws IOException */ public List<Graph> open(String path) throws IOException{ List<Graph> list=new ArrayList<Graph>(); //输入流 InputStream fis=new FileInputStream(path); DataInputStream dis=new DataInputStream(fis); //长度 int len=dis.readInt(); System.out.println("===>"+len); //循环输出 for(int i=0;i<len;i++){ int type=dis.readInt(); int x1=dis.readInt(); int y1=dis.readInt(); int x2=dis.readInt(); int y2=dis.readInt(); int rgb=dis.readInt(); Color color=new Color(rgb); if(type==0){ String item="直线"; //实例化Graph对象 Graph graph=new Graph(color,item,x1,y1,x2,y2); list.add(graph); } else if(type==1){ String item="矩形"; //实例化Graph对象 Graph graph=new Graph(color,item,x1,y1,x2,y2); list.add(graph); } else if(type==2){ String item="圆"; //实例化Graph对象 Graph graph=new Graph(color,item,x1,y1,x2,y2); list.add(graph); } } dis.close(); return list; } }