Solved

How do I make the time tick backwards?

Posted on 2001-06-25
6
222 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 70
topping1 challenge 7 53
word0 challenge 4 54
Where to store the queries for modification of table 4 29
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now