java drawing on JPanel with timer/delay

2019-07-16 11:57发布

问题:

I'm trying to make a small Mosaic programm, that draws squares randomly all over the JPanel. I want to make it so, that it draws every 0,2 sec 1 square (not all at once), but so far i can make only it to draw all at once with a while loop. I have tried with ActionListener and Timer, but i found out, that i cant pass the same Graphics g to ActionListener. Then i tried with Thread.Sleep(200) but then the app froze. Now i have tried with System.currentTimeMillis(); but its same as with Threads... Searched all over the internet but didnt find anything that works.

Main:

import javax.swing.JApplet;


public class Main extends JApplet{
public void init(){
    setSize(500, 300);
    Mosaic mosaic = new Mosaic();

    setContentPane(mosaic);

}
}

App:

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class Mosaic extends JPanel{

private int width, height;
private int ruut; // square 
private int w = width / 2, h = height / 2; // middle of the app 

Random rand = new Random();
Color color;

public Mosaic(){
    this(500, 300, 10);     
}

public Mosaic(int width, int height, int ruut){
    this.width = width;
    this.height = height;
    this.ruut = ruut;       
    setBackground(Color.BLACK);     
}

public void paintComponent(Graphics g){
    super.paintComponent(g);    
    //draws random squares
    while (true) {
        moveNext(g);
        wait(200);
    }
}

//delay n millisec
public void wait(int n){
    long t0, t1;
    t0 = System.currentTimeMillis();

    do {
        t1 = System.currentTimeMillis();            
    } while ((t1 - t0) < n);
}   

//next square
public void moveNext(Graphics g){       

    int r = rand.nextInt(4);        

    switch (r) {
    case 1:
        h += ruut;
        wallTest();
        break;
    case 2:
        h -= ruut;
        wallTest();
        break;
    case 3:
        w -= ruut;
        wallTest();
        break;
    case 4:
        w -= ruut;
        wallTest();
        break;
    }

    color = new Color(0, rand.nextInt(255-50)+50, 0);
    g.setColor(color);
    g.fillRect(w, h, ruut, ruut);       
}

public void wallTest(){
    if (h > height){
        h = 0;
    }
    if (h < 0){
        h = height;
    }
    if (w > width){
        w = 0;
    }
    if (w < 0){
        w = width;
    }
}   

}

回答1:

You need to maintain an off-screen buffer, and draw tiles into that buffer under control of timer events.

Then paintComponent simply copies that buffer to the screen.

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

import javax.swing.JPanel;
import javax.swing.Timer;

public class Mosaic extends JPanel{

    private int width, height;
    private int ruut; // square 
    private int w = width / 2, h = height / 2; // middle of the app 
    private BufferedImage buffer;

    Random rand = new Random();
    Color color;

    public Mosaic(){
        this(500, 300, 10);     
    }

    public Mosaic(int width, int height, int ruut){
        this.width = width;
        this.height = height;
        this.ruut = ruut;
        this.buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        setBackground(Color.BLACK); 
        setPreferredSize(new Dimension(width, height));
        setDoubleBuffered(false);
        new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                moveNext(buffer.getGraphics());
            }
        }).start();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(buffer, 0, 0, this);
    }

    //next square
    public void moveNext(Graphics g){       

        int r = rand.nextInt(4);        

        switch (r) {
        case 1:
            h += ruut;
            wallTest();
            break;
        case 2:
            h -= ruut;
            wallTest();
            break;
        case 3:
            w -= ruut;
            wallTest();
            break;
        case 4:
            w -= ruut;
            wallTest();
            break;
        }

        color = new Color(0, rand.nextInt(255-50)+50, 0);
        g.setColor(color);
        g.fillRect(w, h, ruut, ruut);
        repaint();
    }

    public void wallTest(){
        if (h > height){
            h = 0;
        }
        if (h < 0){
            h = height;
        }
        if (w > width){
            w = 0;
        }
        if (w < 0){
            w = width;
        }
    }   
}


回答2:

Use the javax.swing.Timer class in conjunction with overriding the paintComponent method. You do not want to execute long-running tasks in the EDT since this will cause the GUI to freeze.

Example -

new javax.swing.Timer(DELAY_IN_MILLIS, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        // do stuff
        repaint();
    }
});


回答3:

I have tried with ActionListener and Timer, but i found out, that i cant pass the same Graphics g to ActionListener.

Of course not, the Graphics object is transitory. Instead either do either of:

  1. Add a rectangle to an expandable list and call repaint() (in a loop), drawing every object in the list at once.
  2. Put a BufferedImage in a JLabel and add a 'rectangle at a time' to the BufferedImage.


回答4:

You can use the timer approach as that is only way to not freeze the app. You can call repaint or some equivalent method when timer fires and then in the paint method draw as per requirements.