アニメーション用に JPanel を拡張するクラスで使用される Timer があり、ActionListener がそれをリッスンして actionPerformed を実行し、再描画を行い、必要に応じてタイマーを停止します。しかし、タイマーを開始するメソッド animatePanel は、タイマーの実行中も実行を続けます。これは私が望んでいないことです。タイマーが止まるまで待って戻りたい。
Timer は、クラスのコンストラクターで次のように初期化されます。
timer = new Timer(5, taskPerformer);
そして、これがそれがすることです。animatePanel() と呼ばれるものがあります。
private ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
...
if (some conditions){
...
timer.stop();
...
return;
}
...
}
};
private void animatePanel() {
...
timer.start();
System.out.println("Timer stopped."); //always executes before the timer has stopped :(
//then returns and lets the rest of my program run while the timer is still going, which is BAD
}
タイマーは正常に動作しますが、animatePanel() の戻りが早すぎて残りのプログラムが実行され、問題が発生する場合があります。