[英]Nothing shows up to the screen when I call the repaint() method
這可能是一個常見問題,但可能對每種情況都是唯一的。
.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);
}
}
});
它正在創建一個煙火粒子的ArrayList(它正常工作,因為我測試了在控制台窗口中打印(x,y)位置並且一切正常)。 我希望我的程序在屏幕上為ArrayList列表的每個成員繪制一些粒子
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);
}
}
由於某種原因,即使我畫的所有其他時間都成功了,它也沒有畫在屏幕上。
我發布的代碼是否有問題,或者您認為其他地方有問題?
請記住,您需要在實例化后調用Timer
的start()
方法。
另外,每次Timer
觸發時,您都將添加一個新的JPanel。 您可能需要重寫代碼,以便僅繪制新的Particle
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.