Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8254681

HD Animated GIF don't load with JavaFX Image

XMLWordPrintable

    • x86_64
    • windows_7

      ADDITIONAL SYSTEM INFORMATION :
      Tested with JavaFX 11 and JavaFX 15.

      A DESCRIPTION OF THE PROBLEM :
      Some animated GIF generates an exception when loaded with JavaFX Image.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      It seems i cannot attach a file so you have to download a GIF that causes the error :
      1. Go to https://tenor.com/view/rabbit-cat-animated-happy-jolly-gif-16439626
      2. Select HD GIF (the SD GIF image is loaded OK).
      3. Right click on the animated GIF and save as "sample.gif".
      4. Try to show the downloaded GIF with the sample program.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The animated GIF is displayed in the main window.
      ACTUAL -
      Nothing is displayed in the main window.
      The image has an exception.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.scene.Scene;
      import javafx.scene.image.Image;
      import javafx.scene.image.ImageView;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class AnimatedGifDemo extends Application {

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

          @Override
          public void start(Stage stage) {
              stage.setScene(createScene());
              stage.setTitle("Animated GIF Demo");
              stage.setWidth(800);
              stage.setHeight(600);
              stage.setOnCloseRequest(event -> Platform.exit());
              stage.show();
          }

          private Scene createScene() {
              Image image = new Image(getClass().getResource("sample.gif").toString());
              if (image.isError()) {
                  image.getException().printStackTrace();
              }
              ImageView imageView = new ImageView(image);
              BorderPane borderPane = new BorderPane(imageView);
              return new Scene(borderPane);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      A workaround is to load the image with a Swing ImageIcon and put it in a SwingNode:

      import javax.swing.ImageIcon;
      import javax.swing.JLabel;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.embed.swing.SwingNode;
      import javafx.scene.Scene;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class AnimatedGifDemo extends Application {

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

          @Override
          public void start(Stage stage) {
              stage.setScene(createScene());
              stage.setTitle("Animated GIF Demo");
              stage.setWidth(800);
              stage.setHeight(600);
              stage.setOnCloseRequest(event -> Platform.exit());
              stage.show();
          }

          private Scene createScene() {
              ImageIcon imageIcon = new ImageIcon(getClass().getResource("sample.gif"));
              JLabel imageLabel = new JLabel(imageIcon);
              SwingNode swingNode = new SwingNode();
              swingNode.setContent(imageLabel);
              BorderPane borderPane = new BorderPane(swingNode);
              return new Scene(borderPane);
          }
      }


      FREQUENCY : always


        1. AnimatedGifDemo.java
          1 kB
          Praveen Narayanaswamy
        2. sample.gif
          470 kB
          Praveen Narayanaswamy

            lkostyra Lukasz Kostyra
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: