I'm not that good with Java Swing and I'm trying to use a timer start the game with a delay in 3 seconds
But at the same time I want to show a dialog (also the game has to wait 3 seconds so the focus needs to be on the dialog)
So my dialog is as follow: got this sample code
So in my gameplay panel I do this:
public class GamePlayPanel extends JPanel implements ActionListener {
// attributes
private JOptionCountDownTimer countDownDialog;
public GamePlayPanel(MainWindow mainWindow) {
// initialization attributes
initLayoutPanel();
this.timer = new Timer(DELAY, this);
// Added a delay of 3 seconds so you can prepare to for the game
this.timer.setInitialDelay(3000);
resetTime();
}
public void startGame() {
this.gamePanel.requestFocus();
this.countDownDialog.startCountDown();
startTimer(); // this is my game timer to record the game time
}
public void restartGame() {
this.countDownDialog.resetCountDown();
startTimer();
this.gamePanel.requestFocus();
}
}
It works fine but if I restart the game the count down timer starts at 0 -> 2 seconds.
Also any better ideas on my class JOptionCountDownTimer
? I tried to make it extend a JDialog
class but I couldn't get it to work.