您好,我正在使用 Java Swing 计时器让蜡烛燃烧。一切都很好,除了蜡烛几乎烧到底部的末端部分。当大小更小时计时器应该停止然后让我们说 15.. 现在我做了一些代码,似乎我所做的 if/else 语句被完全忽略了.. 有人能告诉我我做错了什么吗?
下面你可以看到我的代码:(ps getCandleSize 方法现在将长度从 500 带到 10 > 0,然后我得到负值)
package h04SelfBurnCandle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import h02aKaars.DrawCandle;
public class CandlePanel extends JPanel implements ActionListener {
private boolean aan = true;
private int lengte = 500;
private int x;
private int y;
private int speed = 10;
private final int WACHTTIJD = 100; // wachttijd voor de timer
public CandlePanel() {
javax.swing.Timer autoburn = new javax.swing.Timer(WACHTTIJD,this);
autoburn.start();
}
DrawCandle candle = new DrawCandle();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
x = 10;
y = getHeight() - (lengte + 10);
for(int i=0; i < 5; i++) {
if(lengte > 15){
candle.drawCandle(g, x, y, lengte, aan);
}
else {
candle.drawCandleBurned(g, x, y, lengte);
}
x = x + 70;
System.out.println(lengte);
}
}
public int getCandleSize() {
do {
lengte -= speed;
}
while(lengte > 15);
return lengte;
}
@Override
public void actionPerformed(ActionEvent e) {
getCandleSize();
//System.out.println(lengte);
repaint();
}
}