Java Clock Applet

In this lab, you will be introduced to Java applets. You will be creating a graphical clock with a minute, second, and hour hand. The clock will display the actual time and move correctly in real time. After this lab, you should feel comfortable with basic applet concepts.


  1. Download the source files ClockCanvas.java, ClockApplet.java and clock.html.
  2. Open ClockApplet.java. Notice that the ClockApplet class extends the Applet class. Any class that implements an applet must extend the Applet class. There are four important functions involved with all applets. init() is the first function called when the applet is created and should be used to perform any initialization. start() is called next and should be used to start the applet doing whatever it is supposed to do. stop() is called as soon the applet exits. destroy() is the last function called before the applet finally exits. These four functions form the basis of every java applet.
  3. Open ClockCanvas.java. The ClockCanvas class implements the entire clock functionality. ClockCanvas extends the Canvas class, which is a basic Java component used for drawing. This class initially gets the current time and then updates the time every second using a Timer. You must implement the paint function. The paint function draws the three clock hands based on the current time and also displays the time in HH:MM:SS format. Some of the paint function has already been provided. Make any changes necessary to implement the clock.
  4. The following functions will be very useful:
    g.drawLine(a, b, c, d) -- draws a line from (a,b) to (c,d).
    g.drawString(s, x, y) -- draws string s at (x,y).
    Math.cos(x) -- returns a double representing the cosine of x (in radians).
    Math.sin(x) -- returns a double representing the sine of x (in radians).
  5. This applet uses only a small subset of Java. Therefore, you should also become very familiar with the concepts of components, containers, threads, graphics, layout managers, and other related topics. You will be responsible for understanding these concepts in future labs.
  6. Other details will be provided in lab.
  7. Extra credit may be given to outstanding applets.
  8. To get credit, you must have the functionality of your applet checked off in lab and turn in your code online.
  9. The due date will be announced in lab.