当我尝试以这种形状绘制单行时,我不知道问题出在哪里。 我想绘制多行,所以我将旧行的点存储在数组列表中,以便每次绘制新行时都重新绘制。
public class Lines11 extends java.applet.Applet
implements MouseListener, MouseMotionListener {
private final int MAXLINES = 10;
private ArrayList<Point> starts;
private ArrayList<Point> ends;
private Point anchor;
private Point currentpoint;
private int currlines;
public void init() {
starts = new ArrayList<Point>();
ends = new ArrayList<Point>();
currlines = 0;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
anchor = new Point(e.getX(),e.getY());
repaint();
}
public void mouseDragged(MouseEvent e)
{
currentpoint = new Point(e.getX(),e.getY());
addline(e.getX(),e.getY());
repaint();
}
public void mouseMoved(MouseEvent e) { }
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void paint(Graphics g) {
for (int i = 0 ; i < starts.size()&& i<ends.size() ; i++)
g.drawLine(starts.get(i).x,starts.get(i).y,
ends.get(i).x,ends.get(i).y);
}
void addline(int x, int y) {
starts.add(currlines,anchor);
ends.add(currlines,currentpoint);
currlines++;
currentpoint = null;
repaint();
}
}