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 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.
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.
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! 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.
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.
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);
}
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!
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)
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.