I'm trying to make a program with balls that (first of all) bounces when closing into the borders of the screen.
However when checking the bounds.getmaxY() value I can see that the value is increasing and because of the if-loops are never used i guess.
public class bouncyFX extends Application {
public static void main(String[] args) {
launch(args);
}
static Pane pane = new Pane();
@Override
public void start(final Stage primaryStage) {
Scene scene = new Scene(pane, 500, 200);
primaryStage.setScene(scene);
primaryStage.show();
pane.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(final MouseEvent event) {
final Ball ball = new Ball(event.getX(), event.getY(), 12, Color.AQUA);
ball.relocate(event.getX(), event.getY());
pane.getChildren().addAll(ball);
final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
double deltaX = 2;
double deltaY = 2;
public void handle(final ActionEvent event) {
ball.setLayoutX(ball.getLayoutX() + deltaX);
ball.setLayoutY(ball.getLayoutY() + deltaY);
ball.Collision(deltaX, deltaY);
final Bounds bounds = pane.getBoundsInLocal();
final boolean atRightBorder = ball.getLayoutX() >= (bounds.getMaxX()-ball.getRadius());
final boolean atLeftBorder = ball.getLayoutX() <= (bounds.getMinX()+ball.getRadius());
final boolean atBottomBorder = ball.getLayoutY() >= (bounds.getMaxY()-ball.getRadius());
final boolean atTopBorder = ball.getLayoutY() <= (bounds.getMinY()+ball.getRadius());
if(atRightBorder || atLeftBorder)
deltaX *= -1;
if(atBottomBorder ||atTopBorder)
deltaY *= -1;
}
}));
loop.setCycleCount(Timeline.INDEFINITE);
loop.play();
}
});
}