0

i would like to ask how to make 60 sec countdown with Timer starting when first moving the mouse (and display it in the title). Then at the end i would like to display some jOptionPane.Message or something. Here's the code...

private void jPanel2MouseMoved(java.awt.event.MouseEvent evt)   {                                   
    jPanel1.setBounds(evt.getX(), evt.getY(), jPanel1.getWidth(), jPanel1.getHeight());
    int a = KEK.nextInt(jPanel2.getWidth()-15);
    int b = KEK.nextInt(jPanel2.getHeight()-15);
    if(jPanel1.getBounds().intersects(jPanel3.getBounds()) == true){
        rurin++;
        jPanel3.setBounds(a, b, jPanel3.getWidth(), jPanel3.getHeight());
        this.setTitle("Number of red dots touched: " +rurin+" ");
    } 
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    You've not shown us any timer code yourself, nor have you posted compilable/runnable code, making it difficult for us to know where you're stuck or how to answer in a way that not only helps you but also helps future visitors with similar questions. For best help, and to see what you may be doing wrong, please post your best good-faith [mcve] solution attempt with your question. This is a small runnable program that you post here as code-formatted text and that fully shows us your problem. Good luck. – Hovercraft Full Of Eels Apr 08 '17 at 14:34
  • 2
    Use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). Maybe start two Timers. The first Timer will fire every second so you can update a label with the seconds left. The second Timer will fire in 60 seconds. It will stop the first Timer and then display your JOptionPane. – camickr Apr 08 '17 at 14:46

1 Answers1

0
  1. Create an instance of the class that implements the ActionListener: Example EmilsListener list = new EmilsListener();
  2. Create an instance of the Timer class, and pass object and the time for it to restart to it:Timer tym = new Timer(1000,list);
  3. Add the listener to the timer and then start the timer: Eg
tym.addActionListener(list);
tym.start();//But before this make sure you defined the EmilsListener class which must also contain your `jPanel2MouseMoved()` method.

You can check out a complete example of the timer in the accepted answer from this stackoverflow page

Community
  • 1
  • 1
Young Emil
  • 2,220
  • 2
  • 26
  • 37
  • Sorry i don't get it (particularly EmilsListener) , could you use it in a code? – Matěj Pospíšil Apr 08 '17 at 16:09
  • @MatějPospíšil, `Sorry i don't get it` - did you read the tutorial? Did you follow the link provided in this class for an example from another question. Did you search the forum for other examples that use the `Timer` class? – camickr Apr 08 '17 at 18:44
  • Yes, I did . . . – Matěj Pospíšil Apr 08 '17 at 18:57
  • the `EmilListener` is just a class the implements `ActionListener` to be able to use the `actionPerformed()` method. In you case you calling the `jPanel2MouseMoved()` because of what you wanted to do. So you can change `EmilListener` to any name at all. I choose to use `EmilListener`, so you too can choose what you want. – Young Emil Apr 09 '17 at 11:21
  • In fact the whole idea behind this is for you to use the `Timer` class, okay. – Young Emil Apr 09 '17 at 11:27