Adding handlers and timers

In addition to KeyValue, KeyFrame allows us to set a handler which will trigger at the selected Duration. For example, in the preceding timeline, we can change the object color in the middle:

KeyFrame keyFrame = new KeyFrame(Duration.seconds(5), 
(ActionEvent event) -> {
circle.setFill(Color.GREEN);
},
new KeyValue(circle.translateXProperty(), 200));
KeyFrame keyFrame2 = new KeyFrame(Duration.seconds(10),
new KeyValue(circle.translateYProperty(), 200));
Timeline timeline = new Timeline(keyFrame, keyFrame2);

This allows us to make simple timers for events on a UI thread:

Timeline timer = new Timeline(new KeyFrame(Duration.seconds(1), 
(event) -> {
System.out.println("every second on the UI thread");
})); // no keyvalues is ok

timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.178