2

I wrote a little code to change the colours of a few buttons to stimulate a random sequence offlashing colours. i have set a timer to do this for me but i just dont know how to stop it

Heres my code

          Timer timer = new Timer(100, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int buttons[] = new int[9];

                        for (int j = 0; j < buttons.length; j++) {

                        //  System.out.println("times");
                            Component button = frame.getContentPane().getComponent(j);
                            int Red = new Random().nextInt(255 - 1) + 1;
                            int Green = new Random().nextInt(255 - 1) + 1;
                            int Blue = new Random().nextInt(255 - 1) + 1;

                            button.setBackground(new Color(Red, Green, Blue));

                            SwingUtilities.updateComponentTreeUI(button);

                        }

                    }// }

                }); timer.start();
Claudiga
  • 427
  • 1
  • 4
  • 15
  • 1
    You'll need to call the stop() method on the Timer object. When/how do you want it to stop? – d.j.brown Oct 12 '16 at 15:57
  • 1
    create a variable.then increment it when timer tick.stop timer when variable value reach 8 – Madhawa Priyashantha Oct 12 '16 at 16:00
  • @fastsnil i have tried to create an integer variable counter and increment it in the for loop but doesn't work, can you perhaps show me what you mean with code? – Claudiga Oct 12 '16 at 16:19
  • @d.j.brown i want it to stop after a lapse of time let say after 10 seconds. i want it to stop by itself i tried to use the stop() method but it stops the timer before it even starts – Claudiga Oct 12 '16 at 16:21
  • 1
    For [example](http://stackoverflow.com/a/37063037/230513). – trashgod Oct 12 '16 at 19:12
  • 1
    Don't use `updateComponentTree(...)`!!! There is no need to use that method. All you need to do is invoke the setBackground() method and the component will repaint itself. `increment it in the for loop but doesn't work,` - no you don't increment it in the for loop. You increment it at the end of the actionPerformed() method. Then when if reaches 10 you stop the Timer. – camickr Oct 12 '16 at 20:20

1 Answers1

2

There's a few ways you can do it, here's a couple of quick approaches.

One method is by utilising System.currentTimeMillis():

private Timer timer;
private final int DELAY = 100;
private final int DURATION = 10_000;
private long startTime;

public void timerExample() {
    timer = new Timer(DELAY, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (System.currentTimeMillis() >= startTime + DURATION) {
                System.out.println("Done");
                timer.stop();
            } else {
                System.out.println("Tick");
            }
        }
    });
    startTime = System.currentTimeMillis();
    timer.start();
}

This one will stop the Timer if the current time is greater than the startTime + DURATION.

Another method is this use of a counter:

private Timer timer;
private final int DELAY = 100;
private final int DURATION = 10_000;
private final int TOTAL_TICKS = DURATION / DELAY;
private int tickCounter = 0;

public void timerExample() {
    timer = new Timer(DELAY, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (++tickCounter == TOTAL_TICKS) {
                System.out.println("Done");
                timer.stop();
            } else {
                System.out.println("Tick");
            }
        }
    });
    timer.start();
}

With DELAY as 100ms and DURATION as 10,000ms you can calculate the total number of Timer ticks required e.g. 10,000 / 100 = 100 ticks.

Each time action listener is fired, it checks if the total number of ticks are reached, and if so stops the timer.

d.j.brown
  • 1,822
  • 1
  • 12
  • 14