有人可以向我解释一下为什么这段代码没有对 bufferedImage
进行动画处理,以便它移动到新的目标位置。注意:x2 是 bufferedImage
if (answer.equalsIgnoreCase("move")){
target=x2-change;
do{
if (x2>target)
x2=x2-SPEED;
try{Thread.sleep(10);
}catch(Exception exc){}
frame.repaint();
}
while(x2!=target);
}
最佳答案
please explain to me why this code is not animating the bufferedImage
我猜测代码是在事件调度线程(EDT)
上执行的。 Thread.sleep 导致 EDT
hibernate ,这意味着 GUI 无法重新绘制自身,直到动画完成。
在上一个问题中为您提供了一个有效的示例:https://stackoverflow.com/a/34552109/131872使用 Swing 计时器。
为什么使用 Thread.sleep 而不是 Swing Timer???
关于java - 缓冲图像动画代码无法正确重画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560545/