JavaFX GIF Player

The following code will create a JavaFX ImageView in the default Processing window and may be used as a .gif player in addition to displaying other images. You will need the full url for the file, in this case in the sketch folder (change posted code to reflect your system and filename).
Disclaimer: This demo will not run 'asis' on the Windows or Linux operating systems. It will be necessary to create a separate folder entitled 'code' in the sketch folder and copy/paste the seven JavaFX modules into it. These modules are located in the Processing/libraries/javafx/library/yourOS/modules folder. It may also be necessary to add the line 'import processing.javafx.*;' to the top of the sketch.

import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.FileInputStream;

StackPane root;
FileInputStream input;

void setup() {
  size(400, 400, FX2D);
  surface.setTitle("JavaFX GIF Player");
  Canvas canvas = (Canvas)surface.getNative();
  root = (StackPane)canvas.getParent();
  try {
    input = new FileInputStream("/Users/yourName/Documents/Processing/yourSketchName/yourFile.gif");
  }
  catch(IOException e) {
    println("Unable to input file.",e);
  }
  Image image = new Image(input);
  ImageView imageView = new ImageView(image);
  root.getChildren().add(imageView);
}

Output:

2 Likes