JavaFX 3D - A Look Back Through History & Some Experiments
Original post on foojay.io — a place for friends of OpenJDK
After my virtual conference talk “Java and JavaFX on the Raspberry Pi” at the “Oracle Groundbreakers APAC Virtual Tour 2020”, I got in touch with some people who were working on JavaFX 3D in the past, and were curious how that would behave on the Raspberry Pi.
Only one way to find out! Let’s experiment!
History of JavaFX 3D
JavaFX is an open-source, next-generation Java library for rich client applications. JavaFX started with a focus on 2D UI elements.
But by JavaFX 8, it became apparent that certain use cases needed 3D graphics. For example, one of Oracle’s customers needed 3D graphics to visualize a 3-dimensional layout of their shipping containers:
Work on 3D graphics capabilities within JavaFX progressed to the point that 3D characters could be animated with JavaFX. At JavaOne 2013, a chessboard with animated Duke chess pieces was presented at the keynote demo. This animated demo was presented along with a robotic arm that controlled a 3D-printed version of these Duke chess pieces, controlled from the same server.
There is even JavaFX “in space” as this cool video of NASA shows. It’s a demonstration of the “JavaFX Deep Space Trajectory Explorer” as presented on DevNexus 2018.
When JavaFX was taken out of the JDK by Oracle for version 11 in 2018, GluonHQ became one of the main contributors to further develop it within the OpenJFX project. Since then, they kept releasing new versions with the same 6-month release cycle as the OpenJDK. Each version brings new features, improvements, and bug and security fixes. JavaFX 3D is still supported and has become even more powerful with all the ongoing improvements in the framework.
For the upcoming version 16 of OpenJFX, better support for embedded devices running on ARM-processors (e.g. the Raspberry Pi) is in development with better integration of the Direct Rendering Manager (DRM).
Another valuable resource for JavaFX 3D is available at the GitHub project “FXyz3D”. This allows you to create many different 3D custom shapes and even has a GUI application which allows you to visualize all the samples and the different options.
Let’s experiment!
The code
For this post, I collected some existing stuff in this GitHub JavaFX3D project. I didn’t use the original repositories but reworked them a bit to use Maven, so you can get started easily.
Duke
Within the GithHub OpenJDK project, you can find not only the sources of the JDK, but also the Duke in many different formats!
For this experiment, I copied the files from the “Chess Duke” directory as there a multiple 3D images available here.
MoleculeSampleApp
This application renders a water molecule in JavaFX 3D.
3DViewer
This application can load different 3D image file formats and visualize them.
Build and run on PC
Let’s try out if all this works on a Linux PC with OpenJDK 11. We will use this in combination with the current latest JavaFX provided by Gluon.
- Download the JavaFX JDK from https://gluonhq.com/download/javafx-16-ea-sdk-linux/
- Unpack the zip to e.g. /home/{YOUR_NAME}/javafx-sdk-16/
- Move into the directory of one of the Maven projects and build it as a JAR with Maven with
mvn clean package
- Run in with
java -jar ...
and a the javafx-modules we just downloaded
See the detailed info below for each application.
Building and running MoleculeSampleApp
$ cd MoleculeSampleApp
$ mvn clean package
$ java --module-path /home/frank/javafx-sdk-16/lib
--add-modules=javafx.controls
-jar target/moleculesampleapp-0.0.1-jar-with-dependencies.jar</pre>
And oh yeah it runs! As we can expect from Java’s promise for backwards compatibility, this demo created for a Java version in 2013, still works many years later on a much later version of the JDK.
Building and running 3DViewer
$ cd 3DViewer
$ mvn clean package
$ java --module-path /home/frank/javafx-sdk-16/lib
--add-modules=javafx.controls,javafx.fxml
-jar target/Jfx3dViewerApp-0.0.1-jar-with-dependencies.jar</pre>
When the application has started, I opened the Duke image “allStacked_solidColors_w0005_loweredPawnHat.ma” from the Duke Chess directory. The viewer provides multiple options to change the 3D visualization, lighting, etc.
JavaFX 3D on the Raspberry Pi
Some examples
JavaFX 3D is still an experimental feature for embedded but you can get it working if you go back to Java 8 as this tweet of @javafx3d shows:
A practical use-case has been presented some time ago already by PiDome, an open-source full home automation platform developed especially for the Raspberry Pi. It’s a powerful platform providing ease-of-use for non-technical users with possibilities which power users expect.
Window manager versus framebuffer
Regarding JavaFX on embedded, there are a large number of possible configurations on how you want to used. For the Pi, there are two approaches which need there own configuration:
- Run your application inside a window manager (e.g. X11 with Raspberry Pi OS). In this case the application will run in a desktop window.
- Run your application directly to the framebuffer, taking control of the full screen. This is what the DRM-approach allows for (Direct Rendering Manager).
For both configurations, JavaFX can run with hardware acceleration or with software rendering. From a Java developer point, there is no difference, the code is the same. For applicability to the industry, there are important differences. In a development environment, a window manager is preferred, but in embedded hardware, a full-screen solution is often preferred as you don’t want the end-user to open any other application.
As this tweet of José Pereda illustrates, smooth 3D animations on the Raspberry Pi can be achieved!
Conclusion
JavaFX 3D really is a hidden gem! I’ve been using JavaFX already for a long time now but wasn’t aware of these 3D features… And the demos presented here really impressed me.
I didn’t manage to get a running example myself yet on Raspberry Pi, but I hope to spend some time on this in the near future and combine it with some electronics to interact with the 3D environment. To be continued… ;-)