A JavaFX Game Application in a Single Java File with JBang and FXGL

FXGL is a framework to easily create JavaFX-based game applications. In this blog post, I want to show you how this can be done within a single Java-file which doesn’t need a full Maven or Gradle project but can be executed directly with JBang without compilation. This approach can be used as an easy way to get new Java(FX) developers started or create your first experiments with FXGL.

More info and code walk-through together with Almas Baim in this live stream, recorded on December 27th, 2023:

Used Technologies

JBang

By using JBang, no full Maven or Gradle project is needed, and the code can be executed with a single command. JBang even allows you to open a file with VSCodium with all required plugins, see JBang Editing for all info.

The installation instructions for all platforms are documented on jbang.dev/download.

FXGL

FXGL is a JavaFX Game Development Framework, which is open-source and can be found on github.com/AlmasB/FXGL.

Advantages:

It’s good for:

Earlier articles about FXGL on this site:

Java Runtime With Included Java

OpenJFX (= the sources of JavaFX) is a separate project next to the OpenJDK project (= the sources of Java). Most Java runtimes are based on OpenJDK only, so an extra step is needed to download the JavaFX runtimes. Luckily some distributors provide builds of OpenJDK with OpenJFX integrated, so you only need one installation.

On Linux and macOS with SDKMAN!

If you are running this example on Linux or macOS, use SDKMAN! to easily install such a runtime:

$ curl -s "https://get.sdkman.io" | bash
$ sdk install java 21.0.1.fx-zulu  

On Windows

Download an installer, for example, from the Azul website.

Example Application

The sources of this demo can be downloaded from GitHub. Actually, there is only one important file JBangFXGLDemo.java as this is the single file with Java code, needed to run the application. Within the resources directory, you will find the images used in the game.

This example is based on the demo project created for the article Look out, Duke! Part 1: Build a Java game with JavaFX and FXGL where a Maven project is used. All the code spread across multiple classes, has been combined into one file for this example.

JBang Configuration

As with every JBang file, we need to use a specific first line. Additionally, we also define the dependencies (only one in this case, as FXGL includes the JavaFX dependency) and the directory with the resources.

///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS com.github.almasb:fxgl:17.3
//FILES resources/

Structure of the Code

The full class consists of several elements:

Take a look at each section of this example application, and experiment with the various settings and Components.

Running the application

$ jbang JBangFXGLDemo.java 
[JavaFX Application Thread] INFO  Engine               - FXGL-17.3 (30.03.2023 11.49) on MAC (J:21.0.1 FX:21.0.1)
[JavaFX Application Thread] INFO  Engine               - Source code and latest versions at: https://github.com/AlmasB/FXGL
[JavaFX Application Thread] INFO  Engine               -       Ask questions and discuss at: https://github.com/AlmasB/FXGL/discussions
[JavaFX Application Thread] INFO  Engine               -              Join the FXGL chat at: https://gitter.im/AlmasB/FXGL
[FXGL Background Thread 1 ] INFO  FXGLApplication      - FXGL initialization took: 0,315 sec
[FXGL Background Thread 4 ] INFO  FXGLApplication      - Game initialization took: 0,104 sec

Conclusion

FXGL is a perfect library to get started with game development with Java and JavaFX. And by using JBang, it becomes even more easy, as you only need one file to create a complete game.