0

I need some help figuring out how to implement pause functionality into my program.

I'm am creating a timer that I want to be able to pause and play from the same point in time.

Expected outcome: After the pauseButton has been pressed, the startButton picks up right where the pause button left off and displays the same time.

Current outcome: After the pauseButton has been pressed, pressing the startButton starts the timer 2 units less than where the pause button left off. So if the time was paused at 10:58, the startButton picks up the time at 8:56.

Another issue I'm having is that after the pauseButton has been pressed, each iteration of the time change decrements the secondTime timeline by 2 instead of 1 and the minuteTime timeline never changes.

Any help is greatly appreciated. Thank you

// Constant values for time
private final Integer START_TIME_MINUTES = 4;
private final Integer START_TIME_SECONDS = 5;
private final Integer START_TIME_MINUTES = 11;
private final Integer START_TIME_SECONDS = 60;

// Modifiable copies of constants to manipulate
private Integer minutes = START_TIME_MINUTES;
private Integer seconds = START_TIME_SECONDS;

private Integer secondsCopy = START_TIME_SECONDS;

// Creating format in which integers are to be displayed
private final NumberFormat formatter = new DecimalFormat("00");

// Storing string value copies of constants in labels
private final Label minuteLabel = new Label(minutes.toString());
private final Label secondLabel = new Label(seconds.toString());
private final Label secondLabel = new Label(formatter.format(seconds));

// Creating a colon label to add to layout
private final Label colonLabel = new Label(":");

// Creating a label made up of copies of constants
private final Label countDownLabel = new Label("Countdown-> " + minuteLabel.getText() + ":" + secondLabel.getText());
private Timeline minuteTime = new Timeline();
private Timeline secondTime = new Timeline();

private Button pauseButton = new Button("Pause"); 

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Timer Example");  // Title

    System.out.println(countDownLabel);

    Button startButton = new Button("Start" );  // Button
    startButton.setOnAction(actionEvent -> {  // Event Handler
        doTime();  // Calling doTime() function
    });
    startButton.setOnAction(actionEvent -> { doTime(); });

    pauseButton.setOnAction(actionEvent -> { pauseTime(); });

    HBox layout = new HBox(startButton, minuteLabel, colonLabel, secondLabel);  // Layout
    // Changing display of seconds when minute mark reached
    if (seconds == 60)
        secondLabel.setText("00");

    HBox layout = new HBox(startButton, pauseButton, minuteLabel, colonLabel, secondLabel);  // Layout

    Scene myScene = new Scene(layout, 200, 100); // Adding layout to scene
    primaryStage.setScene(myScene);  // Setting scene to stage
    primaryStage.show();  // Displaying stage
}

private void pauseTime(){
    minuteTime.pause();
    secondTime.pause();
}

private void doTime() {

    Timeline minuteTime = new Timeline();
    // Creating a minute timeline

    minuteTime.setCycleCount(Timeline.INDEFINITE);

    Timeline secondTime = new Timeline();
    // Creating a seconds timeline

    secondTime.setCycleCount(Timeline.INDEFINITE);

    if (minuteTime != null && secondTime != null) {}
        minuteTime.stop();


    KeyFrame minuteFrame = new KeyFrame(Duration.seconds(5), actionEvent -> {
        minutes--;
        minuteLabel.setText(minutes.toString());
        if (minutes <= 0) {
            minuteTime.stop();
            Alert minutesAlert = new Alert(Alert.AlertType.INFORMATION);
            minutesAlert.setHeaderText("Minutes KeyFrame Ended");
            minutesAlert.show();
        }
    });

    if (pauseButton.isPressed()){
        minuteTime.playFrom(minuteTime.getCurrentTime());
        secondTime.playFrom(secondTime.getCurrentTime());
    } else {
        // KeyFrame to decrement minuteTime every minute
        KeyFrame minuteFrame = new KeyFrame(Duration.seconds(60), actionEvent -> {
            minutes--;
            minuteLabel.setText(minutes.toString()); // Modifying minute label
            if (minutes <= 0)
                minuteTime.stop();
        });

    KeyFrame secondFrame = new KeyFrame(Duration.seconds(1), actionEvent -> {
        seconds--;
        secondLabel.setText(seconds.toString());

            // continues seconds timer until minute timer is up
            if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.STOPPED) {
        // KeyFrame to decrement secondTime every second
        KeyFrame secondFrame = new KeyFrame(Duration.seconds(1), actionEvent -> {
            seconds--;  // decrement seconds
            secondLabel.setText(formatter.format(seconds)); // modify seconds label

            // stops secondTime if seconds reaches 0 and minuteTime stopeed
            if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.STOPPED)
                secondTime.stop();
                Alert secondsAlert = new Alert(Alert.AlertType.INFORMATION);
                secondsAlert.setHeaderText("Seconds KeyFrame Ended");
                secondsAlert.show();

            // if secondTime reaches 0, but minuteTime is still running, reset seconds
            if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.RUNNING)
                seconds = START_TIME_SECONDS;

            } 

            // Changes seconds label to 00 when minute mark is reaches
            if (seconds == 60)
                secondLabel.setText("00");
        });

            if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.RUNNING){
                seconds = START_TIME_SECONDS;
            }
    });

    minuteTime.getKeyFrames().add(minuteFrame);
    minuteTime.playFromStart(); // execute the timeline


    secondTime.getKeyFrames().add(secondFrame);
    secondTime.playFromStart();
        minuteTime.getKeyFrames().add(minuteFrame); // Adding frame to timeline
        minuteTime.playFrom(Duration.seconds(59)); // execute the timeline

        secondTime.getKeyFrames().add(secondFrame);
        secondTime.playFromStart();
    }
}`
  • See if you can get some ideas from https://stackoverflow.com/questions/59681624/best-way-to-synchronize-countdowntimers-in-javafx/59684907#59684907 – SedJ601 Sep 11 '20 at 05:21
  • https://stackoverflow.com/questions/50766244/creating-a-timer-that-shows-the-time-in-hhmmss-format-javafx/50774740#50774740 – SedJ601 Sep 11 '20 at 05:22
  • Does this answer your question? [how to set up two timelines to one app?](https://stackoverflow.com/questions/50626831/how-to-set-up-two-timelines-to-one-app) – SedJ601 Sep 11 '20 at 05:26
  • The possible duplicate is probably your best help. – SedJ601 Sep 11 '20 at 05:27

0 Answers0