Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Resize animation for JavaFX stage?

Tags:

java

javafx

I've been trying to make a scaling transition for a JavaFX stage to replaces the current Scene (in this case a login frame) for an application's main window.
When this happens, since the new scene is bigger, the windows gets re-sized abruptly in a non-elegant manner.

Is there any way to set up a scaling or re-size transition to do this to the stage resizing?

Relevant code:

InputStream is = null;
try {
    is = getClass().getResourceAsStream("/fxml/principal.fxml");
    Region pagina = (Region) cargadorFXML.load(is);
    cargadorFXML.<ContenedorPrincipal>getController().setEscenario(escenario);

    final Scene escena = new Scene(pagina, 900, 650);

    escena.setFill(Color.TRANSPARENT);
    escenario.setScene(escena);
    escenario.sizeToScene();
    escenario.centerOnScreen();
    escenario.show();
} catch (IOException ex) {
    // log "Unable to load the main application driver"
    log.error("No fue posible cargar el controlador principal de la aplicación."); 
    log.catching(ex);
} finally {
    if (is != null) {
        try {
            is.close();
        } catch (IOException e) {}
    }
}
like image 307
fturizo Avatar asked Jun 25 '13 20:06

fturizo


People also ask

How are the animated properties defined in JavaFX?

The animated properties are defined with KeyValues . Animation is the basic class in JavaFX to define high-level animation. Both Transition and Timeline extend Animation . An animation is started with play () or playFromStart () methods and ended with the stop () method.

How do you animate in JavaFX?

JavaFX - Animations. In general, animating an object implies creating illusion of its motion by rapid display. In JavaFX, a node can be animated by changing its property over time. JavaFX provides a package named javafx.animation. This package contains classes that are used to animate the nodes.

How do I make a sliding scene in JavaFX?

JavaFX Scene Sliding Animation The idea here is to load the new scene beyond the visibility of the screen. For example, if the window heightis 500px, then load the new scene at 500px. Then using KeyFrame animation, take the scene translateY to 0.

What are transitions in JavaFX?

Transitions in JavaFX provide the means to incorporate animations in an internal timeline. Transitions can be composed to create multiple animations that are executed in parallel or sequentially. See the Parallel Transition and Sequential Transition sections for details. The following sections provide some transition animation examples.


2 Answers

I really liked your idea so I managed to do a little something. I hope this will help you.

I used a Timer to change the stage width and height every 25ms in order to give the impression of an animation.

import java.util.Timer;
import java.util.TimerTask;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class SmoothResize extends Application {

    @Override
    public void start(final Stage stage) throws Exception {
        stage.setTitle("Area Chart Sample");
        Group root = new Group();
        Scene scene  = new Scene(root, 250, 250);
        stage.setResizable(false);

        Timer animTimer = new Timer();
        animTimer.scheduleAtFixedRate(new TimerTask() {
            int i=0;

            @Override
            public void run() {
                if (i<100) {
                    stage.setWidth(stage.getWidth()+3);
                    stage.setHeight(stage.getHeight()+3);
                } else {
                    this.cancel();
                }
                i++;
            }

        }, 2000, 25);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
like image 112
Marc Avatar answered Oct 07 '22 00:10

Marc


You can also use animation mechanism with Timeline.
Some example is available here.

The only problem is that Stage do not have writable height or width property and You have to create it by Your own. Example below.

WritableValue<Double> writableHeight = new WritableValue<Double>() {
    @Override
    public Double getValue() {
        return stage.getHeight();
    }

    @Override
    public void setValue(Double value) {
        stage.setHeight(value);
    }
};
like image 4
kcpr Avatar answered Oct 07 '22 00:10

kcpr