• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Timer Trouble

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have created a timer using:



However, when I call timer.start() inside the mouseReleased(...) method, it doesn't work, and "10 seconds has passed" is never outputted to the screen.

Can anyone help me?

Thanks
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you must have another 'timer' object, otherwise the compiler would give you
a 'cannot resolve symbol' error at timer.stop() ('timer' is declared after)

try changing
javax.swing.Timer timer=new javax.swing.Timer(1000,taskPerformer);
to
timer=new javax.swing.Timer(1000,taskPerformer);
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well actually it is already

timer=...

It doesn't make any sense


timer is a class variable(static) of the gui(extends JFrame)
And in the constructor I set timer=new Timer(3000,taskPerformer);

Then I apply timer.start() in the mouseReleased method
[ May 11, 2006: Message edited by: colin shuker ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you might have to post all your code.

do you have both of these in your constructor?
javax.swing.Timer timer=new javax.swing.Timer(1000,taskPerformer);
and
timer=new Timer(3000,taskPerformer);
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, outside the constructor is
javax.swing.Timer timer;

then inside is timer=...

My code is 3000 lines long, and the GUI part is confusing too.
But basically when I call timer.start() after pressing the start
button on the menu... the classes' actionPerformed method calls a method which uses timer.start(), and that works fine.

But when the mouse is realised and its the computers turn to move, the timer doesnt work again.
I could post the whole code if you want to look at it, or just perhaps the gui, but then you wouldn't be able to execute it.

Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ifnot in the constructor, where is this line?
javax.swing.Timer timer=new javax.swing.Timer(1000,taskPerformer);

it looks like your program structure is something like this



can you modify the above to reproduce your problem?
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I ran your program but minus the javax.swing.timer part at the bottom, and it works fine, but thats essentially what mine is. And mine works like that.

But I need to restart the timer after the mouse has been released,
The button press (start button) is just to start the chess game, and
when you release a piece over a square... this should invoke the timer to start while the computer thinks about his move... but whats happening is that the computer keeps thinking forever.

Its only when we start the game with computer having first go, and when we click the start button that timer.start() works.


Perhaps if you could implement the MouseListener interface, so that mouseReleased(MouseEvent e) triggers timer.start(), as well as triggering it from the start button.
I can't seem to do it, I'll try it with your code though, and if you could try to, that would be great.

Thanks for your help
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so I've amended your code, and it works now.
Why doesn't my code work

Here is what I did to yours


I'll just have to take another look at my code, it should essentially be the same as this. Thanks for your help
[ May 11, 2006: Message edited by: colin shuker ]
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic