Here is my following code:
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 500, 500, Color.WHITE);
ImageView image = new ImageView(new Image(getClass().getResourceAsStream("dice.jpeg")));
image.setX(35);
image.setY(225);
image.setFitWidth(50);
image.setFitHeight(70);
root.getChildren().add(image);
Line line = new Line(20, 40, 120, 40);
line.setStroke(Color.RED);
line.setStrokeWidth(10);
root.getChildren().add(line);
if (line.getBoundsInParent().intersects(image.getBoundsInParent())) {
System.out.println("intersect");
}
Timeline timeline = new Timeline(
new KeyFrame(
Duration.seconds(2),
new KeyValue(line.translateYProperty(), 600)
));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.setAutoReverse(false);
timeline.play();
primaryStage.setScene(scene);
primaryStage.show();
}
I want this System.out.println("intersect")
message to be print when the line and image intersect, but when I run my code it doesn't work. Can anyone please tell me what am I doing wrong? Any help is appreciated!