$30
Assignment #4
Write a Java graphics program that displays the time of day, on an analog clock, as shown below:
Note that the clock has an hour, minute, and second hand, which in the example shown above indicates the time of day as
2:19 and 58 seconds.
Be sure your clock indicates the correct time of day, and updates, once per second, i.e. is animated. Listen carefully in
class for hints on how to perform the animation. The File menu simply contains an Exit menu item; the Show menu
depicts two choices, specifically “Second Hand On” and “Second Hand Off.” For example, to perform the animation,
consider using timers (highly suggested!), as shown on this web site, specifically:
Second Hand example that processes events using timers
Alternatively use the following to aid in animation, a one second delay is useful, since the clock only updates once per second…
public class sleeper // test driver
{
public static void main(String[] args)
{
System.out.println("Sleep for a while, ok just ten seconds...");
Delay.sleep(10);
}
}
// A simple class to delay specified number of seconds
public class Delay // No one is required to understand this part, only use it
{
public static void sleep(int numberOfSeconds)
{
if (numberOfSeconds <= 0)
return;
try
{
Thread.sleep(numberOfSeconds*1000); // convert to seconds; sleep() takes ms.
}
catch(Exception e)
{
;
}
}
}
Sample run:
% java sleeper
Sleep for a while, ok just ten seconds...
%
Note that calling Delay.sleep(1) from the PaintComponent() method disrupts normal event processing,
but is a bit easier to code and understand.