Start Search Contents Index Links About

A watch

Get the time. Using sin and cos functions.

2002 - Week 38 - Havard Rast Blok

This week we will make two watches; One with digital and one analog display. Thus the main tasks of this week will be to get the local time in a desired format and later use geometry to draw the hands of the analog watch.

First you need to retrieve the local time and extract the hour, minute and second information. Then it should be fairly simple to display this in a JFrame or applet if you wish. Add a small loop that updates the display, and you know have a digital watch.

Important: If you make a loop that get the time and updates the display, be aware that an endless loop might use all your CPU power. Therefore, you should implement a pause of some milliseconds in your loop. You could do this by calling the Thread.sleep() method.

Hint: java.util.GregorianCalendar


DigitalWatch.java

Now that you have the time information at hand, you could draw an analog watch. Try to let the hands of the watch move synchronously with the system time, e.g. not stepping every second or minute.

Hint: Given an angle of a hand with the 12-o'clock line, the outer position of the hand could be found using sinus and co-sinus, however, a watch hand starts (and ends) its loop at the 12-o'clock line while the sinus function starts its period at the 3-o'clock line.


AnalogWatch.java



site: Håvard Rast Blok
mail:
updated: 27 July 2007