BallAnimationTimerController.java// BallAnimationTimerController.java
// Bounce a circle around a window using an AnimationTimer subclass.
import java.security.SecureRandom;
import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.geometry.Bounds;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class BallAnimationTimerController {
@FXML private Circle c;
@FXML private Pane pane;
public void initialize() {
SecureRandom random = new SecureRandom();
// define a timeline animation
AnimationTimer timer = new AnimationTimer() {
int dx = 1 + random.nextInt(5);
int dy = 1 + random.nextInt(5);
int velocity = 60; // used to scale distance changes
long previousTime = System.nanoTime(); // time since app launch
// specify how to move Circle for current animation frame
@Override
public void handle(long now) {
double elapsedTime = (now - previousTime) / 1000000000.0;
previousTime = now;
double scale = elapsedTime * velocity;
Bounds bounds = pane.getBoundsInLocal();
c.setLayoutX(c.getLayoutX() + dx * scale);
c.setLayoutY(c.getLayoutY() + dy * scale);
if (hitRightOrLeftEdge(bounds)) {
dx *= -1;
}
if (hitTopOrBottom(bounds)) {
dy *= -1;
}
}
};
timer.start();
}
// determines whether the circle hit left/right of the window
private boolean hitRightOrLeftEdge(Bounds bounds) {
return (c.getLayoutX() <= (bounds.getMinX() + c.getRadius())) ||
(c.getLayoutX() >= (bounds.getMaxX() - c.getRadius()));
}
// determines whether the circle hit top/bottom of the window
private boolean hitTopOrBottom(Bounds bounds) {
return (c.getLayoutY() <= (bounds.getMinY() + c.getRadius())) ||
(c.getLayoutY() >= (bounds.getMaxY() - c.getRadius()));
}
}
Maintained by John Loomis, updated Sat Feb 10 10:26:06 2018