import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
public class LineDrawer extends JPanel {
  private List<List<Point>> lines=new ArrayList<List<Point>>();  //存放所有的线条
  private List<Point> currLine; //当前线条

  public LineDrawer(){
    setBackground(Color.white);
    addMouseListener(new MouseAdapter(){
      //当用户按下鼠标，就开始绘制一条新的线条
      public void mousePressed(MouseEvent event){
        currLine=new ArrayList<Point>();
        lines.add(currLine);
        currLine.add(new Point(event.getX(),event.getY()));
      }
    });

    addMouseMotionListener(new MouseMotionAdapter(){
      //拖动鼠标时，随之画线条
      public void mouseDragged(MouseEvent event){
        currLine.add(new Point(event.getX(),event.getY()));
        repaint();
      }
    });

  }

  public void paintComponent(Graphics g){
    super.paintComponent(g); 

    for( List<Point>points:lines){
      int x1=-1,y1=-1,x2=-1,y2=-1;
      for(Point point:points){
        x2=(int)point.getX();
        y2=(int)point.getY();
        if(x1!=-1)
          g.drawLine(x1,y1,x2,y2);
        x1=x2;
        y1=y2;
      }
    }
  }
  public static void main(String args[]){
    JPanel panel=new LineDrawer();
    JFrame frame=new JFrame("Hello");
    frame.setContentPane(panel);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setVisible(true);
  }
}


/****************************************************
 * 作者：孙卫琴                                     *
 * 来源：<<Java面向对象编程>>                       *
 * 技术支持网址：www.javathinker.net                *
 ***************************************************/
