XPenguen
XPenguen

Reputation: 183

TranslateTransition JavaFX

I am trying to implement a TranslateTransition to my rectangle but it doesn´t do anything at all. Here is my code:

primaryStage.setTitle("AnimationTest");
    Group group = new Group();

    Rectangle rect = new Rectangle(0,0,100,100);
    group.getChildren().add(rect);
    TranslateTransition transition =new TranslateTransition(Duration.millis(1000),rect);
        transition.setByX(100);

    Button button = new Button("StartAnimation");
        button.setOnAction((e)->{
            transition.play();
        });
    VBox layout = new VBox();
    layout.getChildren().addAll(group, button);
    Scene scene = new Scene(layout, 600, 600);
    primaryStage.setScene(scene);
    primaryStage.show();

Upvotes: 0

Views: 233

Answers (1)

James_D
James_D

Reputation: 209339

According to the documentation for Group:

A Group will take on the collective bounds of its children

In other words, as the rectangle moves, the coordinate system of the group adjusts to fit everything it contains. Since the only thing it contains is the rectangle, the rectangle remains fixed with respect to the group's container.

Use a Pane instead of the Group:

primaryStage.setTitle("AnimationTest");
Pane pane = new Pane();

Rectangle rect = new Rectangle(0,0,100,100);
pane.getChildren().add(rect);
TranslateTransition transition =new TranslateTransition(Duration.millis(1000),rect);
    transition.setByX(100);

Button button = new Button("StartAnimation");
    button.setOnAction((e)->{
        transition.play();
    });
VBox layout = new VBox();
layout.getChildren().addAll(pane, button);
Scene scene = new Scene(layout, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();

Upvotes: 1

Related Questions