나는 게임 이벤트가 발생할 때 화면에 애니메이션을 보여주는 타이머가 있습니다. 내가하고 싶은 일은이 애니메이션이 끝날 때까지 주 실행을 일시 중지하는 것입니다.하지만 시도 할 때마다 타이머가 실행되는 것을 차단하는 것 같습니다. 나는 Thread.sleep()을 사용하고 lock 객체에서 wait() 및 notify()를 호출하려고했지만 동일한 결과를 보았습니다. Timer 리스너의 actionPerformed()는 호출되지 않습니다.Timer (Java Swing)를 차단하지 않고 프로그램 실행을 일시 중지하는 방법은 무엇입니까?
이 코드는 타이머 설정하십시오 EffectsPane 객체 내부
protected void showMovingEffect(int steps, Direction dir, AnimatedImageSet imgs) {
effectsPane.runAnimation(imgs, dir, steps);
waiting = true;
while (waiting) {
synchronized(animationWaitLock) {
try {
animationWaitLock.wait();
} catch (InterruptedException e) {}
}
}
}
그리고 타이머와 청취자를 :
private void runAnimation(AnimatedImageSet ais, Direction dir, int iters) {
System.out.println("run");
imgs = ais;
top = 0;
left = 0;
topStep = dir.getRowIncrement() * 10;
leftStep = dir.getColIncrement() * 10;
iterations = iters;
index = 0;
active = true;
timer.start();
}
public void actionPerformed(ActionEvent e) {
System.out.println(index);
top += topStep;
left += leftStep;
index++;
currentImage = imgs.getImage(index);
repaint();
if (index == iterations) {
active = false;
timer.stop();
synchronized(animationWaitLock) {
animationWaitLock.notify();
}
waiting = false;
}
}
의 actionPerformed의 시작에서에서 System.out.println 호출() 호출되지 않기 때문에 wait() 호출도 Timer를 일시 중지하거나 무언가를 차단하고 있습니다. showMovingEffect()에서 sleep/wait 줄을 주석 처리하면 애니메이션이 실행되지만 프로그램이 일시 중지되지 않습니다.
SLEEP을 사용하지 마십시오 !!!!! – paulsm4
나는 그렇지 않습니다. wait()를 사용하고 있습니다. – Leonide