import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.ArrayList;
import java.awt.Point;
class Mouse extends MouseAdapter
{
protected ArrayList<Point> points=null;
int endX=0,endY=0;
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==1)
points.add(new Point(e.getX(),e.getY()));
else if(e.getClickCount()==2)
{
endX=e.getX();
endY=e.getY();
}
}
}
class MyPanel extends JPanel
{
private Mouse mouse=new Mouse();
public MyPanel()
{
this.addMouseListener(mouse);
}
public void paint(Graphics g)
{
int k=mouse.points.size();
for(int i=0;i<k-1;i++)
g.drawLine(mouse.points.get(i).x, mouse.points.get(i).y, mouse.points.get(i+1).x, mouse.points.get(i+1).y);
g.drawLine(mouse.endX,mouse.endY,mouse.points.get(1).x, mouse.points.get(1).y);
}
}
public class Paint extends JFrame{
private MyPanel mpl=new MyPanel();
public Paint()
{
this.add(mpl);
this.setSize(400,400);
this.setVisible(true);
}
public static void main(String[] args) throws Exception
{
new Paint();
}
}
int k=mouse.points.size();
上面這一句有問題。mouse.points是null,你還要調用size()方法,可不就是空指針異常了呢。