/* A class with one thread, implementing a Digital Clock */ import java.awt.Graphics; import java.awt.Font; import java.util.Date; public class DigitalClock extends java.applet.Applet implements Runnable { Font theFont = new Font("TimesRoman",Font.BOLD,24); Date theDate; Thread runner; // override the applet start method public void start() { // construct an instance of a thread and use the run method // from "this" applet runner = new Thread(this); runner.start(); // call the thread start method } // override the applet stop method public void stop() { runner = null; } // provide a method run to be the body of the thread // this method required by the runnable interface public void run() { Thread thisThread = Thread.currentThread(); while (runner == thisThread) { theDate = new Date(); // this method will call applet method update, // which will call paint below repaint(); // current thread will sleep for 1 second try { thisThread.sleep(1000); } catch (InterruptedException e) { } } } // provide the applet paint method public void paint(Graphics g) { g.setFont(theFont); g.drawString(theDate.toString(),10,50); } }