/* * StopWatch.java * This file contains a java program to emulate a two button stopwatch * Author Stuart Hansen * Last Modified January 31, 2002 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class StopWatch extends JFrame { long currentTime; // the current time long displayTime; // the display time Timer timer; // the timer goes off every 1/10 seconds State state; // the state keeps track of whether the stopwatch // is running JButton startStop; // starts and stops the watch JButton lap; // freezes/unfreezes and resets the display JTextField display; // this is where the time is displayed // Creates new StopWatch public StopWatch() { super ("StopWatch"); // instantiate and initialize the startStop button startStop = new JButton("Start/Stop"); startStop.addActionListener(new startStopController()); // instantiate and initialize the lap button lap = new JButton("Lap"); lap.addActionListener(new lapController()); // instantiate and intialize the display display = new JTextField("00:00:0"); display.setEditable(false); // add the GUI components to the window Container c = getContentPane(); c.setLayout (new FlowLayout () ); c.add(startStop); c.add(lap); c.add(display); // set the window's properties setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize( 120, 150); // setup all the timer variables and objects currentTime = 0; displayTime = 0; timer = new Timer(100, new TimerHandler ()); state = new Stopped(); timer.setRepeats(true); timer.start(); // open the window show(); } // Get the current time public long getCurrentTime () {return currentTime;} // Set the current time public void setCurrentTime (long timeArg) {currentTime = timeArg;} // Get the displayed time (not necessarily the same as the current time) public long getDisplayTime () {return displayTime;} // setDisplayTime updates the displays text, which is a string public void setDisplayTime (long timeArg) { displayTime = timeArg; // Translate the minutes to a string String minutes = Long.toString((displayTime/600)%60); if ((displayTime/600)%60 < 10) minutes = "0"+ minutes; // Translate the seconds to a string String seconds = Long.toString((displayTime/10%60)); if ((displayTime/10%60) < 10) seconds = "0" + seconds; // Translate the tenths of seconds to a string String tenths = Long.toString(displayTime%10); // Display the updated time display.setText( minutes + ":" + seconds + ":" + tenths); } // The state classes represent the different control states the // stopwatch can be in. Button and timer events are ultimately // processed by the state. // State is the interface that all concrete states must implement public interface State { public void updateTime(); // run at a timer event public void startStopPushed(); // run when startStop is clicked public void lapPushed(); // run when lap is clicked } // The state for a stopped stopwatch public class Stopped implements State { public void updateTime () { } public void startStopPushed() { state = new Started(); } public void lapPushed() { setCurrentTime(0); setDisplayTime(0); } } // The state when the stopwatch is stopped while in lap mode public class LapAndStopped implements State { public void updateTime () { } public void startStopPushed() { state = new LapAndStarted(); } public void lapPushed() { setDisplayTime(getCurrentTime()); state = new Stopped(); } } // The state when the stopwatch is running 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(); } } // The state when the stopwatch is running but the display is frozen 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(); } } // The startStopController simply dispatches the event to the state public class startStopController extends AbstractAction { public void actionPerformed (ActionEvent e) { state.startStopPushed(); } } // The lapController simply dispatches the event to the state public class lapController extends AbstractAction { public void actionPerformed (ActionEvent e) { state.lapPushed(); } } // This class updates the time whenever the timer goes off class TimerHandler implements ActionListener { public void actionPerformed (ActionEvent e) { state.updateTime(); } } // A nice simple main program public static void main (String args []) { new StopWatch(); } }