简体   繁体   English

当我调用repaint()方法时,屏幕上没有任何显示

[英]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: 这是我在代码中调用.repaint()的地方:

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). 它正在创建一个烟火粒子的ArrayList(它正常工作,因为我测试了在控制台窗口中打印(x,y)位置并且一切正常)。 I want my program to paint a little particles on the screen for every member of my ArrayList list 我希望我的程序在屏幕上为ArrayList列表的每个成员绘制一些粒子

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. 请记住,您需要在实例化后调用Timerstart()方法。

Also, you're adding a new JPanel every time your Timer fires. 另外,每次Timer触发时,您都将添加一个新的JPanel。 You may want to rewrite your code so that you simply paint new Particle s instead. 您可能需要重写代码,以便仅绘制新的Particle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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