自制画图板,欢迎各位大神给点意见。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class hua extends JFrame implements MouseMotionListener, ActionListener{
private JButton red;
private JButton green;
private JButton black;
private JButton eraser;
private Container con;
private JButton undo;
private JButton redo;
private int x1,y1,x2,y2,line=0,times=0;
private int rubber[] = new int[2];
hua(){
super("画图");
setDefaultCloseOperation(EXIT_ON_CLOSE);
con=this.getContentPane();
con.setLayout(new FlowLayout());
this.setBounds(50, 50, 500, 500);
this.setBackground(Color.yellow);
inta(); //初始化
this.addMouseMotionListener(this);
this.setVisible(true);
}
public void inta(){
black = new JButton("黑色");
red = new JButton("红色");
green = new JButton("绿色");
eraser = new JButton("橡皮");
undo = new JButton("Undo");
redo = new JButton("Redo");
black.addActionListener(this);
green.addActionListener(this);
eraser.addActionListener(this);
red.addActionListener(this);
con.add(red);
con.add(black);
con.add(green);
con.add(eraser);
}
public void mouseDragged(MouseEvent e){
rubber[0] = x2 = e.getX();
rubber[1] = y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){
x1=x2=e.getX();
y1=y2=e.getY();
}
public void paint(Graphics g){
if (line == 0) {
g.setColor(Color.black);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
else if (line == 1)
{
g.setColor(Color.red);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
else if (line == 2){
g.setColor(Color.green);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
else if(line == 4){
g.setColor(Color.yellow);
g.fillRect(rubber[0] - 3, rubber[1] - 3, 10, 10);
}
}
public void actionPerformed(ActionEvent a){
if (black == a.getSource())
line = 0;
else if (red == a.getSource())
line = 1;
else if (green == a.getSource())
line = 2;
else if (eraser == a.getSource())
line = 4;
}
}
class LoginFrame extends JFrame implements ActionListener{
private Container cont;
private JButton drawa;
private JLabel testa;
public LoginFrame(){
this.setTitle("绘图登陆界面");
this.setSize(300, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
addCompent();
this.setVisible(true);
}
public void addCompent(){
cont = this.getContentPane();
cont.setLayout(null);
testa = new JLabel("点击进入绘图");
drawa = new JButton("开始绘图");
testa.setBounds(90,0,80,100);
drawa.setBounds(90,100,100,30);
drawa.addActionListener(this);
cont.add(testa);
cont.add(drawa);
}
public void actionPerformed(ActionEvent a){
if(drawa == a.getSource()){
new hua();
this.dispose();
}
}
}
class drawline{
public static void main(String[] args){
new LoginFrame();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class DrawCurves extends JFrame implements MouseMotionListener, ActionListener{
private JButton btnRed;
private JButton black;
private JButton eraser;
private Container cont;
private int x1,y1,x2,y2,line=0,times=0;
private int rubber[] = new int[2];
DrawCurves(){
super("画图");
setDefaultCloseOperation(EXIT_ON_CLOSE);
cont=this.getContentPane();
cont.setLayout(new FlowLayout());
this.setBounds(50, 50, 500, 500);
this.setBackground(Color.yellow);
inta(); //初始化
this.addMouseMotionListener(this);
this.setVisible(true);
}
public void inta(){
black = new JButton("黑笔");
btnRed = new JButton("红笔");
eraser = new JButton("橡皮擦");
black.addActionListener(this);
eraser.addActionListener(this);
btnRed.addActionListener(this);
cont.add(black);
cont.add(btnRed);
cont.add(eraser);
}
public void mouseDragged(MouseEvent e){
rubber[0] = x2 = e.getX();
rubber[1] = y2 = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){
x1=x2=e.getX();
y1=y2=e.getY();
}
public void paint(Graphics g){
if (line == 0) {
g.setColor(Color.black);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
else if (line == 1)
{
g.setColor(Color.red);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
else if (line == 2){
g.setColor(Color.green);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
else if(line == 3){
g.setColor(Color.yellow);
g.fillRect(rubber[0] - 3, rubber[1] - 3, 10, 10);
}
}
public void actionPerformed(ActionEvent a){
if (black == a.getSource())
line = 0;
else if (btnRed == a.getSource())
line = 1;
else if (eraser == a.getSource())
line = 3;
}
}
class CurvesFrame extends JFrame implements ActionListener{
private Container cont;
private JButton drawa;
public CurvesFrame(){
this.setTitle("绘图登陆界面");
this.setSize(300, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
addCompent();
this.setVisible(true);
}
public void addCompent(){
cont = this.getContentPane();
cont.setLayout(null);
drawa = new JButton("进入绘图");
drawa.setBounds(90,100,100,30);
drawa.addActionListener(this);
cont.add(drawa);
}
public void actionPerformed(ActionEvent a){
if(drawa == a.getSource()){
new hua();
this.dispose();
}
}
}
class drawline{
public static void main(String[] args){
new CurvesFrame();
}
}