Q:

(Count-up stopwatch) Write a program that simulates a stopwatch, as shown in Figure 16.45a

0

(Count-up stopwatch) Write a program that simulates a stopwatch, as shown in Figure  16.45a. When the user clicks the Start button, the button’s label is changed to Pause, as shown in Figure 16.45b. When the user clicks the Pause button, the button’s label is changed to Resume, as shown in Figure 16.45c. The Clear button resets the count to 0 and resets the button’s label to Start.

FIGURE 16.45 (a–c) The program counts up the time.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (Count-up stopWatch) Write a program that simulates a stopWatch, as shown in   *
* Figure 16.45a. When the user clicks the Start button, the button’s label is    *
* changed to Pause, as shown in Figure 16.45b. When the user clicks the Pause    *
* button, the button’s label is changed to Resume, as shown in Figure 16.45c.    *
* The Clear button resets the count to 0 and resets the button’s label to Start. *
*********************************************************************************/
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 Exercise_16_20 extends Application {
	protected Button btStart = new Button("Start");
	protected Button btClear = new Button("Clear");

	@Override // Override the start method in the Application class
	public void start(Stage primaryStage) {
		// Create a hbox for buttons
		HBox paneForButtons = new HBox(5);
		paneForButtons.setAlignment(Pos.CENTER);
		paneForButtons.getChildren().addAll(btStart, btClear);

		// 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
		btStart.setOnAction(e -> {
			if (btStart.getText().equals("Start") ||
				 btStart.getText().equals("Resume")) {
				stopWatch.play();
				btStart.setText("Pause");
			} else {
				stopWatch.pause();
				btStart.setText("Resume");
			}
		});

		btClear.setOnAction(e -> {
			stopWatch.clear();
		});

		// Create a scene and place it in the stage
		Scene scene = new Scene(pane);
		primaryStage.setTitle("Exercise_16_20"); // Set the stage title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage
	}
}

StopWatch.java

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;

public class StopWatch extends StackPane {
	// Data field
	private int hour;
	private int minute;
	private int second;
	private Text text = new Text();
	private Timeline animation; // animate stopwatch

	/** Construct a default StopWatch */
	public StopWatch() {
		setPadding(new Insets(5, 15, 5, 15));
		clear();
		text.setFont(Font.font(30));
		getChildren().add(text);
		animation = new Timeline(
			new KeyFrame(Duration.millis(1000), e -> run()));
		animation.setCycleCount(Timeline.INDEFINITE);
	}

	/** Play animation */
	public void play() {
		animation.play();
	}

	/** Pause animation */
	public void pause() {
		animation.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 + "";
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now