Solved

How do I make the time tick backwards?

Posted on 2001-06-25
6
226 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

696 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