1
votes

How to create an animation during the changing of stages?

-For instance, I have 2 stages that have different sizes(Width and Height). Normally, without any animations, the 2nd screen just pops out in an instant. I want the 2nd stage to show smoothly(like opening or closing a window in Mac OS Sierra). All tutorials that I can find are mostly about animations in changing scenes. Maybe my way of searching is wrong.

Here's how I change stages.

  FXMLLoader loader = new FXMLLoader(getClass().getResource(Insert FXML here));
    Parent root = loader.load();
    Stage stage = new Stage();
    stage.setResizable(false);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setScene(new Scene(root));
    stage.show();

Any thread that are related to what I'm looking for is greatly appreciated.

1

1 Answers

0
votes

Try an AnimationTimer. Timeline won't work becuase the Properties of the Stage are read Only.


FXMLLoader loader = new FXMLLoader(getClass().getResource(Insert FXML here));
Parent root = loader.load();
Stage stage = new Stage();

stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(new Scene(root));
stage.show();


//set your Stage width,height,x,y to zero

stage.setX(0);
stage.setY(0);
stage.setWidth(0);
stage.setHeight(0);
AnimationTimer t = new AnimationTimer() {
            @Override
            public void handle(long now) {
                stage.setX(stage.getX()+1);
                stage.setX(stage.getX()+1);
                stage.setWidth(stage.getWidth()+1);
                stage.setHeight(stage.getHeight()+1);
                // Here put your condition for "destination and desired size" 
                 // in this case everything will move/grow by 600 px
                if( stage.getX() == 600){
                    this.stop();
                    stage.setResizable(false);
                }
            }
        };
        t.start();

This won't look smooth enough, but you can figure out an "interpolation" mechanism, to make a even smooth movement.

EDIT:

I forgot! You also can use Transitions:

Animation transition = new Transition() {
    {
        setCycleDuration(Duration.millis(2000));
        setInterpolator(Interpolator.EASE_IN);
    }
    @Override
    protected void interpolate(double frac) {
        // frac is the percentage of how much time of the Transition
        // Already passed (1000 ms => frac = 0.5)
        // Here you just Multiply frac with the designated value
        stage.setHeight(height*frac);
        stage.setWidth(width*frac);
        stage.setX(x*frac);
        stage.setY(y*frac);        
    }
};
transition.play();

This tends to be smoother since you can use the Interpolator