Solved

How do I make the time tick backwards?

Posted on 2001-06-25
6
224 Views
Last Modified: 2010-03-31
Hi guys,
I'm trying to create an applet that will display some kind of timer. Basically I need it to decrement the time every second, so it looks like a countdown of some sort. I have some code for the time to tick forward... but I have no idea how to make it tick backwards. Any help would be appreciated.

Here is what I have so far,

import java.awt.*;
import java.util.*;
import java.applet.*;

public class example extends Applet implements Runnable
{
    Date d;
    Thread runner;  
   
     public void init()
     {          
          setLayout(null);
          setSize(420,250);          
     }
     
     public void start()
     {
         if(runner == null)
         {
             runner = new Thread(this);
             runner.start();
         }
     }
     
     public void stop()
     {
         if(runner != null)
         {
             runner.stop();
             runner = null;
         }
     }
     
     public void run()
     {        
         while(true)
         {
             d = new Date();      
             repaint();
             try
             {
                 runner.sleep(1000);
             }
             catch(InterruptedException e)
             {
                 System.out.println(e);
             }
         }
     }
     
     public void paint(Graphics g)
     {
         g.drawString(d.toString() , 5, 100);        
     }
}
0
Comment
Question by:tixnmix
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6223700
Replace your run() method with the following:


    public void run()
    {
        Date start = new Date();        
        while(true)
        {
            d.setTime(new Date().getTime() - start.getTime());      
            repaint();
            try
            {
                runner.sleep(1000);
            }
            catch(InterruptedException e)
            {
                System.out.println(e);
            }
        }

Let me know if u have any problems :)

0
 

Author Comment

by:tixnmix
ID: 6223815
Thanks for the reply, but I cant seem to get it to work though. =(
nothing is showing up on the applet...
0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 6223877
woops, two changes required:

1. Initialise d at the start (line 7(ish))

d = new Date();

2. Change the setTime call to:

d.setTime(start.getTime() - (new Date().getTime() - start.getTime()));

See how that goes.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:tixnmix
ID: 6223907
Ah !! Thanks a lot, that did it. =)
0
 
LVL 92

Expert Comment

by:objects
ID: 6223926
No worries.
Thanks for the points :)
0
 
LVL 4

Expert Comment

by:kylar
ID: 6224632
That's a good hack. I think that there should be some kind of reverse flag that allows you to create a Timer (for example) and have it tick in reverse. Objects, you've given me some food for thought. (And it's too early on monday for that)

Kylar

"Time keeps on tickin', tickin', tickin', into the future..."
    -Steve Miller, 'Fly Like an Eagle'
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 44
topping3 challenge 14 80
jsp login check 12 31
eclipse console opening separately 2 21
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question