JavaFX GIF Player Discussion

Hello @svan,

This is a discussion of your gallery post:
JavaFX GIF Player

I do not want to sidetrack Gallery posts, so I thought I would bring this here.

Great examples! Could you add a note to your JavaFX posts that it doesn’t run under Processing on Windows? It might help avoid confusion for new users.
Thanks!

This may help Windows users with your JavaFX posts:
Processing and JavaFX - #5 by glv

If you do not have a file to load it will fall through the catch() and generate a NullPointerException.

You can add a clean exit() in the catch():

void exit() {
   println("Exiting!");
}

Or you can check input:

 (input != null)
    {
    Image image = new Image(input);
    // Code...
  }  
}

This will load from a URL and also save to the dataPath():

import processing.javafx.*;

PImage img;

void setup() 
  {
  size(200, 200, FX2D);
  
  String urlString = "http://learningprocessing.com/code/assets/sunflower.jpg";
  img = loadImage(urlString);
  println(img.width, img.height);
  
  String url = dataPath("") + "\\" + "sunflower.jpg";
  println(url);
  
  img.save(url);  // If you want to save it and use it later
  
  image(img, 0, 0);  
  }

Note:
Above won’t work with a link to a GIF.
It is a demonstration of:

  • Getting an image directly from a URL; handy for demo code if you have Internet access and able to link to the file.
  • Saving the PImage to a file.
  • Using dataPath()

:)

The JavaFX ImageView will do double duty by displaying images or playing a .gif animation. I have not seen any ‘simple’ example of Processing code that will run a .gif animation; this demo will do that. The try…catch construct was added because of an error message that indicated an uncaught exception was possible, so that’s why it was added. If you add ‘,e’ to the println() it will give a more detailed explanation of what went wrong. I have run the demo on all three platforms, macOS, Windows11, and Linux. It runs on macOS without error. On both Windows and Linux it is necessary to create a separate folder entitled ‘code’ in the sketch folder and then dig out the seven JavaFX modules from the libraries folder and copy/paste them into the ‘code’ folder, since the runtime doesn’t seem to realize that’s where they are located. It may also be necessary to add the line ‘import processing.javafx.*;’ at the top. It’s unfortunate that JavaFX won’t run without error on all three platforms; I have no idea why it works on a mac and not the other two. All I know is that it is a good library and I wish that the requisite seven modules were not separate.

It is necessary for W10.

It does indeed! Cool beans!

This will save a GIF from a URL that can be used with your code directly (works with W10):

  // Saves a GIF from a URL
  String urlString = "https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2233c4019c84e00b15d090b47921d0578a64f948.gif";  
  byte b[] = loadBytes(urlString);
  
  String url = dataPath("") + "\\" + "animation.gif";  
  saveBytes(url, b);

You opened an issue on this and the response is here:
https://github.com/processing/processing4/pull/856#issuecomment-26509641

In the mean time there is a workaround and anyone interested in using JavaFX can do so and we are here to support this.

I have created a template locally that contains the modules and that is a simple working solution for me. I also provided a link in previous post to help Windows users.

I have worked around most challenges related to Processing and this is just one more example.

I may challenge myself to find a solution to this but not a priority for me.

I can use the JavaFX library when needed for Windows 10 and that is a viable solution. I’m already familiar with working with local libraries in sketches, which is a useful skill I’ve developed with other libraries and would benefit others to learn.

Viva la open source and all the challenges!

I threw that into ChatGPT and it came back with:

Viva indeed! :tada: Open source is a wild ride — full of collaboration, breakthroughs, and occasional chaos. But that’s the beauty of it: people all over the world building and improving together, learning from each other, and keeping the spirit of freedom and transparency alive.

Have fun and enjoy the ride!

:)

Mac doesn’t like the url technique:
Invalid URL: Invalid URL or resource not found

Does it work if you cut and past this directly in a browser?
Which browser?
Which OS?

https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2233c4019c84e00b15d090b47921d0578a64f948.gif

It works with all of these browsers in W10:

Microsoft Edge
Firefox
Google Chrome
Brave
LibreWolf

:)

Your link works ok, I see the animated gif.
The first problem is this line of code:
String url = dataPath("") + "\\" + "animation.gif";
Which generates this on my system:
/Users/s/Documents/Processing/javafx_gifPlayer_glv/data\animation.gif
The slash preceeding ‘animation.gif’ is going the wrong way; it’s supposed to be a forward slash not a backward slash.

But there’s more to it than that; even after that is fixed it still won’t run. You are calling a ‘string’ a ‘url’ which is not the same thing on a Mac. There is a way to convert a string to a url in java, but image() is expecting an input stream in this example. It likes the connection that FileInputStream() creates. I’m a little surprised that Windows accepts the byte array file.

Everything works as expected on Windows 10:

Code used:

String urlString1 = "http://learningprocessing.com/code/assets/sunflower.jpg";
println(urlString1);

println();

String urlString2 = dataPath("") + "\\" + "animation.gif";  
println( urlString2);

What does a MAC do with above for comparison? Save sketch first.

Reference for use of \\:

:)

Output to your example:

http://learningprocessing.com/code/assets/sunflower.jpg
/var/folders/gp/zjsplfcs5gbgm8n1jt_pfx800000gn/T/processing/06a727fa-e6c2-48b1-b3a7-681713df38bc/sketch_250615d/data\animation.gif

There is no dataPath until the file is saved.
Then this:

void setup(){
String urlString1 = "http://learningprocessing.com/code/assets/sunflower.jpg";
println(urlString1);

println();

String urlString2 = dataPath("") + "\\" + "animation.gif";  
println( urlString2);

println();
String fileStr = dataPath("") + "/animation.gif"; 
println(fileStr);
}

Outputs this:

http://learningprocessing.com/code/assets/sunflower.jpg

/Users/s/Documents/Processing/dataPath_demo/data\animation.gif 

/Users/s/Documents/Processing/dataPath_demo/data/animation.gif

It is not so black and white and depends on operating system.

Reference:

:)

Looks pretty black and white to me; Mac uses forward slashes and Windows uses back slashes. It’s been that way from the very beginning going back to MS-DOS.

My snippet of code provided previously said “load” and should have been “save” and I corrected it.
It saves a GIF that was downloaded for potential use with your example if integrated correctly.

Complete working code for W10 provided for clarity:

// Source of code:
// https://discourse.processing.org/t/javafx-gif-player/46543

// Modifications made by glv 2025-06-16

// This is written for Windows 10 and works.
// See this topic for a discussion on how to get JavaFX working on Windows:
// https://discourse.processing.org/t/processing-and-javafx/45692

import processing.javafx.*;

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(605, 605, FX2D);
  surface.setTitle("JavaFX GIF Player");
  Canvas canvas = (Canvas)surface.getNative();
  root = (StackPane)canvas.getParent();

  // Once you have saved this you can comment these two lines:
  String urlString = "https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2233c4019c84e00b15d090b47921d0578a64f948.gif";
  saveUrlFile(urlString);  // saves a GIF from a URL      
  
  //String url = dataPath("") + "\\" + "animation.gif";  // Works!
  String url = dataPath("") + "\\animation.gif";         // Works!

  try {
    input = new FileInputStream(url);
  }
  catch(IOException e) {
    println("Unable to input file.", e);
  }
  
  if (input != null)
    {
    Image image = new Image(input);
    ImageView imageView = new ImageView(image);
    root.getChildren().add(imageView);
    }
}


// saves a GIF from a URL (Universal Resource Locator)
// I could not use saveFile() as this was reserved by Processing

void saveUrlFile(String _urlString)
{
  //String urlString = "https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2233c4019c84e00b15d090b47921d0578a64f948.gif";

  byte b[] = loadBytes(_urlString);

  //String url = dataPath("") + "\\" + "animation.gif";
  String url = dataPath("") + "\\animation.gif";
  saveBytes(url, b);
}

:)

I see that you added a note to your Gallery post about this!

Thanks!

It is clear that there are differences between the Mac and Windows experiences for users of Processing in a number of areas.

This may make a good Wiki at some point:

:)

Function dataPath() is supposed to accept an actual filename string:
final String url = dataPath("animation.gif");
We don’t have to worry about whether an OS uses "\" or "/" if we do like the above!

2 Likes

Thanks for the reference!

I will most certainly start using these more often in the future.

I will submit an issue to have the Processing Javadocs shared somewhere on the main website as well. With the exiting references may be a good place.

Is it somewhere on the main website that I am missing?

Processing Javadocs:

:)

It’s puzzling useful functions like sketchPath(), sketchFile(), dataPath(), dataFile(), getExtension(), etc.; aren’t mentioned on Processing’s web reference:

BtW, listPaths() is excellent to get all images, audio, video and other assets from a path.

An example using it:

1 Like

I’m not sure that you need the extra saveUrlFile() function. The following works ok in Windows11. There must be something special about the website that we’ve been using for testing in that it apparently will automatically download the .gif file. I tried using a .gif file uploaded to DropBox and it failed. My original post works ok for a .gif animation pre-loaded into the sketch folder (doesn’t need to be in ‘data’ folder)

import processing.javafx.*;

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();
  String urlString = "https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2233c4019c84e00b15d090b47921d0578a64f948.gif";
  byte b[] = loadBytes(urlString);
  String url = sketchPath("testGIF.gif");
  saveBytes(url, b);
  try {
    input = new FileInputStream(url);
  }
  catch(IOException e) {
    println("Unable to input file.",e);
  }
  Image image = new Image(input);
  ImageView imageView = new ImageView(image);
  root.getChildren().add(imageView);
}

Thanks for the contributions.

1 Like

Processing 1st search the sketchPath(), then it attempts its subfolder “data”.

However, this double-folder path search only happens on actual Processing load functions, like loadImage(), loadStrings(), etc.

However, having assets in subfolder “data” is tradition.

  • I modularize my code into reusable functions for a library I have.
  • It makes my projects cleaner, easier to maintain, and much more efficient to develop new ideas.
  • It is easy to comment out once you have downloaded the file.
  • It prompts questions and comments to engage the forum.

It is up to the website (or server) whether they allow your Processing application to access and download their images directly.

The URL I provided is from a Processing topic in this forum:

I can also get images from Wikipedia and Processing GitHub repositories.

Another favorite of mine:

http://learningprocessing.com/code/assets/sunflower.jpg

size(200, 200);
image(loadImage("http://learningprocessing.com/code/assets/sunflower.jpg"), 0, 0);

:)

This seems to work (folder path in picture) with W10 and your last example:

import processing.javafx.*; //Required

Copy some jar files to the JavaFX library:

And I did not have to copy any jar files to the sketch folder!

It is NOT an all encompassing solution for W10.
It does not seem to work with some of your examples if I copy all the jar files to the library folder.

I am not actively looking for a solution but I do like to experiment and every step taken is a step forward.

:)

I can confirm that the same procedure works in Windows11 with this particular demo. I removed the ‘code’ folder from the sketch folder after copy/pasting the seven module files plus ‘javafx.jar’ into the javafx/library folder and it still ran without error. The jar files still have to be copy/pasted, just not into a separate ‘code’ folder. If it works every time then Processing could be shipped with the files out there instead of buried deeper in a separate ‘modules’ folder. It’s worth further exploration.

1 Like