File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Applet timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet timer" Watch "Applet timer" New topic
Author

Applet timer

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,
like displaying Textfield and label in a applet,I want to display a timer in applet.That timer should work like a stopwatch.like when i clicking a button ,first time the time should start and when i click another time it should stop.how to do this?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Is this a continuation of this work?

You'd need a thread running that periodically wakes up and increments some timing counter. The thread would start if the button is clicked, and be stopped when the button is clicked again.

Stopping the thread can be implemented by setting a boolean variable from the ActionListener, and then checking that variable in the thread.


Ping & DNS - updated with new look and Ping home screen widget
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
without thread,I have tried a program below.
.
Now its just displaying time in milliseconds when i press start button.If want to display it in hh:mm:ss,what should i do?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
If "ms" is the time in milliseconds, then

hours = ms / 1000.0 / 60.0 / 60.0

minutes = (ms - hours * 60 * 60 * 1000) / 1000.0 / 60.0

seconds = (ms - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000.0

You'll need to do some appropriate rounding using the Math.floor method, and also think about adding leading zeros if the numbers are smaller than 10. If you're feeling adventurous, you might also have a look at the java.text.SimpleDateFormat class, which makes formatting dates and times much easier.

As an aside, please post formatted code. I'm assuming that you have indented code (if not then you should get in the habit), but in the post all formatting is lost.
[ December 15, 2007: Message edited by: Ulf Dittmer ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
hi..
i cant understand the following lines

You'll need to do some appropriate rounding using the Math.floor method, and also think about adding leading zeros if the numbers are smaller than 10. If you're feeling adventurous, you might also have a look at the java.text.SimpleDateFormat class, which makes formatting dates and times much easier.
can you explain in brief
from next time onwards i'l see that my code gets intended....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
You'll need to do some appropriate rounding using the Math.floor method,

Doing divisions gives you a result like "10.42324" or something, when you really need an integer that is rounded down like "10". That's what Math.floor does.
and also think about adding leading zeros if the numbers are smaller than 10.

Say, if the minutes are "9" you still want to show "09" in the output. So you need to check for values smaller than 10 and add an additional "0" in front of them.
If you're feeling adventurous, you might also have a look at the java.text.SimpleDateFormat class

Let's just ignore that. You'll learn more by using the approach I outlined above.
[ December 17, 2007: Message edited by: Ulf Dittmer ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,I've tried the following program.But in applet browser ,when start is clicked i got the output0.0.
.
what is my mistake?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
I haven't looked at the details, but two things jump out.

These 3 should be "int", not "double". You need to use Math.floor like I suggested, and then cast to int. Otherwise the math doesn't work out.


You're aware that this will only display the seconds, since everything else will be erased?

I would also slow the timer way down. What good is having it fire every millisecond if the display only cares about seconds? That takes up too many CPU cycles accomplishing nothing.
[ December 17, 2007: Message edited by: Ulf Dittmer ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518

[ December 17, 2007: Message edited by: preethi Ayyappan ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Please re-read my previous post. You need to use Math.floor and a cast to int when calculating hours/minutes/seconds, not during display. Plus, you have not addressed the second part of what I wrote.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518

I would also slow the timer way down. What good is having it fire every millisecond if the display only cares about seconds? That takes up too many CPU cycles accomplishing nothing.

I dont understand this.could you please explain?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
You have set the timer to fire once every millisecond, while the display code (once you correct the problem I pointed out) only changes its display every second. So the code will run many times without changing anything on the screen. To me, that makes no sense.

By the way, in my previous post, by "second part" I meant the multiple calls to "setText". There should only be a single call that displays hours, minutes and seconds in one go.
[ December 18, 2007: Message edited by: Ulf Dittmer ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
hello ulf,
Can you show me some sample code regarding this 'setText' which can call more than one variable......
i've tried this...

why this is not working?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
why this is not working?

Because it's not doing what I've said repeatedly above - cast the result to an int:
The way to display more than one value is to use string concatenation:

[ December 19, 2007: Message edited by: Ulf Dittmer ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi, Thank you for your help.I've tried that code.But Now the time is displayed as 0:0:0 ,if i click the start button.whether i have to change the time passed?here is the code what i tried.



[ December 19, 2007: Message edited by: preethi Ayyappan ]
[ December 19, 2007: Message edited by: preethi Ayyappan ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Now that the timer runs every 100ms you should increase timePassed by 100 on every iteration.

As an aside, a more accurate way to determine time passed would be to store System.currentTimeMillis() when the timer starts, and then to get it again on each iteration in the actionPerformed method, and to subtract the start time. By counting in your own code you're relying on the timer running exactly every 100ms, which is unlikely to be the case.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
sorry for the inconvinience......can you explain me in brief.

As an aside, a more accurate way to determine time passed would be to store System.currentTimeMillis() when the timer starts, and then to get it again on each iteration in the actionPerformed method, and to subtract the start time. By counting in your own code you're relying on the timer running exactly every 100ms, which is unlikely to be the case.

I've tried upto what i understood.

It shows the error:
t1.getTime(System.currentTimeMillis());
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cannot invoke getTime(long) on the primitive type long

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Cannot invoke getTime(long) on the primitive type long

The error message is quite to the point - long does not contain a method called getTime. Where did you get the idea that it does?

You need something like
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks a lot.Its working now.
[ December 20, 2007: Message edited by: preethi Ayyappan ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Applet timer
 
Similar Threads
displaying time
Re:Integrating applet timer
Time
Repainting jPanel in Applet
KeyListener - Please Help