import javax.swing.*; import java.awt.*; import java.awt.event.*; public class StopWatchView extends JFrame { private long currentTime; // the current time private long displayTime; // the display time private Timer timer; // the timer goes off every 1/100 seconds private State state; // the state keeps track of whether the stopwatch // is running public JTextArea display; /** Creates new Model */ public StopWatchView() throws Exception{ super ("StopWatch"); display = new JTextArea("00:00:0", 1, 5); Container c = getContentPane(); c.setLayout (new FlowLayout () ); c.add(display); addWindowListener ( new WindowAdapter() { public void windowClosing ( WindowEvent e ) { System.exit ( 0 ); } }); setSize( 120, 150); currentTime = 0; displayTime = 0; timer = new Timer(67, new TimerHandler ()); state = new Stopped(); timer.setRepeats(true); timer.start(); show(); EventRemoteGluer startStopEvtRemoteGluer = new EventRemoteGluer("StartStop"); startStopEvtRemoteGluer.glue(new startStopEventHandler()); EventRemoteGluer lapEvtRemoteGluer = new EventRemoteGluer("Lap"); lapEvtRemoteGluer.glue(new lapEventHandler()); } // getters and setters for the time variables public long getCurrentTime () {return currentTime;} public void setCurrentTime (long timeArg) {currentTime = timeArg;} public long getDisplayTime () {return displayTime;} public void setDisplayTime (long timeArg) { displayTime = timeArg; String minutes = Long.toString((displayTime/600)%60); if ((displayTime/600)%60 < 10) minutes = "0"+ minutes; String seconds = Long.toString((displayTime/10%60)); if ((displayTime/10%60) < 10) seconds = "0" + seconds; String tenths = Long.toString(displayTime%10); display.setText( minutes + ":" + seconds + ":" + tenths); } class TimerHandler implements ActionListener { public void actionPerformed (ActionEvent e) { state.updateTime(); } } public interface State { public void updateTime(); public void startStopPushed(); public void lapPushed(); } public class Stopped implements State { public void updateTime () { } public void startStopPushed() { state = new Started(); } public void lapPushed() { setCurrentTime(0); setDisplayTime(0); } } public class LapAndStopped implements State { public void updateTime () { } public void startStopPushed() { state = new Started(); } public void lapPushed() { setDisplayTime(getCurrentTime()); state = new Stopped(); } } public class Started implements State { public void updateTime () { setCurrentTime(getCurrentTime()+1); setDisplayTime(getCurrentTime()); } public void startStopPushed() { state = new Stopped(); } public void lapPushed() { state = new LapAndStarted(); } } public class LapAndStarted implements State { public void updateTime () { setCurrentTime(getCurrentTime()+1); } public void startStopPushed() { state = new LapAndStopped(); } public void lapPushed() { setDisplayTime(getCurrentTime()); state = new Started(); } } public class startStopEventHandler implements EventHandler { public void handle(Event event) { state.startStopPushed(); } } public class lapEventHandler implements EventHandler { public void handle(Event event) { state.lapPushed(); } } public static void main (String args []) throws Exception{ StopWatchView swv = new StopWatchView(); } }