java 畫板畫圖程序

fpy7 9年前發布 | 3K 次閱讀 Java 生產計劃 物料管理 車間管理

import java.awt.*;

import javax.swing.; import java.awt.event.;

public class Board {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Winmain win = new Winmain();
}

}

class Winmain extends JFrame implements ActionListener, MouseMotionListener {// 主窗口 static int pensize, erasersize;// 畫筆大小和橡皮大小 static int size; static Color pencolor;// 畫筆顏色 JButton but1, but2, but3, but4; JPanel panel;

Winmain() {
    super("自由畫圖程序");
    this.setSize(250, 150);
    setBackground(Color.white);// 背景為白色
    Container con = getContentPane();
    con.setLayout(new BorderLayout());
    JPanel pa = new JPanel();
    pa.setLayout(new GridLayout(1, 4));
    but1 = new JButton("畫筆");// 快速轉換到畫筆
    but1.addActionListener(this);
    pa.add(but1);
    but2 = new JButton("橡皮");// 快速轉換到橡皮
    but2.addActionListener(this);
    pa.add(but2);
    but3 = new JButton("畫筆…");// 打開畫筆設置界面
    but3.addActionListener(this);
    pa.add(but3);
    but4 = new JButton("橡皮…");// 打開橡皮設置界面
    but4.addActionListener(this);
    pa.add(but4);
    con.add(pa, "North");
    panel = new JPanel();
    panel.setBackground(Color.white);// 設置背景為白色
    panel.addMouseMotionListener(this);
    con.add(panel, "Center");
    pencolor = Color.black;// 初始畫筆顏色為黑色
    pensize = 3;// 初始畫筆大小半徑為3個像素點
    erasersize = 5;// 初始橡皮大小半徑為5個像素點
    size = 3;
    setVisible(true);
    pack();
}

public static void setpen(int pensize2, Color pencolor2) {// 與設置畫筆界面的接口
    pensize = pensize2;
    pencolor = pencolor2;
    size = pensize;
}

public static void seteraser(int erasersize2) {// 與設置橡皮界面的接口
    erasersize = erasersize2;
    pencolor = Color.white;
    size = erasersize;
}

public void actionPerformed(ActionEvent e1) {
    if (e1.getSource() == but1) {
        pensize = 3;
        size = pensize;
        pencolor = Color.black;
    } else if (e1.getSource() == but2) {
        erasersize = 5;
        size = erasersize;
        pencolor = Color.white;
    } else if (e1.getSource() == but3) {// 打開畫筆設置界面
        Winpen741 w741 = new Winpen741();
        w741.setVisible(true);
    } else if (e1.getSource() == but4) {// 打開橡皮設置界面
        Wineraser742 w742 = new Wineraser742();
        w742.setVisible(true);
    }
}

public void mouseDragged(MouseEvent e2) {// 拖動鼠標自由作畫
    int x, y;
    x = e2.getX();
    y = e2.getY();
    Graphics pen;
    pen = getGraphics();
    pen.setColor(pencolor);
    pen.fillOval(x - size + 7, y - size + 56, 2 * size, 2 * size);// +7和+56是為了矯正畫筆位置
}

public void mouseMoved(MouseEvent e3) {

}

}

class Winpen741 extends JFrame implements ActionListener {// 設置畫筆界面 JButton but, but1, but2, but3, but4, but5, but6; JTextField tf; Color c; int pensize;

Winpen741() {
    super();
    setSize(300, 150);
    Container con = getContentPane();
    con.setLayout(new GridLayout(2, 1));
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(2, 3));
    but1 = new JButton();
    but1.setBackground(Color.pink);
    but1.addActionListener(this);
    p1.add(but1);
    but2 = new JButton();
    but2.setBackground(Color.blue);
    but2.addActionListener(this);
    p1.add(but2);
    but3 = new JButton();
    but3.setBackground(Color.yellow);
    but3.addActionListener(this);
    p1.add(but3);
    but4 = new JButton();
    but4.setBackground(Color.gray);
    but4.addActionListener(this);
    p1.add(but4);
    but5 = new JButton();
    but5.setBackground(Color.green);
    but5.addActionListener(this);
    p1.add(but5);
    but6 = new JButton();
    but6.setBackground(Color.red);
    but6.addActionListener(this);
    p1.add(but6);
    con.add(p1);
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(1, 3));
    JLabel la = new JLabel("輸入畫筆的大小");
    p2.add(la);
    tf = new JTextField(16);
    p2.add(tf);
    but = new JButton("確定");
    but.addActionListener(this);
    p2.add(but);
    con.add(p2);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == but1)
        c = Color.pink;
    else if (e.getSource() == but2)
        c = Color.blue;
    else if (e.getSource() == but3)
        c = Color.yellow;
    else if (e.getSource() == but4)
        c = Color.gray;
    else if (e.getSource() == but5)
        c = Color.green;
    else if (e.getSource() == but6)
        c = Color.red;
    else if (e.getSource() == but) {
        String s = null;
        s = tf.getText();
        pensize = Integer.parseInt(s);
        Winmain.setpen(pensize, c);// 返回畫筆大小和顏色
        this.setVisible(false);
        this.dispose();
    }
}

}

class Wineraser742 extends JFrame implements ActionListener {// 設置橡皮界面 JTextField tf; JButton but; int erasersize;

Wineraser742() {
    super();
    setSize(300, 150);
    Container con = getContentPane();
    con.setLayout(new GridLayout(1, 3));
    JLabel la = new JLabel("輸入橡皮的大小");
    con.add(la);
    tf = new JTextField(16);
    con.add(tf);
    but = new JButton("確定");
    but.addActionListener(this);
    con.add(but);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == but) {
        String s = null;
        s = tf.getText();
        erasersize = Integer.parseInt(s);
        Winmain.seteraser(erasersize);// 返回橡皮大小
        this.setVisible(false);
        this.dispose();
    }
}

}</pre>

 本文由用戶 fpy7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!