Java Clock Applet (Siemens Wall Clock)
Wrong time? Did you know that the new DST rules in the U.S. in 2007 require an update of the Java Runtime Environment (JRE)? See here for more details.
This Java applet contains a photo of my Siemens wall clock and draws the hands to show the current time. The real clock does not have a seconds hand. I have added one, so that you can see when the minutes hand will move. The time shown is the time of your computer's clock.
The photo is in the Portable Network Graphics (PNG) format with a transparent background. Since the background of a Java applet is gray by default, a small JavaScript determines the background colour of this web page and calls a function of the Java applet to set the same background colour there. On web pages that do not use themes, the background colour may alternatively be set as a parameter.
The source code is in this Java archive. (Choose 'Save to disk' if you browser displays the binary contents. Unpack with any Zip archiver or change the extension from "jar" to "zip" for opening.) I have added some comments. It should be quite easy to understand, once you know the basic principles behind the implementation.
One such principle is the use of double buffering, which means that first, we draw the image into memory, and then this in-memory image is drawn onto the screen. This avoids flickering that would otherwise occur when we draw the elements one after the other. In Java, you use a Graphics2D object to rasterize vector graphic elements, independent of whether you want to render onto the screen or into an in-memory image.
The other feature that is used in the applet is clipping. When drawing the elements from back to front, only the area that needs to be updated is rendered. Then, when the in-memory image is drawn onto the screen, we again clip the area that is to be updated. With clipping, the CPU utilization is slightly lower than without.
For the timing, I used a java.util.Timer object. The previous versions used a combination of long and short delays, but using the Timer class is by far easier.
Java Analog Clock in the Windows Taskbar
|
I have my Taskbar on the right-hand side of the desktop, so that it's about 120 pixels wide. This allows for an applet with a smaller version of the background image as a fine analog clock for the desktop. To load a Toolbar from the local file system, rather than from the Internet, is a bit tricky. Contact me, and I will send you files and a description. |
|
More Java Analog Clock Applets
Visit
FreewareJava.com
to find more Java clock applets!