homerun
homerun

Reputation: 20765

JavaFX Splash sceen with GIF

Recently I have developed a personal software to help me with my daily schedule. I have tried to have a splash screen of Homer Simpson eating, as a cool startup splash screen, But unfortunately the GIF won't play.

here is the whole code

public class Homer extends Application
{
    public static final String SPLASH_GIF =
            "https://media.giphy.com/media/JRQ1PegFVKXBu/giphy.gif";
    private static final int SPLASH_WIDTH = 400;
    private static final int SPLASH_HEIGHT = 224;
    private Pane splashLayout;


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

    @Override
    public void start(final Stage initStage) throws Exception {
        ImageView splash = new ImageView(new Image(
                SPLASH_GIF
        ));
        splashLayout = new Pane();
        splashLayout.getChildren().add(splash);

        Scene scene = new Scene(splashLayout, SPLASH_WIDTH, SPLASH_HEIGHT);
        scene.setFill(Color.TRANSPARENT);
        initStage.initStyle(StageStyle.TRANSPARENT);
        initStage.setWidth(SPLASH_WIDTH);
        initStage.setHeight(SPLASH_HEIGHT);
        initStage.setScene(scene);
        initStage.show();
        System.out.println("finished");
    }
}

Having the picture format to be switched to PNG as following will work

public static final String SPLASH_GIF =
        "http://www.dashboardwidgets.com/showcase/data/43/homerQuotes-1p2f.png";

It will result in just a simple picture of Homer, not eating a banana. I would like to have it with a GIF.

How can it be possible?

Upvotes: 1

Views: 1335

Answers (2)

jewelsea
jewelsea

Reputation: 159466

Failure is because the source link is to an html file not a gif file

The issue is that the link in some Folk's question is not actually an image (even though the url ends in .gif), it is instead a HTML page or a redirect to a HTML page (you can see this by loading the link in a browser).

If you replace the html link:

with the actual GIF link:

then the GIF image loading and display works as expected.

Aside on protocol support for image loading

Actually the issue isn't https related. The JavaFX API does implement loading images via https correctly. According to the Image javadoc:

All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.

The underlying JavaFX image implementation just opens a stream from the URL class (which can be determined by searching the JavaFX source code). So any protocol supported by the underlying JRE implementation will work with the JavaFX image class. This includes http:, https:, file: and jar: protocols; and may include others depending upon the JRE implementation. You can even add custom protocol handlers if you wish.

Upvotes: 2

Abacus
Abacus

Reputation: 19461

When trying your example, I can put it down to a

com.sun.javafx.iio.ImageStorageException: No loader for image data

If I use a different URL with an animated gif that is loaded by http and not by https (http://www.picgifs.com/music-graphics/music-graphics/beatles/music-graphics-beatles-186070.gif) and there the ImageView is working.

I also downloaded Homer with the banana and saved it and loaded it with

final ImageView imageView = new ImageView(new Image("file:JRQ1PegFVKXBu.gif"));

The ImageView works al right with that.

I found no hint in the JavaFX API that https is not supported, but it would be better anyway to not do a download in you startup splashscreen, as this might take to long.

Upvotes: 2

Related Questions