如何延迟java中正在进行的绘画

时间:2017-08-17 06:11:42

标签: java swing jpanel

所以我在JPanel中绘制线条,我希望每次绘制一条线时暂停一下。我已经使用过TimeUnit和Timer之类的其他东西,我发现环顾这个网站,但到目前为止,我无法让它工作。我已经准备好了JFrame,这只是最后一块。此外,我想知道是否有效的方法可以应用于其他类型的形状和事物,而不仅仅是线条。

public class Panel extends JPanel { 
  Color [] colors = {Color.WHITE, Color.BLUE, Color.RED,Color.YELLOW,Color.CYAN,Color.GREEN, Color.PINK,Color.ORANGE};
  Random r = new Random();
  int amount;   
  public Panel(int amount) {
      this.amount = amount;
  } 
  public void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      for (int i = 0; i < amount; i++) {
          Timer timer = new Timer(2000, new ActionListener() {
              public void actionPerformed(ActionEvent evt) {      
                    g2.setColor(colors[r.nextInt(7)]);
                    g2.drawLine(r.nextInt(600), r.nextInt(600), r.nextInt(600), r.nextInt(600));
              }
             });
          timer.setRepeats(false);
          timer.start();
      }
  }
}

3 个答案:

答案 0 :(得分:2)

这是我的任务解决方案

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class Panel extends JPanel {
    Color[] colors = {Color.WHITE, Color.BLUE, Color.RED, Color.YELLOW, Color.CYAN, Color.GREEN, Color.PINK, Color.ORANGE};

    Random r = new Random();

    int amount, actual;

    Color nextColor;

    int x1, y1, x2, y2;

    public Panel(int amount) {
        this.amount = amount;
        Timer timer = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                nextColor = colors[r.nextInt(7)];
                x1 = r.nextInt(600);
                x2 = r.nextInt(600);
                y1 = r.nextInt(600);
                y2 = r.nextInt(600);
                repaint();
                if (++actual >= amount) {
                    Timer t = (Timer) evt.getSource();
                    t.stop();
                }
            }
        });
        timer.setRepeats(true);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        if (nextColor != null) {
            g.setColor(nextColor);
            g.drawLine(x1, y1, x2, y2);
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frm = new JFrame("Lines");

                frm.add(new Panel(10));
                frm.setSize(700, 700);
                frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:0)

你的打印没有延迟,但因为你在for循环中写了timer.start(),它在两秒之前执行得很多,从而调用下一个timer.start(),因此好像计时器不工作。 我建议你使用另一种逻辑。是的,这种方法也可以应用于其他形状。

答案 2 :(得分:-2)

您可以将其添加到您的代码中

public class Panel extends JPanel { 
Color [] colors = {Color.WHITE, Color.BLUE, Color.RED,Color.YELLOW,Color.CYAN,Color.GREEN, Color.PINK,Color.ORANGE};
Random r = new Random();
int amount; 
public Panel(int amount) {
    this.amount = amount;
}   
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    for (int i = 0; i < amount; i++) {
        Timer timer = new Timer(2000, new ActionListener() {
              public void actionPerformed(ActionEvent evt) {      
                    g2.setColor(colors[r.nextInt(7)]);
                    g2.drawLine(r.nextInt(600), r.nextInt(600), r.nextInt(600), r.nextInt(600));
TimeUnit.SECONDS.sleep(1);
              }
           });
        timer.setRepeats(false);
        timer.start();
    }
}

您也可以使用Thread.sleep(1000)代替TimeUnit。