Jump to content
  • Advertisement
Sign in to follow this  
Code Sage

Creating a Timer

This topic is 1234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am attempting to create my own custom textBox that users will be able to type there name into. I would use JTextField, but using that would require much more work than just creating my own. In my own opinion anyway... And as we all know every text box has that blinking caret. Basically the blinking caret is just a sprite, but I only want the sprite to be displayed every half a second, which would be 500 milliseconds. My question is, is it ok to use a simple Timer object like so to create this effect?

timer.schedule(new TimerTask() {
     public void run() {
          g.drawImage(caret, x, y, width, height, null);
     }, 0, 500);

The problem with that is that I am having to create this timer within my render method. So wouldn't that schedule a gillion timers every time the render method is called?

 

Another way I have seen timers done, which I am not sure exactly how it's done but it's using System.currentTimeMillis(); I guess they just check the how many millseconds have passed by and than add to that time to keep the check going?

 

So what would be the proper way of doing this? Thanks for any help!

Share this post


Link to post
Share on other sites
Advertisement

I would just use System.currentTimeMillis(). In your case a full cycle is 1000 ms. 500 ms visible and then 500 ms not visible.

 

So first you might want to make the clock run in range 0 to 1000 ms. This can be achieved by using modulus, for example: System.currentTimeMillis() % 1000.

 

Lastly, you'll need to check wether time is < 500 or more.

 

 

Example:

final int BLINK_TIME_MS = 500; // time visible, total cycle time 2*BLINK_TIME_MS 
...
boolean isVisible = System.currentTimeMillis() % (2*BLINK_TIME_MS) < BLINK_TIME_MS;
 
if (isVisible)
{
  g.drawImage(caret, x, y, width, height, null);
}

Share this post


Link to post
Share on other sites

That works like a charm man! Thanks that really helped.

 

Interesting how the modulus thing works. I am not exactly sure, but that is a really helpful tip.

Edited by Code Sage

Share this post


Link to post
Share on other sites

Was just going to warn a bit against the use of TimerTask , one of the main problems with it is that if one of the tasks throws an exception it can kill all other tasks that are added as well. Using a ScheduledThreadExecutor (http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newScheduledThreadPool%28int%29)  is another good way of handling this , but depending on what you're doing it might be overkill for just blinking  a control so rxa's solution is probably easier.

Share this post


Link to post
Share on other sites

 

I would just use System.currentTimeMillis(). In your case a full cycle is 1000 ms. 500 ms visible and then 500 ms not visible.

 

So first you might want to make the clock run in range 0 to 1000 ms. This can be achieved by using modulus, for example: System.currentTimeMillis() % 1000.

 

Lastly, you'll need to check wether time is < 500 or more.

 

 

Example:

final int BLINK_TIME_MS = 500; // time visible, total cycle time 2*BLINK_TIME_MS 
...
boolean isVisible = System.currentTimeMillis() % (2*BLINK_TIME_MS) < BLINK_TIME_MS;
 
if (isVisible)
{
  g.drawImage(caret, x, y, width, height, null);
}

 

This will also allow you to set a typing flag.  If you notice, the cursor doesn't blink while you're still typing.  

Share this post


Link to post
Share on other sites


This will also allow you to set a typing flag.  If you notice, the cursor doesn't blink while you're still typing.

 

Ahh that is true, thanks for the tip I didn't actually notice.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!