// Clock.java // This applet implements an animated digital clock, which // is gotten from the system class Date. The applet runs // in its own thread and is repainted every second. The color // of the display is rotated through seven colors. import java.applet.Applet; import java.awt.*; import java.util.Date; public class Clock extends Applet implements Runnable { Font clockFont = new Font ("TimesRoman", Font.BOLD, 24); Date currentDate; Thread clockThread; Color [] colorList = {Color.red, Color.green, Color.blue, Color.magenta, Color.cyan, Color.pink, Color.orange}; int colorIndex = 0; // Implement the Runnable start method public void start() { clockThread = new Thread(this); clockThread.start(); } // Implement the Runnable stop method public void stop() { clockThread = null; } // Implement the run method from Runnable public void run() { Thread thisThread = Thread.currentThread(); while (clockThread == thisThread) { currentDate = new Date(); repaint(); try { Thread.sleep(1000); } catch(InterruptedException e) {} } //** end of while (clockThread == ... } //** end of run method // Override the paint method public void paint(Graphics grafObj) { if (colorIndex == colorList.length) colorIndex = 0; grafObj.setFont(clockFont); grafObj.setColor(colorList[colorIndex++]); grafObj.drawString(currentDate.toString(), 10, 50); } //** end of paint method } //** end of Clock class