Java 调用repaint()方法时,屏幕上没有显示任何内容
这可能是一个常见的问题,但可能是每种情况所特有的 下面是我在代码中调用Java 调用repaint()方法时,屏幕上没有显示任何内容,java,swing,arraylist,paintcomponent,repaint,Java,Swing,Arraylist,Paintcomponent,Repaint,这可能是一个常见的问题,但可能是每种情况所特有的 下面是我在代码中调用.repaint()的地方: 由于某种原因,它没有在屏幕上绘画,尽管我其他所有的绘画都是成功的 我发布的代码中是否有问题,或者您认为其他地方有问题?请记住,实例化后需要调用计时器的start()方法 此外,每次启动计时器时,您都会添加一个新的JPanel。您可能需要重写代码,以便只需绘制新的粒子s即可。使用整个JComponent/JPanel仅表示单个椭圆形是浪费。JComponent不是“精灵”。但是,您正在调用add(c
.repaint()
的地方:
由于某种原因,它没有在屏幕上绘画,尽管我其他所有的绘画都是成功的
我发布的代码中是否有问题,或者您认为其他地方有问题?请记住,实例化后需要调用
计时器的start()
方法
此外,每次启动计时器时,您都会添加一个新的JPanel。您可能需要重写代码,以便只需绘制新的粒子
s即可。使用整个JComponent/JPanel仅表示单个椭圆形是浪费。JComponent不是“精灵”。但是,您正在调用add(canvas)
-它位于哪里?也就是说,您在哪个组件上调用add
?它有一个布局经理吗?您没有设置FireworksDisplay
实例的大小和/或边界。。。。
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);
}
}
});
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);
}
}