简体   繁体   中英

JavaFX Cant apply any animation class to “stage”

I wanna apply any transition class but their .setNode() method only accepts Node which does not accept scene or stage . How can i do animations on directly stage? Below code gives error;

  TranslateTransition transition = new TranslateTransition();
    transition.setFromX(1000);
    transition.setFromY(1000);
    transition.setToX(500);
    transition.setToY(500);

    transition.setDuration(Duration.seconds(5));
    transition.setNode(stage);
    transition.play();

You can subclass Transition and override the interpolate method to call setX() and setY() :

import javafx.animation.Animation;
import javafx.animation.Transition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class AnimatedWindow extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setScene(new Scene(new StackPane(), 400, 400));
        Animation transition = new Transition() {

            private final double startX = 1000 ;
            private final double endX = 500 ;

            private final double startY = 1000 ;
            private final double endY = 500 ;

            {
                setCycleDuration(Duration.seconds(5));
            }

            @Override
            protected void interpolate(double frac) {
                double x = frac * endX + (1 - frac) * startX ;
                double y = frac * endY + (1 - frac) * startY ;
                primaryStage.setX(x);
                primaryStage.setY(y);
            }

        };

        primaryStage.show();

        transition.play();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM