[Java] java 画板画图程序 →→→→→进入此内容的聊天室

来自 , 2020-10-01, 写在 Java, 查看 225 次.
URL http://www.code666.cn/view/bf62768c
  1. import java.awt.*;
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. public class Board {
  7.  
  8.         /**
  9.          * @param args
  10.          */
  11.         public static void main(String[] args) {
  12.                 // TODO Auto-generated method stub
  13.                 Winmain win = new Winmain();
  14.         }
  15.  
  16. }
  17.  
  18. class Winmain extends JFrame implements ActionListener, MouseMotionListener {// 主窗口
  19.         static int pensize, erasersize;// 画笔大小和橡皮大小
  20.         static int size;
  21.         static Color pencolor;// 画笔颜色
  22.         JButton but1, but2, but3, but4;
  23.         JPanel panel;
  24.  
  25.         Winmain() {
  26.                 super("自由画图程序");
  27.                 this.setSize(250, 150);
  28.                 setBackground(Color.white);// 背景为白色
  29.                 Container con = getContentPane();
  30.                 con.setLayout(new BorderLayout());
  31.                 JPanel pa = new JPanel();
  32.                 pa.setLayout(new GridLayout(1, 4));
  33.                 but1 = new JButton("画笔");// 快速转换到画笔
  34.                 but1.addActionListener(this);
  35.                 pa.add(but1);
  36.                 but2 = new JButton("橡皮");// 快速转换到橡皮
  37.                 but2.addActionListener(this);
  38.                 pa.add(but2);
  39.                 but3 = new JButton("画笔…");// 打开画笔设置界面
  40.                 but3.addActionListener(this);
  41.                 pa.add(but3);
  42.                 but4 = new JButton("橡皮…");// 打开橡皮设置界面
  43.                 but4.addActionListener(this);
  44.                 pa.add(but4);
  45.                 con.add(pa, "North");
  46.                 panel = new JPanel();
  47.                 panel.setBackground(Color.white);// 设置背景为白色
  48.                 panel.addMouseMotionListener(this);
  49.                 con.add(panel, "Center");
  50.                 pencolor = Color.black;// 初始画笔颜色为黑色
  51.                 pensize = 3;// 初始画笔大小半径为3个像素点
  52.                 erasersize = 5;// 初始橡皮大小半径为5个像素点
  53.                 size = 3;
  54.                 setVisible(true);
  55.                 pack();
  56.         }
  57.  
  58.         public static void setpen(int pensize2, Color pencolor2) {// 与设置画笔界面的接口
  59.                 pensize = pensize2;
  60.                 pencolor = pencolor2;
  61.                 size = pensize;
  62.         }
  63.  
  64.         public static void seteraser(int erasersize2) {// 与设置橡皮界面的接口
  65.                 erasersize = erasersize2;
  66.                 pencolor = Color.white;
  67.                 size = erasersize;
  68.         }
  69.  
  70.         public void actionPerformed(ActionEvent e1) {
  71.                 if (e1.getSource() == but1) {
  72.                         pensize = 3;
  73.                         size = pensize;
  74.                         pencolor = Color.black;
  75.                 } else if (e1.getSource() == but2) {
  76.                         erasersize = 5;
  77.                         size = erasersize;
  78.                         pencolor = Color.white;
  79.                 } else if (e1.getSource() == but3) {// 打开画笔设置界面
  80.                         Winpen741 w741 = new Winpen741();
  81.                         w741.setVisible(true);
  82.                 } else if (e1.getSource() == but4) {// 打开橡皮设置界面
  83.                         Wineraser742 w742 = new Wineraser742();
  84.                         w742.setVisible(true);
  85.                 }
  86.         }
  87.  
  88.         public void mouseDragged(MouseEvent e2) {// 拖动鼠标自由作画
  89.                 int x, y;
  90.                 x = e2.getX();
  91.                 y = e2.getY();
  92.                 Graphics pen;
  93.                 pen = getGraphics();
  94.                 pen.setColor(pencolor);
  95.                 pen.fillOval(x - size + 7, y - size + 56, 2 * size, 2 * size);// +7和+56是为了矫正画笔位置
  96.         }
  97.  
  98.         public void mouseMoved(MouseEvent e3) {
  99.  
  100.         }
  101. }
  102.  
  103. class Winpen741 extends JFrame implements ActionListener {// 设置画笔界面
  104.         JButton but, but1, but2, but3, but4, but5, but6;
  105.         JTextField tf;
  106.         Color c;
  107.         int pensize;
  108.  
  109.         Winpen741() {
  110.                 super();
  111.                 setSize(300, 150);
  112.                 Container con = getContentPane();
  113.                 con.setLayout(new GridLayout(2, 1));
  114.                 JPanel p1 = new JPanel();
  115.                 p1.setLayout(new GridLayout(2, 3));
  116.                 but1 = new JButton();
  117.                 but1.setBackground(Color.pink);
  118.                 but1.addActionListener(this);
  119.                 p1.add(but1);
  120.                 but2 = new JButton();
  121.                 but2.setBackground(Color.blue);
  122.                 but2.addActionListener(this);
  123.                 p1.add(but2);
  124.                 but3 = new JButton();
  125.                 but3.setBackground(Color.yellow);
  126.                 but3.addActionListener(this);
  127.                 p1.add(but3);
  128.                 but4 = new JButton();
  129.                 but4.setBackground(Color.gray);
  130.                 but4.addActionListener(this);
  131.                 p1.add(but4);
  132.                 but5 = new JButton();
  133.                 but5.setBackground(Color.green);
  134.                 but5.addActionListener(this);
  135.                 p1.add(but5);
  136.                 but6 = new JButton();
  137.                 but6.setBackground(Color.red);
  138.                 but6.addActionListener(this);
  139.                 p1.add(but6);
  140.                 con.add(p1);
  141.                 JPanel p2 = new JPanel();
  142.                 p2.setLayout(new GridLayout(1, 3));
  143.                 JLabel la = new JLabel("输入画笔的大小");
  144.                 p2.add(la);
  145.                 tf = new JTextField(16);
  146.                 p2.add(tf);
  147.                 but = new JButton("确定");
  148.                 but.addActionListener(this);
  149.                 p2.add(but);
  150.                 con.add(p2);
  151.         }
  152.  
  153.         public void actionPerformed(ActionEvent e) {
  154.                 if (e.getSource() == but1)
  155.                         c = Color.pink;
  156.                 else if (e.getSource() == but2)
  157.                         c = Color.blue;
  158.                 else if (e.getSource() == but3)
  159.                         c = Color.yellow;
  160.                 else if (e.getSource() == but4)
  161.                         c = Color.gray;
  162.                 else if (e.getSource() == but5)
  163.                         c = Color.green;
  164.                 else if (e.getSource() == but6)
  165.                         c = Color.red;
  166.                 else if (e.getSource() == but) {
  167.                         String s = null;
  168.                         s = tf.getText();
  169.                         pensize = Integer.parseInt(s);
  170.                         Winmain.setpen(pensize, c);// 返回画笔大小和颜色
  171.                         this.setVisible(false);
  172.                         this.dispose();
  173.                 }
  174.         }
  175. }
  176.  
  177. class Wineraser742 extends JFrame implements ActionListener {// 设置橡皮界面
  178.         JTextField tf;
  179.         JButton but;
  180.         int erasersize;
  181.  
  182.         Wineraser742() {
  183.                 super();
  184.                 setSize(300, 150);
  185.                 Container con = getContentPane();
  186.                 con.setLayout(new GridLayout(1, 3));
  187.                 JLabel la = new JLabel("输入橡皮的大小");
  188.                 con.add(la);
  189.                 tf = new JTextField(16);
  190.                 con.add(tf);
  191.                 but = new JButton("确定");
  192.                 but.addActionListener(this);
  193.                 con.add(but);
  194.         }
  195.  
  196.         public void actionPerformed(ActionEvent e) {
  197.                 if (e.getSource() == but) {
  198.                         String s = null;
  199.                         s = tf.getText();
  200.                         erasersize = Integer.parseInt(s);
  201.                         Winmain.seteraser(erasersize);// 返回橡皮大小
  202.                         this.setVisible(false);
  203.                         this.dispose();
  204.                 }
  205.         }
  206. }

回复 "java 画板画图程序"

这儿你可以回复上面这条便签

captcha