/////// Timer.java /////// import java.awt.*; import java.applet.*; import java.net.URL; /** a 60-min timer that rings when the set time expires */ public class Timer extends Applet { public void init() { if ( cb == null ) cb = getCodeBase(); if ( ding == null ) ding = getAudioClip(cb, "audio/ding.au"); init2(); } protected void init2() { tmt = null; if ( cntl == null ) cntl = new TimerControls(this, Color.cyan); if ( face == null ) face = new TimerFace(Color.cyan); face.set(m,s); setLayout(new BorderLayout()); add("Center", face); add("South", cntl); } public void countDown() { tick(); // down face.set(m, s); } protected void tick() { if ( s <= 0 ) { if ( m <= 0 ) { ring(); pause(); return; } s=59; m--; } else s--; return; } protected void ring() { try // 0.4-sec interval between sounds { if ( ding != null ) { ding.play(); Thread.sleep(400); ding.play(); Thread.sleep(400); ding.play(); } else { tk.beep(); Thread.sleep(400); tk.beep(); Thread.sleep(400); tk.beep(); } } catch( InterruptedException e) { } } // returns true if timer is started else false public boolean beginEnd() { if ( tmt == null ) { tmt = new Quartz(this); tmt.tref(tmt); tmt.start(); // start Quartz thread return true; } pause(); return false; } public void pause() { if ( tmt != null ) { tmt.tref(null); tmt=null; } } public void start() { face.set(m,s); } public void set(int min, int sec) { if ( min < 0 || sec < 0 ) return; pause(); m = Math.min(min, 60); if ( m == 60 ) s = 0; else s = Math.min(sec, 59); face.set(m,s); } protected TimerControls cntl; protected TimerFace face; protected URL cb; // code base protected AudioClip ding; // for applet sound protected Toolkit tk; // for application sound protected int m=0, s=0; // min, sec, counter protected Quartz tmt = null; // timing thread }