Class Clock
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
|
+----Clock
- public class Clock
- extends Applet
- implements Runnable
This extension to the Applet class tells you what time it is.
The time is displayed in a 24 point Bold Arial font, and is
updated every second (so the display may occasionally skip
a beat).
The applet is an example used in a presentation on Java
programming.
-
Clock()
-
-
init()
-
Main entry point for our simple Applet.
-
LeadingZero(int)
-
Utility routine to convert an int to a string, adding a
leading zero if the value is less than ten.
-
run()
-
The main thread of execution.
-
start()
-
Called each time the browser displays the page.
-
stop()
-
Called each time the browser leaves the page.
-
update(Graphics)
-
Display update method.
Clock
public Clock()
init
public void init()
- Main entry point for our simple Applet. All we do is make
ourself visible
- Overrides:
- init in class Applet
start
public void start()
- Called each time the browser displays the page.
If we haven't got a running thread, then create one,
and start the new thread running.
- Overrides:
- start in class Applet
stop
public void stop()
- Called each time the browser leaves the page.
Stop and junk any thread that may be running.
- Overrides:
- stop in class Applet
run
public void run()
- The main thread of execution.
While we have a running thread, update the display,
sleep for the specified time and go round again.
update
public void update(Graphics g)
- Display update method.
This applet overrides "update" (rather than providing a
"paint" method) because we want to clear our background
to a non-default colour. By doing this directly here,
we avoid having our background cleared for us and
so the clock display shouldn't flicker so much.
- Parameters:
- g - Graphics context we are to display the time in
- Overrides:
- update in class Component
LeadingZero
public String LeadingZero(int value)
- Utility routine to convert an int to a string, adding a
leading zero if the value is less than ten.
Note that the conversion from int to String is done by
using the "+" operator to append the int to a constant
string. This is the one time in Java where an operator
appears to be overloaded. However, it is really just
acting as a string concatenation operator - an implicit
call to the toString() method is done on the int before
the operation is performed.
- Parameters:
- value - The number to be converted
- Returns:
- String representation of the number