package draw;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class TestMouse extends JFrame {
public void init()
{
//TestMouse ts=new TestMouse();
String[] Shape = { "直线", "圆", "矩形", "点","橡皮擦","画笔" };
Color[] color = { Color.BLACK, Color.BLUE, Color.green, Color.PINK, Color.YELLOW,Color.cyan,Color.red};
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel j1 = new JPanel();
JPanel j2 = new JPanel();
JPanel j3 = new JPanel();
DrawListener dl = new DrawListener(j2);
setTitle("画图version2");
setSize(700, 800);
j1.setBackground(Color.white);
j2.setBackground(Color.white);
j3.setBackground(Color.white);
setLayout(new BorderLayout());
add(j1, BorderLayout.NORTH);
add(j2, BorderLayout.CENTER);
add(j3, BorderLayout.SOUTH);
j1.setSize(100, 800);
j2.setSize(500, 800);
j3.setSize(100, 800);
for (int i = 0; i < Shape.length; i++) {
JButton btn = new JButton(Shape[i]);
j1.add(btn);
btn.addActionListener(dl);
}
for (int i = 0; i < color.length; i++) {
JButton btn = new JButton();
btn.setBackground(color[i]);
btn.addActionListener(dl);
j3.add(btn);
}
setVisible(true);
j2.addMouseListener(dl);
j2.addMouseMotionListener(dl);
}
public static void main(String[] args) {
TestMouse ts=new TestMouse();
ts.init();
}
}
package draw;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class DrawListener extends Canvas implements ActionListener, MouseListener, MouseMotionListener {
private int x1, x2, y1, y2;
private String Shape = "直线";
private Color color;
private JPanel j2;
private Graphics2D g;
private int x4, y4;
public DrawListener(JPanel j2) {
// TODO Auto-generated constructor stub
this.j2 = j2;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("")) {
JButton b = (JButton) e.getSource();
color = b.getBackground();
System.out.println(color);
} else {
JButton b = (JButton) e.getSource();
Shape = b.getActionCommand();
System.out.println(Shape);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
x1 = arg0.getX();
y1 = arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
x2 = arg0.getX();
y2 = arg0.getY();
g = (Graphics2D) j2.getGraphics();
g.setColor(color);
if (Shape.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
} else if (Shape.equals("圆")) {
if (x1 < x2 && y1 < y2) {
g.drawOval(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 > y2) {
g.drawOval(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.drawOval(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.drawOval(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
// g.drawOval(x1, y1, x2 / 2, y2 / 2);
} else if (Shape.equals("矩形")) {
if (x1 < x2 && y1 < y2) {
g.drawRect(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 > y2) {
g.clearRect(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.clearRect(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.clearRect(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
} else if (Shape.equals("点")) {
if (x1 < x2 && y1 < y2) {
g.fillOval(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 > y2) {
g.fillOval(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.fillOval(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.fillOval(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
g = (Graphics2D) j2.getGraphics();
g.setColor(color);
x2 = arg0.getX();
y2 = arg0.getY();
if (Shape.equals("画笔")) {
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
} else if (Shape.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
try {
j2.repaint();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.dispose();
} else if (Shape.equals("圆")) {
if (x1 < x2 && y1 < y2) {
g.drawOval(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
try {
j2.repaint();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (x1 > x2 && y1 > y2) {
g.drawOval(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.drawOval(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.drawOval(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
// g.drawOval(x1, y1, x2 / 2, y2 / 2);
} else if (Shape.equals("矩形")) {
if (x1 < x2 && y1 < y2) {
// g.clearRect(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
try {
j2.repaint();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (x1 > x2 && y1 > y2) {
g.clearRect(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.clearRect(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.clearRect(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
g.drawRect(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
} else if (Shape.equals("点")) {
if (x1 < x2 && y1 < y2) {
try {
j2.repaint();
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.fillOval(x1, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 > y2) {
g.fillOval(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.fillOval(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.fillOval(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
g = (Graphics2D) j2.getGraphics();
g.setColor(Color.white);
x4 = arg0.getX();
y4 = arg0.getY();
if (Shape.equals("橡皮擦")) {
g.fillOval(x4, y4, x2, y2);
}
}
}
实现了拖动可显示轨迹,但是画板一次只能绘画一个图形(这个问题还没解决)