Solved

How do I make the time tick backwards?

Posted on 2001-06-25
6
225 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 55
going to wrong jsp page 2 33
ejb entity bean example issue 2 16
java example issue 3 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 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