... javafx.scene.shape.Circle; 12   import javafx.util.Duration;
13
14   public class TimelineAnimationController {
15      @FXML Circle c;
16      @FXML Pane pane;
17
18      public void initialize() {
19         SecureRandom random = new SecureRandom();
20
21         // define a timeline animation
22         Timeline timelineAnimation = new Timeline(
23            new KeyFrame (Duration.millis(10),
24               new EventHandler<ActionEvent>() {
25                  int dx = 1 + random.nextInt(5);
26                  int dy = 1 + random.nextInt(5);
27
28                  // move the circle by the dx and dy amounts
29                  @Override
30                  public void handle(final ActionEvent e) {
31                     c.setLayoutX(c.getLayoutX() + dx);
32                     c.setLayoutY(c.getLayoutY() + dx);
33                     Bounds bounds = pane.getBoundsInLocal();
34
35                     if (hitRightOrLeftEdge(bounds)) {
36                        dx *= -1;
37                     }
38
39                     if (hitTopOrBottom(bounds)) ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.