简体   繁体   中英

Nothing shows up to the screen when I call the repaint() method

This is probably a common question, but likely unique to every situation.

Here is where I call .repaint() in code:

timer = new Timer(5, new ActionListener() {
        double t = 0;
        public void actionPerformed(ActionEvent e) {

            ArrayList<Particle> fireworks = manager.getFireworks(t/1000);
            showFireworks(fireworks,t/1000);
            t = t + timer.getDelay();
            for (Particle projectile : fireworks) {
                canvas = new FireworksDisplay(projectile);
                canvas.repaint();
                add(canvas);
            }
        }
    });

It is creating an ArrayList of firework particles (which works correctly, since I tested printing the (x,y) postions in the console window and it went fine). I want my program to paint a little particles on the screen for every member of my ArrayList list

Here is my DrawPanel

    private class FireworksDisplay extends JPanel {
    private Color colour;
    private int xPos;
    private int yPos;
    private int size;
    public FireworksDisplay(Particle particle) {
        super();
        String string = particle.getColour();
        string = string.toLowerCase();
        switch(string) {
        case "blue":    this.colour = Color.blue;
                        break;
        case "red":     this.colour = Color.red;
                        break;
        case "green":   this.colour = Color.green;
                        break;
        case "orange":  this.colour = Color.orange;
                        break;
        case "cyan":    this.colour = Color.cyan;
                        break;
        case "magenta": this.colour = Color.magenta;
                        break;
        case "yellow":  this.colour = Color.yellow;
                        break;
        case "pink":    this.colour = Color.pink;
                        break;
        default:        this.colour = Color.white;
                        break;
        }
        double[] positions = particle.getPosition();
        this.xPos = (int) (getWidth()*positions[0]*(50) + tubeImage.getSize().width/2);
        this.yPos = (int) (getHeight()*positions[1]*(50) + tubeImage.getSize().height - 100);

        this.size = particle.getRenderSize();

    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(colour);
        g.fillOval(xPos, yPos, size, size);
    }
}

For some reason it's not painting to the screen even though all the other times I painted were successful.

Is there a problem in my posted code, or you you think there's something wrong elsewhere?

Remember that you need to call Timer 's start() method after instantiation.

Also, you're adding a new JPanel every time your Timer fires. You may want to rewrite your code so that you simply paint new Particle s instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM