每1秒java绘制一行

时间:2015-03-24 14:41:41

标签: java swing

我有一个带有坐标点的链表。我想每秒绘制一行。以下是绘图功能:

public void draw(Graphics g){
    for (Line line : lines) {
        g.drawLine(line.x1, line.y1, line.x2, line.y2);
    }
}

2 个答案:

答案 0 :(得分:3)

我认为这会有所帮助,

 int i = 0;
 Timer timer = new Timer(1000, null);
    timer.setInitialDelay(0);

    timer.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(i < lines.length){
                Line line = lines[i];
                g.drawLine(line.x1, line.y1, line.x2, line.y2);
                i++;
            }else{
                timer.stop();
            }
        }
    });
    timer.start();

答案 1 :(得分:0)

你可以创建一个新线程并等待1000毫秒:

public void draw(Graphics g){

    new Thread(new Runnable() {

        @Override
        public void run() {
            for (Line line : lines) {
                g.drawLine(line.x1, line.y1, line.x2, line.y2);
                Thread.sleep(1000);
            }

        }
    }).start();
}