// Multithreaded time display applet. // by Andrew W. Donoho // 97/03/08 /* Replacement for Lemay's DigitalThreads class. This design uses one object per thread (1 Applet and 1 Thread). Basically, TickTock creates the Tick thread to tell it when the time has advanced one second. Tick does this by calling TickTock's Tock method. TickTock then tells itself to repaint the frame. It also demonstrates a saner method of initializing the additional thread using the Applet's init method. */ import java.applet.Applet; import java.awt.Graphics; import java.awt.Font; import java.util.Date; public class TickTock extends Applet { Font f = new Font("TimesRoman", Font.BOLD, 24); Tick t; Date d; public void init() { t = new Tick(this); t.start(); } public void Tock(Date d) { this.d = d; repaint(); } public void paint(Graphics g) { g.setFont(f); g.drawString(d.toString(), 10, 50); } public void destroy() { t.stop(); } } public class Tick extends Thread { TickTock tt; public Tick(TickTock tt) { this.tt = tt; } public void run() { while (true) { tt.Tock(new Date()); try {sleep(1000);} catch (InterruptedException e) {} } } }