2

I'm creating a little "Game" where you have to click buttons that flash a color and if you press it while the color is still on the button, the button stays that color. So far I have the timers setting the colors on and off but I'm having trouble stopping the timer if the button is pressed. This is my code so far.

    //Changes To The Colors
    ActionListener timerListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {

            jButton1.setBackground(Color.blue);

        }
    };

    int timerDelay = 1750;
    Timer timer = new Timer(timerDelay, timerListener);

    //Changes Colors Back To Default
    ActionListener defaultTime = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {

            jButton1.setBackground(null);

        }
    };

    int waiter = 1000;
    Timer defaultState = new Timer(waiter, defaultTime);

    timer.start();
    timer.setRepeats(true);
    defaultState.start();
    defaultState.setRepeats(true);

And seen as I'm using Netbeans I added in the ActionPerformed option this is where I am getting the issues. It's not letting me call the timer.stop();

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    if(jButton1.getBackground().equals(Color.blue)){
        jButton1.setBackground(Color.blue);
        timer.stop();
        defaultState.stop();

    }

} 

Right now I'm only using one button just to get the hang of the whole swing timer thing

Jonah
  • 1,013
  • 15
  • 25
  • possible duplicate of [how to use a swing timer to start/stop animation](http://stackoverflow.com/questions/8088002/how-to-use-a-swing-timer-to-start-stop-animation) – ControlAltDel Sep 25 '14 at 16:47

1 Answers1

2

When you are running this code jButton1's color is blinking. Once blue and once default. jButton1ActionPerformed() in this method you are going to stop the timer if button's background color is blue(if(jButton1.getBackground().equals(Color.blue))). This is not true when the button is default(It's set default, because you are setting the color null). It's blinking because timer is repeating. If the button is blue, you can stop the timer without any trouble.

If the background color is null, if(jButton1.getBackground().equals(Color.blue)) condition is false. That's why your times isn't stop.

Click the button when it's background is blue. Your timer will be stopped.

If you need to stop the timer with the button color,

ActionListener timerListener = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent evt) {

        jButton1.setBackground(Color.blue);
        timer.stop();
        defaultState.stop();
    }
};

Hope this helps.

codebot
  • 2,540
  • 3
  • 38
  • 89
  • I'll give it a try and let you know. :) – Jonah Sep 25 '14 at 17:35
  • I might be misunderstanding your explanation, but I want the button's color to blink on and off unless you click the button when the buttons color is blue other wise it keeps on blinking until you do. – Jonah Sep 25 '14 at 17:40
  • According to your code this is what happening. Button is blue and when you press the button, the timer will be stopped – codebot Sep 25 '14 at 17:43
  • mine is throwing an error when ever I press the button though. It's not letting me call timer.stop(); – Jonah Sep 25 '14 at 17:46
  • Wow. I figured out the problem. Real amature mistake on my part. Thanks for your time, it is sincerely appreciated. :D – Jonah Sep 25 '14 at 19:11