user3770144
user3770144

Reputation: 105

Translate transition with String in JavaFX

I would like move vertically with the String which is converted to Text and then implemented into translate transition. When I start it, nothing happened. Any idea how could it work. Thanks.

Code:

public class Pohyb extends Application {

@Override
public void start(Stage primaryStage) {

    Group root = new Group();
    Scene scene = new Scene(root, 300, 300);
    scene.setFill(Paint.valueOf("B0B0B0"));
    primaryStage.setTitle("Canvas");


    primaryStage.setScene(scene);
    startTransition(scene);
    primaryStage.show();   


}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

private void startTransition(Scene scene) {


    //OBDELNÍK
     final Rectangle rect = new Rectangle(32, 32);
     rect.setLayoutX((300/2)- (32/2));
     rect.setLayoutY((300/2)- (32/2));
     rect.setFill(Paint.valueOf("ffffff"));

     Text text = new Text("A");
     text.setLayoutX(145);
     text.setLayoutY(155);


     Group root = (Group) scene.getRoot();
     root.getChildren().add(rect);
     root.getChildren().add(text);

      final TranslateTransition translate = new TranslateTransition(new Duration(1000));
        translate.setNode(text);
        translate.setFromY(text.getLayoutY());
        translate.setToX(text.getLayoutY() + 100);
        translate.setAutoReverse(true);
        translate.setCycleCount(5);
        translate.play();
}    }

Upvotes: 1

Views: 781

Answers (1)

José Pereda
José Pereda

Reputation: 45456

If you increase the size of the stage you'll see your animation working, but at a higher Y coordinate than expected.

If you look at the javadoc for TranslateTransition, it states that it will use translateY property by default. So this will work:

final TranslateTransition translate = new TranslateTransition(new Duration(1000));
translate.setNode(text);
translate.setFromY(text.getTranslateY());
translate.setToY(text.getTranslateY()+100);
translate.setAutoReverse(true);
translate.setCycleCount(5);
translate.play();

This will also work, since the byY property will by used if no toY one is provided:

final TranslateTransition translate = new TranslateTransition(new Duration(1000));
translate.setNode(text);
translate.setByY(100);
translate.setAutoReverse(true);
translate.setCycleCount(5);
translate.play();

Upvotes: 2

Related Questions