[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();
}
}
}