在Java applet中动态绘制多行

时间:2016-01-15 20:56:41

标签: java applet

当我尝试以这种形状绘制单行时,我不知道问题出在哪里。 我想绘制多行,所以我将旧行的点存储在数组列表中,以便每次绘制新行时都重新绘制。

enter image description here

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();
    }
}

0 个答案:

没有答案