1、 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class huahua extends JFrame { DrawPanel drawingArea; int index = 0; drewings[] itemList = new drewings[5000]; Container c = getContentPane();
2、 public huahua() { super("简单画板"); drawingArea = new DrawPanel(); c.add(drawingArea, BorderLayout.CENTER); itemList[index] = new Penci(); setSize(500 , 500) ; setVisible(true); show();
3、 } public static void main(String[] Strgs) { new huahua(); } class mousea extends MouseAdapter { public void mousePressed(MouseEvent e) { itemList[index].x1 = itemList[index].x2 = e.getX();
4、 itemList[index].y1 = itemList[index].y2 = e.getY(); index++; itemList[index] = new Penci(); } public void mouseReleased(MouseEvent e) { itemList[index].x1 = e.getX();
5、itemList[index].y1 = e.getY(); //重新获取x1y1的值 虽然移动的时候x1y1在变但那时移动监听的不是释放监听的 //如果不重新获取坐标将变成默认的0,0 itemList[index].x2 = e.getX(); itemList[index].y2 = e.getY(); repaint(); index++; itemL
6、ist[index] = new Penci(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class mouseb extends MouseMotionAdapter { public v
7、oid mouseDragged(MouseEvent e) { itemList[index - 1].x1 = itemList[index].x2 = itemList[index].x1 = e.getX(); itemList[index - 1].y1 = itemList[index].y2 = itemList[index].y1 = e.getY(); //按住移动是获取了x1y1的坐标但这坐标不会赋值给释放 动作不同不会相互赋值
8、 //index++; itemList[index] = new Penci(); repaint(); } public void mouseMoved(MouseEvent e) { } } class DrawPanel extends JPanel { public DrawPanel() { setB
9、ackground(Color.white); addMouseListener(new mousea()); addMouseMotionListener(new mouseb()); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Gra
10、phics2D) g; //定义画笔 int j = 0; while (j <= index) { drew(g2d, itemList[j]); j++; } } void drew(Graphics2D g2d, drewings i) { i.drew(g2d);//将画笔传入到各个子类中,用来完成各自的绘图
11、 } } } class drewings implements Serializable { int x1 , y1 , x2, y2; void drew(Graphics2D g2d) { } } class Penci extends drewings{ void drew(Graphics2D g2d) { g2d.drawLine(x1, y1, x2, y2); //System.out.println(x1); } }






