java-timeline-timer-test/src/timertest/Controller.java

54 lines
1.2 KiB
Java

package timertest;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.util.Duration;
public class Controller {
@FXML private Label countLabel;
private int count = 0;
private int delay = 500; //default delay
private Timeline ticker;
public Controller(){
this.ticker = new Timeline(new KeyFrame(
Duration.millis(delay),
ae -> tick()
)
);
this.ticker.setCycleCount(Animation.INDEFINITE);
}
private void tick() {
this.count++;
this.countLabel.setText("Count: " + this.count);
System.out.println("Tick: " + this.count);
}
@FXML
private void startTimer() {
if (this.ticker.getStatus() == Animation.Status.RUNNING) {
this.ticker.setRate(this.ticker.getRate() + 1); // make timeline run faster
System.out.println("FASTER!");
} else {
this.ticker.play();
System.out.println("Start");
}
}
@FXML
private void stopTimer() {
System.out.println("Stop");
this.ticker.setRate(1.0);
this.ticker.stop();
}
}