?
Solved

How do I make the time tick backwards?

Posted on 2001-06-25
6
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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