This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to set a timer for timed mode?  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how I will start it; I tried doing some research and I don't think Swing timers fit.

But here's really what I'm after:

For example, the player chooses Timed mode, then we'll suppose there'll be 1 minute for the player to play the game, and once it's over, then scores will be recorded. (When paused, the timer will also pause).

I'm not exactly asking for codes, I'm a beginner and would appreciate a few suggestions like what stuff should I learn about, something like that to begin this task.
 
Saloon Keeper
Posts: 9219
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming your game is event-driven, and doesn't use a main program loop (usually the case with real-time games).

When your player starts their turn, you can pass a TimerTask to java.util.Timer that executes once or twice per second (use scheduleAtFixedRate()). Each time it executes, you update the GUI (just call repaint() and let the GUI decide how many seconds are left). When the TimerTask determines that time has run out, it tells your model that the turn should end, and then it performs cancel() on itself.
 
Marshal
Posts: 60095
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: Complicated and confusing post to follow.

There are two sorts of Timer, which work slightly differently. You can read about the other one (Swing) here. Then you can decide which sort is better for your current requirements. If the Swing Timer works in the Event Dispatch Thread (EDT), you may find it blocks your entire GUI, in which case you will want the other Timer. In Timer, it suggests this link will help; I haven't read it.
Remember you need to enter the EDT so your Timer or TimerTask would have to enter it: start reading here.

Actually, there is a third kind of Timer (Management) in the API.
 
Stephan van Hulst
Saloon Keeper
Posts: 9219
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Remember you need to enter the EDT so your Timer or TimerTask would have to enter it: start reading here.



Not necessarily. They can simply update the model, and call repaint() on the GUI. repaint() does not have to be on the EDT.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!