/* * StopWatch.java * Model contains the data and the state for the stopwatch * Created on September 11, 2001, 8:29 AM */ /** * * @author Stuart Hansen * @version */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class StopWatch 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 JButton startStop; public JButton lap; public JTextArea display; /** Creates new Model */ public StopWatch() { super ("StopWatch"); startStop = new JButton("Start/Stop"); startStop.addActionListener(new startStopController()); lap = new JButton("Lap"); lap.addActionListener(new lapController()); display = new JTextArea("00:00:0", 1, 5); Container c = getContentPane(); c.setLayout (new FlowLayout () ); c.add(startStop); c.add(lap); 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(); } // 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 startStopController extends AbstractAction { public void actionPerformed (ActionEvent e) { state.startStopPushed(); } } public class lapController extends AbstractAction { public void actionPerformed (ActionEvent e) { state.lapPushed(); } } public static void main (String args []) { StopWatch stopwatch = new StopWatch(); } }