Question
In Class Exercise Write a new class called StopWatch. It should extend Application and implement EventHandler. The GUI should look like: To accomplish this: Use
In Class Exercise
Write a new class called StopWatch. It should extend Application and implement EventHandler.
The GUI should look like:
To accomplish this: Use VBox as the root layout.
Add a TextField as the first child of the VBox and a FlowPane as the second child.
To center the time, set the Alignment attribute of the TextField to Pos.CENTER. See the javadocs for details of how to do this. To make the time large, change its font. First: Font currentFont = tfTime.getFont(); jtfTime.setFont( Font.font(currentFont.getName(), FontWeight.BOLD, 48)); This gets the font used by default for the TextField (tfTime.getFont()). Then it creates a new font using the same font name, rendered in bold and in 48 point Finally, it sets the font of the TextField to this new font For this to work: import javafx.scene.text.*; The behavior of this program should be that, once started, it updates the time every 100 msec. To accomplish this, create and start a java.util.Timer object. Use the full name java.util.Timer, as there are several Timer classes in the Java API. Create the timer and start it in the doStart method. timer = new java.util.Timer(); timer.scheduleAtFixedRate(new MyTimerTask(), 0, 100); This will cause the run method of the class MyTimerTask to be called once every 100 msec to let you know that 100 msec has gone by. For this to work, declare MyTimerTask as an inner class a class declared inside your main class as in: public class Stopwatch extends Application implements EventHandler { // Attributes are GUI components (buttons, text fields, etc.) // are declared here. private Stage stage; // The entire window, including title bar & borders private Scene scene; // Interior of window . . . // NOTE: No public on inner classes class MyTimerTask extends java.util.TimerTask { public void run() { } } } Since this class (MyTimerTask) is inside the StopWatch class, it has access to all of StopWatchs attributes. Declare a variable (long) called currentTime as an attribute of StopWatch and have MyTimerTask increment it by 100 each time it is called. The run method above should also display the current time in the StopWatchs TextField (tfTime) each time it is called. Finally, the doStop method should call timer.cancel() where the timer is the one created inside of doStart make this timer an attriubute of the StopWatch class and this should be easy. Notes: Keep an attribute with the accumulated time for the timer (this should be of type long). Initialize accumulated time to 0L and the TextField to "00:00.0" (this means 0 minutes, 0 seconds, and 0 tenths of a second). On a Start click, clear the accumulated time (zero it out), and set the TextField to "00:00.0". Create the timer and start it. Change the name of the button to Stop. On each Timer event (in the run() method of MyTimerTask), increment the accumulated time by 100 (100 msec) and update the TextField. The TextField is formatted with: String.format("%02d:%02d.%d", minutes, seconds, tenthsOfSeconds) it is up to you to figure out how to calculate these three values from the accumulated time. On a Stop click, cancel the timer. Change the name of the button back to Start.
Led Stop Watch 00:00.0 StartStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started