I am writing a javafx application in which I want to put a rectangle in a specific place ( coordinates 100, 100) and I want its width to increase on the left side of the rectangle. (I wish to use anchor pane in my program.) This is the code I have, I brought it from this link:
JavaFX rectangle width animation
public class RectangleWidthAnimation extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Rectangle statusBar = new Rectangle(300, 100);
Button animationButton = new Button("Animate width increase by 25");
animationButton.setOnAction(event -> {
System.out.println("Animation start: width = " + statusBar.getWidth());
KeyValue widthValue = new KeyValue(statusBar.widthProperty(), statusBar.getWidth() + 25);
KeyFrame frame = new KeyFrame(Duration.seconds(2), widthValue);
Timeline timeline = new Timeline(frame);
timeline.play();
timeline.setOnFinished(finishedEvent -> System.out.println("Animation end: width = " + statusBar.getWidth()));
});
VBox container = new VBox(10, statusBar, animationButton);
container.setAlignment(Pos.CENTER_RIGHT);
primaryStage.setScene(new Scene(container, 350, 150));
primaryStage.show();
}
}
What I want to change is that I don't want to combine animationButton and statusBar together. I want to have control over the statusbar itself. So I just put this instead:
VBox container = new VBox(statusBar);
container.setLayoutX(100);
container.setLayoutY(100);
container.setAlignment(Pos.CENTER_RIGHT);
primaryStage.setScene(new Scene(new Group(container, animationButton), 350, 150));
But when I do this, the rectangle width increases from the right side, which is not what I want. ( I want the width to increase from left side. In other words, I want to fix the upper right coordinate of the rectangle and make it seem that the upper left coordinate of the rectangle is moving leftwards.)
The first code makes statusBar grow to the left but both statusBar and animationButton are inside one Vbox(container). But I want to put statusBar in a single VBox or HBox and apply the previous code again. I tried this code but it strangely makes the statusBar grow to the right.
public class RectangleWidthAnimation extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Rectangle statusBar = new Rectangle(300, 100);
Button animationButton = new Button("Animate width increase by 25");
animationButton.setOnAction(event -> {
System.out.println("Animation start: width = " + statusBar.getWidth());
KeyValue widthValue = new KeyValue(statusBar.widthProperty(), statusBar.getWidth() + 25);
KeyFrame frame = new KeyFrame(Duration.seconds(0.4), widthValue);
Timeline timeline = new Timeline(frame);
timeline.play();
timeline.setOnFinished(finishedEvent -> System.out.println("Animation end: width = " + statusBar.getWidth()));
});
VBox container = new VBox(statusBar);
container.setLayoutX(100);
container.setLayoutY(100);
container.setAlignment(Pos.CENTER_RIGHT);
AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().add(container);
anchorPane.getChildren().add(animationButton);
primaryStage.setScene(new Scene(anchorPane, 500, 350));
primaryStage.show();
}
}
Any suggestions on how to solve this?