-2

When starting an application, how can I make an ImageView show for a certain time and after that time hide in JavaFx?

Layynezz
  • 112
  • 1
  • 8
  • Like a splash screen? – b3tuning May 28 '20 at 05:31
  • No, it would be a main stage. – Layynezz May 28 '20 at 05:36
  • 4
    Use a `PauseTransition`. You can adapt [this answer](https://stackoverflow.com/a/34681073/6395627). – Slaw May 28 '20 at 07:21
  • what if i wanted to use a transition with TranslateTransition how would it be? – Layynezz May 28 '20 at 14:46
  • 3
    I recommend reading the API documentation, and possibly any tutorials you can find, then try to implement something yourself. If you get stuck on something _specific_ please ask a _specific_ question with a [mre] demonstrating the problem. – Slaw May 28 '20 at 15:18

1 Answers1

3
package sample;

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;


public class Main extends Application {

    @Override
    public void start(Stage stage) {

        Pane pane = new Pane();
        ImageView imageView = new ImageView(
                new Image(getClass().getResource("your-image.png").toString()));
        pane.getChildren().add(imageView);
        stage.setScene(new Scene(pane));
        stage.show();

        PauseTransition wait = new PauseTransition(Duration.seconds(3));
        wait.setOnFinished((e) -> imageView.setVisible(false));
        wait.play();
    }


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

Update:

package sample;

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.util.Duration;


public class Main extends Application {

    @Override
    public void start(Stage stage) {

        AnchorPane anchorPane = new AnchorPane();
        ImageView imageView = new ImageView(
                new Image(getClass().getResource("your-image.png").toString()));
        imageView.setOpacity(0);
        imageView.setFitWidth(200);
        imageView.setFitHeight(200);
        anchorPane.getChildren().add(imageView);

        AnchorPane.setTopAnchor(imageView, 0d);
        AnchorPane.setRightAnchor(imageView, 0d);

        Scene scene = new Scene(anchorPane);
        stage.setScene(scene);
        stage.setMaximized(true);
        stage.show();

        int seconds = 5;

        // Move the image to the center of the stage
        TranslateTransition translateTransition = new TranslateTransition(Duration.seconds(seconds), imageView);
        translateTransition.setToX((stage.getWidth() - imageView.getFitWidth()) / -2);
        translateTransition.setToY((stage.getHeight() - imageView.getFitHeight()) / 2);
        translateTransition.play();

        // Fade in and out:
        KeyFrame startfadeIn = new KeyFrame(Duration.seconds(0.2), new KeyValue(imageView.opacityProperty(), 0));
        KeyFrame endFadeIn = new KeyFrame(Duration.seconds(seconds), new KeyValue(imageView.opacityProperty(), 1.0));
        KeyFrame endFadeOut = new KeyFrame(Duration.seconds(seconds * 2), new KeyValue(imageView.opacityProperty(), 0));
        Timeline timeline = new Timeline(startfadeIn, endFadeIn, endFadeOut);
        timeline.play();

        // Remove imageView:
        PauseTransition wait = new PauseTransition(Duration.seconds(seconds * 2));
        wait.setOnFinished((e) -> anchorPane.getChildren().remove(imageView));
        wait.play();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
anko
  • 1,628
  • 1
  • 6
  • 14
  • what if i wanted to use a transition with TranslateTransition how would it be? – Layynezz May 28 '20 at 14:46
  • 1
    Do you want to move the ImageView?! Just add this: TranslateTransition translateTransition = new TranslateTransition(Duration.seconds(3), imageView); translateTransition.setToX(100d); translateTransition.play(); – anko May 28 '20 at 15:07
  • Your answer helped me, but I think I did not imply myself, the ImageView would like it to appear from the edge of an AnchorPane with a Translate Transition, and to last 5 seconds, after that time it hides, also with a Translate Transition. – Layynezz May 28 '20 at 15:20
  • 1
    Like Slaw stated already it is not so clear what you are trying to achive. Nevertheless please take a look at my updated answer. Maybe I had a good guess and it helps you. ;) – anko May 28 '20 at 16:58
  • Just what i was looking for! – Layynezz May 28 '20 at 21:23
  • hehe, okay. ;-) – anko May 28 '20 at 21:36