Issue
I am trying to show a GIF in a scene on JavaFX, I have it shown on the scene and it plays but it loops back to the beginning before the full GIF is played. The GIF is 216 frames so about 30 seconds long. Is this why it doesn't play the whole thing? Or is there a way to make it play the full thing?
rules.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
Stage rulesWindow = new Stage();
Pane r_2 = new Pane(tutorialView);
Scene sc = new Scene(r_2,width/2,height/2);
rulesWindow.setScene(sc);
tutorialView.setFitWidth(width/2);
tutorialView.setFitHeight(height/2);
rulesWindow.show();
}
});
I instantiated tutorialView
outside the method, so it is not seen in this part of the code.
I can't upload what it plays because the file is too big but if you watch the full GIF it stops where the green circles appear for the first time
Solution
I tried playing the gif back on a Windows PC using JRE Temurin 16.0.2 (the java runtime from adoptopenjdk) and JavaFX 17.0.0.1 and that worked (played all the way through).
I then tried running the same test app on JavaFX 16 and it started looping before playing the whole animation (before the green dots started appearing in the animation).
So JavaFX 16 replicates the issue as Michelle described it, and JavaFX 17 fixes the issue due to a bug fix (as was predicted by Jose).
So, if working with long running gifs, ensure you are using JavaFX 17+.
Test code (using the gif from the question):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GifTest extends Application {
@Override
public void start(Stage stage) {
Scene scene = new Scene(
new StackPane(
new ImageView(
GifTest.class.getResource(
"game.gif"
).toExternalForm()
)
)
);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you would like more control over playback or if you must use an earlier JavaFX version with broken gif playback, you can decode the gif yourself and play it as an animation. See also this sample sprite based playback.
Answered By - jewelsea
Answer Checked By - Clifford M. (JavaFixing Volunteer)