import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseLine extends JFrame
{
int x,y;
int endX,endY;
public TestMouseLine()
{
Container c=getContentPane();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(400,300);
setLocation(400,300);
show();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(x,y,endX,endY);
}
public static void main(String[] args)
{
new TestMouseLine();
}
}
为什么用鼠标画的时候有很多线呢?怎么样才能只有一跟线随着鼠标转呢?