2

Possible Duplicate:
Stop timer with conditional only works first time?

I'm very confused as to how to make a timer using the swing and not util timer.

I am making a game where users have to answer questions in a 30 second time limit. I have a PlayFrame, where the time is shown, and a method inside PlayFrame called startTimer which contains all the timer stuff.

public static void startTimer() {

    int elapsedSeconds = 0;
    javax.swing.Timer myTimer = new javax.swing.Timer(1000, new MyTimerActionListener());
    elapsedSeconds++;

    if (elapsedSeconds == 30) {
        myTimer.stop();
        timerLabel.setText("0");
        wrong();
    } else {
        String text = String.format("f", 30 - elapsedSeconds);
        timerLabel.setText(text);
    }

    if (myTimer != null && myTimer.isRunning()) {
        myTimer.stop();
        myTimer = null;
        timerLabel.setText("0");
    } else {
        elapsedSeconds = 0;
        myTimer = new javax.swing.Timer(1000, new MyTimerActionListener());
        myTimer.start();
        String text = String.format("t", 30);
        timerLabel.setText(text);
    }
}

What I want this method to do is have a timer that counts down from 30 until the question is answered correctly. If the answer is answered incorrectly I want the timer to stop.

For an answer perhaps some psuedocode (or real code) to move me in the right direction. And this is for personal use, not homework or anything.

Please remember that this is a part of my whole code and it needs to work with other parts of it. I'll give more information upon request, thanks!

EDIT: New startTimer() method NOTE all System.out.print is for testing only:

  public static void startTimer() {
    class TimerListener implements ActionListener {
        Timer timer = new Timer(1000, new TimerListener());
        int elapsedSeconds = 30;
        String seconds = Integer.toString(elapsedSeconds);

        @Override
        public void actionPerformed(ActionEvent evt) {
            timer.start();
            if (elapsedSeconds == 0) {
                //System.out.print("here");
                timer.stop();
                PlayFrame.wrong();
            }
            else{
                //System.out.print("hersfde");
                elapsedSeconds--;
            PlayFrame.timerLabel.setText(seconds);
            }
            //System.out.println(elapsedSeconds);
        }
    }
    //System.out.print("l");
}

Doesn't do anything and not sure why.

Community
  • 1
  • 1
CodeAddict
  • 194
  • 2
  • 5
  • 19
  • You'll need to create a separate thread for the timer display. I've got code to do this but it's at home and I'm at work. :/ I'll post it later if I remember and/or this question hasn't been answered by then. – Mike G Jan 18 '13 at 19:04
  • 3
    I showed sample code for a count-down Swing Timer in your [last question](http://stackoverflow.com/a/14391322/522444). Have you checked it out? Run it? Questions? – Hovercraft Full Of Eels Jan 18 '13 at 19:05
  • @HovercraftFullOfEels, yes I saw it, it was very helpful. However, I'm not sure how to integrate that into my project. Basically I have a home for the timer (jLabel) so I wouldnt need the extra GUI stuff you included. However copying and paste it works beautifully. I want to be able to have the timer enclosed in the method shown above to keep everything localized. – CodeAddict Jan 18 '13 at 19:12
  • @trashgod, that was my previous question, this is more specific to the method instead of the entire project – CodeAddict Jan 18 '13 at 19:13
  • I can't see why you need a timer, the way you use it. Why don't you do all work in the listener? – Mordechai Jan 18 '13 at 19:13
  • @MouseEvent, could you explain how I would do the work in the listener? I would need to start and stop it on command, as well as make new instances of it. I was told to use the timer to do this. – CodeAddict Jan 18 '13 at 19:14

1 Answers1

8

This is how I would make a countdown timer:

Timer timer = new Timer(1000, new TimerListener());

class TimerListener implements ActionListener{
    int elapsedSeconds = 30;

    public void actionPerformed(ActionEvent evt){
        elapsedSeconds--;
        timerLabel.setText(elapsedSeconds)
        if(elapsedSeconds <= 0){
            timer.stop();
            wrong()
            // fill'er up here...
        }
    }

}
Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • But I can't have that in the method right? Would I make a class and put this in there? +1 for keeping it simple and clear – CodeAddict Jan 18 '13 at 19:26
  • Nitpick: `elapsedSeconds` should be `remainingSeconds` – Mel Nicholson Jan 18 '13 at 20:04
  • @CodeAddict Yes, this code lives in its own class. (class keyword is even in the code sample) – Mel Nicholson Jan 18 '13 at 20:06
  • So I have a class called CountDown. would I put the code above after the line `public class CountDown{` ? and then how would I call this class in my main frame (PlayFrame) ? – CodeAddict Jan 18 '13 at 20:15
  • @CodeAddict No, `TimerListener` is an inner class. You should put the first line, in your outer class, and call `timer.start()` when required. – Mordechai Jan 25 '13 at 16:01