一个简单的画图板程序。
用到的图片在附件中。(新手,多包涵)
package netjava0626; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; //定义一个画板类 public class DrawingBoard { private String biaozhi = "huabi"; private String color; private Graphics g; private int x1, y1, x2, y2, x3, y3, w, h; private JLabel lab; // 初始化面板并实现组建 public void show() { JFrame frame = new JFrame(); // 创建一个工具栏 JButton but01 = new JButton("文件(F)"); JButton but02 = new JButton("编辑(E)"); JButton but03 = new JButton("查看(V)"); JButton but04 = new JButton("图像(I)"); JButton but05 = new JButton("颜色(C)"); JButton but06 = new JButton("帮助(H)"); JToolBar bar = new JToolBar(); bar.add(but01); bar.add(but02); bar.add(but03); bar.add(but04); bar.add(but05); bar.add(but06); frame.add(bar, BorderLayout.NORTH); //创建颜色框的监听器 ActionListener a3 = new ActionListener(){ public void actionPerformed(ActionEvent e) { //设置颜色 color = e.getActionCommand(); if(color.equals("Color.black") ){ g.setColor(Color.black); lab.setBackground(Color.black); }else if(color.equals("Color.gray")){ g.setColor(Color.gray); lab.setBackground(Color.gray); }else if(color.equals("Color.darkGray")){ g.setColor(Color.darkGray); lab.setBackground(Color.darkGray); }else if(color.equals("Color.green")){ g.setColor(Color.green); lab.setBackground(Color.green); }else if(color.equals("Color.LIGHT_GRAY")){ g.setColor(Color.LIGHT_GRAY); lab.setBackground(Color.LIGHT_GRAY); }else if(color.equals("Color.magenta")){ g.setColor(Color.magenta); lab.setBackground(Color.magenta); }else if(color.equals("Color.orange")){ g.setColor(Color.orange); lab.setBackground(Color.orange); }else if(color.equals("Color.pink")){ g.setColor(Color.pink); lab.setBackground(Color.pink); }else if(color.equals("Color.red")){ g.setColor(Color.red); lab.setBackground(Color.red); }else if(color.equals("Color.white")){ g.setColor(Color.white); lab.setBackground(Color.white); } } }; // 创建监听器 ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { biaozhi = e.getActionCommand(); } }; // 创建工具面板 JPanel panel01 = new JPanel(); panel01.setPreferredSize(new Dimension(60, 104)); String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi", "fangdajing", "huabi", "shuazi", "youqitong", "bianji", "zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" }; for (int i = 0; i < arr.length; i++) { ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg"); JButton but = new JButton(img); but.setActionCommand(arr[i]); but.addActionListener(al); but.setPreferredSize(new Dimension(24, 22)); panel01.add(but); } frame.add(panel01, BorderLayout.WEST); // 颜色选项栏 JPanel panel02 = new JPanel(); panel02.setPreferredSize(new Dimension(100, 50)); panel02.setLayout(null); JPanel panel04 = new JPanel(); panel04.setPreferredSize(new Dimension(100, 40)); // panel04.setBackground(Color.white ); panel04.setLayout(new GridLayout(2, 5)); Color [] arr02={Color.black,Color.gray,Color.darkGray,Color.green,Color.LIGHT_GRAY,Color.magenta, Color.orange,Color.pink,Color.red,Color.white}; String[] arr01 = {"Color.black","Color.gray","Color.darkGray","Color.green","Color.LIGHT_GRAY","Color.magenta", "Color.orange","Color.pink","Color.red","Color.white"}; lab = new JLabel(); lab.setPreferredSize(new Dimension()); lab.setOpaque(true); lab.setBackground(Color.black); lab.setBounds(60, 0, 47, 50); for (int i = 0; i < arr02.length; i++) { JButton but = new JButton(); but.setBackground(arr02[i]); but.setActionCommand(arr01[i]); but.setPreferredSize(new Dimension(16, 16)); but.addActionListener(a3); panel04.add(but); } panel02.add(lab); panel04.setBounds(107, 0, 100, 50); panel02.add(panel04); frame.add(panel02, BorderLayout.SOUTH); // 画布 JPanel panel03 = new JPanel(); panel03.setPreferredSize(new Dimension(694, 400)); panel03.setBackground(Color.WHITE); frame.add(panel03, BorderLayout.CENTER); frame.setTitle("画图板"); frame.setSize(754, 508); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(3); MouseListener a1 = new MouseAdapter() { // 鼠標按下時獲得坐标 public void mousePressed(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); } // 鼠标松开时获取坐标 public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); if (biaozhi.equals("shouwei")) { g.drawLine(x1, y1, x1, y1); } else if (biaozhi.equals("dianbi")) { } else if (biaozhi.equals("zhixian")) { g.drawLine(x1, y1, x2, y2); } else if (biaozhi.equals("juxing")) { if (x2 > x1 && y2 > y1) { x3 = x1; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } else if (x2 > x1 && y2 < y1) { x3 = x1; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } else if (x2 < x1 && y2 < y1) { x3 = x2; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } else { x3 = x2; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } } else if (biaozhi.equals("tuoyuan")) { if (x2 > x1 && y2 > y1) { x3 = x1; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } else if (x2 > x1 && y2 < y1) { x3 = x1; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } else if (x2 < x1 && y2 < y1) { x3 = x2; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } else { x3 = x2; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } } else if (biaozhi.equals("yuanji")) { if (x2 > x1 && y2 > y1) { x3 = x1; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } else if (x2 > x1 && y2 < y1) { x3 = x1; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } else if (x2 < x1 && y2 < y1) { x3 = x2; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } else { x3 = x2; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } } } }; // 添加拖動時的方法 MouseMotionListener a2 = new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { x3 = e.getX(); y3 = e.getY(); // 当鼠标在拖动时也要注意对坐标的赋值 if (biaozhi.equals("huabi")) { g.drawLine(x1, y1, x3, y3); x1 = x3; y1 = y3; } else if (biaozhi.equals("ca")) { g.setColor(Color.white); g.fillRect(x1, y1, 10, 20); x1 = x3; y1 = y3; g.setColor(Color.black); } else if (biaozhi.equals("shuazi")) { g.fillRect(x1, y1, 5, 5); x1 = x3; y1 = y3; } else if (biaozhi.equals("youqitong")) { for (int i = 0; i < 20; i++) { int j = (int) (Math.random() * 10); int k = (int) (Math.random() * 10); g.drawLine(x3 + j, y3 + k, x3 + j, y3 + k); } } } }; panel03.addMouseListener(a1); panel03.addMouseMotionListener(a2); frame.setVisible(true); g = panel03.getGraphics(); } public static void main(String[] args) { DrawingBoard draw = new DrawingBoard(); draw.show(); } }