Write a JPanel stopwatch program using the already provided java files as a skeleton.
Drawable.java – this is a simple interface that contains one method. Any entities that you draw graphically must implement this interface and must be drawn via a call to the draw() method (including the digits and the colons that separate the digits).
LED.java – this is the base class for a class that will represent the digits in your stopwatch. You must extend this class in a logical way and implement the abstract method draw() so that your digits can be effectively utilized in your stopwatch. You must implement (minimally) the two classes described below. Feel free to write additional classes as you feel they are necessary. StopWatch Class: - The StopWatch class will both graphically and logically represent your stopwatch. It will represent the stopwatch graphically by displaying the digits and separators in a clear, readable way. (Note: You are NOT ALLOWED to show your stopwatch on the display as a string of any sort. All of the digits and separators you show must be objects that you create yourself and which are drawn graphically. Specifically, the digits must be instances of the Digit class, which is explained in more detail below).
It will represent the stopwatch logically by having start, stop, reset and lap buttons as shown in the A5snap.htm file. These buttons are the controls for the stopwatch, and must be implemented in your StopWatch class. Read A5snap.htm thoroughly to see the required functionality of the stopwatch. –
To implement the StopWatch class you must extend JPanel. Within your JPanel subclass you will need another JPanel to maintain 4 JButtons – one to start the watch, one to stop it, one to reset it and one for a "lap". The "lap" button is a toggle button. When pressed the first time it will show you a "frozen" snapshot of the current registered time, while still allowing the stopwatch to continue ticking. It will also record the lap time by appending it to the end of a text file (numbering the entries). When pressed again it will once again show the current elapsed time (which will continue to progress). Note that the other buttons should still work correctly when used in conjunction with "lap". For example, if the stopwatch is running, and a user presses "lap", the current elapsed time should be shown, frozen on the screen. If the user then presses "reset", the screen view should not change (it is still the lap) but the stopwatch itself should be reset to 0 (and it will immediately begin ticking upward again since "stop" has not been pressed. If the user then presses "lap" again the elapsed time since "reset" will be shown (and will continue ticking updward).
See A5snap.htm for more details on this functionality. - You will also have to graphically draw the stopwatch by drawing its underlying digits. This will be done within the paintComponent() method of the StopWatch class Finally, you must allow the stopwatch to actually "tick". This can be done in a fairly simple (if somewhat imprecise – see the note at the end of this Assignment sheet) way using the javax.swing.Timer class and an ActionListener. Look up this class in the Java API for more details. There are some other things that will be helpful to know for the StopWatch class implementation as well. See handouts for more help. Digit class: - Look at the LED class that is provided for you. This is an abstract class that gives the representation of an LED element. It consists of 7 line segments, as shown in the constructor.
LED implements Drawable yet leaves the draw() method abstract. Your Digit class will extend LED so that you can represent each of the 10 possible digits that must be shown in a stopwatch (and in many other applications). Since the segments themselves are part of the LED class, clearly the Digit class must simply store the value of the digit and implement the draw() method to output the segments corresponding to that digit. Since the digits will be changing as the stopwatch runs, you will also need some mutator and accessor methods for this class. The Digit class must also scale the size of its objects based on a parameter to the constructor. Helpful Hints: - As indicated above, look for some handouts that will give some hints about completing this assignment correctly. In particular, see handouts A5Help.java and MyPanel.java to see some hints about how to set up your program. - In order to write the timer information to a file, you will need to "handle" a possible IOException. You can do this simply by putting an exception hander around the code, and leaving the "handle" part empty. You have already seen this in Lab8.java.
Extra #1; Add a time of day clock to your stopwatch (using the same Digit class for the underlying implementation). If you do this you should include a way to toggle between the stopwatch and the time of day, and showing the time of day should not turn off the stopwatch. >
Extra #2 A stopwatch is typically a "count up" timer, used to time how long events take to occur. A "count down" timer is more useful for timing things that require a known amount of time (ex: baking cookies, most things cooked in the microwave, time for guessing in many trivia games). Add a "count down" timer to your stopwatch, where the amount of time is initialized by the user and when started the timer counts down from that value to zero. > If you do the "count down" option above, it is nice to have the timer "tell you" when it is finished. You can do this by having your program make some noise when the stopwatch reaches zero. This can be done by having your program "play()" a sound clip that you have read in. It is not hard to do but clearly you will have to figure out to do it to get the credit.