how to delay a painting in progress in java

So I’m drawing lines in a JPanel and I want to have it pause for a second every time that it draws one. I have used other things like TimeUnit and Timer that I found looking around this website but so far I just can’t get it to work. I have the JFrame all set up and ready to go and this is just the last piece. Also I was wondering if whatever method that works can be applied to other types of shapes and things instead of just lines.

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

Answer

Here is mine solution for your task

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

Leave a Reply

Your email address will not be published. Required fields are marked *