Question: Implement to the java program down below. Set the title to Stopwatch and center the stage on the screen. Place all classes into one file.

Implement to the java program down below. Set the title to Stopwatch and center the stage on the screen. Place all classes into one file. Output should look similar to the book.

Name the program: GUIStopwatchXX.java, where XX are your initials.

The old code is:

package Chapter_16;

import javafx.scene.layout.StackPane;

import javafx.util.Duration;

import javafx.animation.Timeline;

import javafx.animation.KeyFrame;

import javafx.scene.text.Text;

import javafx.scene.text.Font;

import javafx.geometry.Insets;

import javafx.application.Application;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.control.Button;

import javafx.geometry.Pos;

public class CountUpStopwatch extends Application

{

protected Button startBtn = new Button("Start");

protected Button clearBtn = new Button("Clear");

@Override // Override the start method

public void start(Stage primaryStage)

{

// Create a hbox for buttons

HBox paneForButtons = new HBox(5);

paneForButtons.setAlignment(Pos.CENTER);

paneForButtons.getChildren().addAll(startBtn,

clearBtn);

// Create a Stopwatch

StopWatch stopWatch = new StopWatch();

// Create a border pane

BorderPane pane = new BorderPane();

pane.setBottom(paneForButtons);

pane.setCenter(stopWatch);

// Create and register handlers

startBtn.setOnAction(e ->

{

if (startBtn.getText().equals("Start")

|| startBtn.getText().equals("Resume"))

{

stopWatch.play();

startBtn.setText("Pause");

} else

{

stopWatch.pause();

startBtn.setText("Resume");

}

});

clearBtn.setOnAction(e ->

{

stopWatch.clear();

startBtn.setText("Start");

});

// Create a scene and place it in the stage

Scene scene = new Scene(pane);

// Set the stage title

primaryStage.setTitle("CountUpStopwatch");

// Place the scene in the stage

primaryStage.setScene(scene);

primaryStage.show(); // Display the stage

}

private static class StopWatch extends StackPane

{

// Data field

private int hour;

private int minute;

private int second;

private Text text = new Text();

// animate stopwatch

private Timeline timelineAnimation;

/**

* Construct a default StopWatch

*/

public StopWatch()

{

setPadding(new Insets(5, 15, 5, 15));

clear();

text.setFont(Font.font(30));

getChildren().add(text);

timelineAnimation = new Timeline(

new KeyFrame(Duration.millis(1000), e

> run()));

timelineAnimation.setCycleCount(Timeline.INDEF

INITE);

}

/**

* Play timelineAnimation

*/

public void play()

{

timelineAnimation.play();

}

/**

* Pause timelineAnimation

*/

public void pause()

{

timelineAnimation.pause();

}

/**

* Animate stopwatch

*/

protected void run()

{

if (minute == 59)

{

hour = hour + 1;

}

if (second == 59)

{

minute = minute + 1;

}

second = second < 59 ? second + 1 : 0;

text.setText(getTime());

}

/**

* Reset stopwatch

*/

public void clear()

{

hour = 0;

minute = 0;

second = 0;

text.setText(getTime());

}

/**

* Return time as a string

*/

private String getTime()

{

return pad(hour) + ":" + pad(minute) + ":" +

pad(second);

}

/**

* Return n as padded string

*/

private String pad(int n)

{

return n < 10 ? "0" + n : n + "";

}

}

public static void main(String[] args)

{

Application.launch(args);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!