2

JavaFX on the Raspberry Pi

 2 years ago
source link: https://devm.io/java/javafx-raspberry-pi
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java on the Raspberry Pi

JavaFX on the Raspberry Pi

01. Sep 2022


As we have seen in the previous articles in this series, Java and the Raspberry Pi make a great combination! But it gets even better when we add JavaFX in the mix. JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.

JavaFX was created by Sun Microsystems, later became part of Oracle Corporation and is now an open-source project on openjfx.io for which commercial support is available via GluonHQ, which is also the main maintainer of the project. The sources are available on GitHub.

JavaFX allows you to quickly build a user interface while using the language and tools you already know as a Java developer. (Fig. 1) This can be styled with a CSS syntax and is very flexible and extendable and even allows you to draw your own layout elements as you can see in this history graph (available on GitHub as part of the sources of my book "Getting Started with Java on the Raspberry Pi").

Fig. 1

Fig. 1: The history of JavaFX.

In this article, we are going to use the power of JavaFX to build an application that combines the “business logic” (interacting with the GPIOs of the Raspberry Pi) with the “presentation layer” (the user interface) into one program. In comparison with other approaches where the UI is created as a web interface, we don’t need to rely on a browser and API calls. This makes testing and debugging a lot easier as there can’t be any errors somewhere in the process of sending or getting data from an API as we have a very tight integration between the layers of our application.

Starting a new JavaFX project

If you are new to JavaFX and want to have a quick start to test it on your developer PC, Gluon has created the website start.gluon.io which will help you to get a project with the basic code and all configurations in a Maven project (Fig.2).

Fig. 2

Fig. 2: Gluon provides a fast start.

For a minimal JavaFX project, you only need to define the "Application Details" and select the version and modules you are going to need.

As Gluon also offers additional features and you can use their tools to build mobile applications, extra options can be selected in the "Gluon Features" section. Be aware some of these require a license or a popup will be shown when your application starts.

A nice feature of this site is the "Preview Project" button which will show you the structure of your project before you download it (Fig. 3).

Fig. 3

Fig. 3: In the project preview, you can see the structure of the project before downloading it.

By clicking the "Generate Project" button, you will get a ZIP with the full project you can open in your IDE and run immediately with mvn javafx:run.

JavaFX on the Raspberry Pi

The Pi4J project – a friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform. It also provides a minimal JavaFX example project which has been extended with some example GPIO interaction. This is the application we are going to use in this article.

But before we can run it on a Raspberry Pi, we need to take some additional steps. When you start with a new Raspberry Pi OS, please select the “Raspberry Pi OS Full (32-bit)” version in the Raspberry Pi Imager tool, as this one includes Java OpenJDK 11 (Fig. 4).

Fig. 4

Fig. 4: The Raspberry Pi image

Once you have started your Raspberry Pi with the SD card with the full OS, we need to add JavaFX. Some JDK providers (Azul, BellSoft…) provide a version which includes JavaFX, but to benefit from all the improvements done by Gluon and the community for the best performance of JavaFX on embedded, we are going to install JavaFX 17 (early access) separately. Open a terminal to download the package, unzip it and put it in a “logical” place in the opt-directory.

$ wget -O openjfx.zip https://gluonhq.com/download/javafx-17-ea-sdk-linux-arm32/

$ unzip openjfx.zip \
$ sudo mv javafx-sdk-17/ /opt/javafx-sdk-17/

One more thing to add: Maven. This will let us build the example application as modules so we can make fully use of the Pi4J dependencies, but not the JavaFX ones, as the ones optimized for the Raspberry Pi are now available in the /opt/javafx-sdk-17/ directory thanks to the download from the previous step.

$ sudo apt install maven

$ mvn -v

Apache Maven 3.6.0

Maven home: /usr/share/maven

Java version: 11.0.11, vendor: Raspbian, runtime: /usr/lib/jvm/java-11-openjdk-armhf

Default locale: en_GB, platform encoding: UTF-8

OS name: "linux", version: "5.10.17-v7l+", arch: "arm", family: "unix"

The wiring

As this project controls a LED and will read the state of a physical push button, we need to setup a breadboard wiring. The LED needs to be connected to BCM 22 and the button to BCM 24 (Fig. 5). In both cases a resistor is added to limit the current for the LED, and prevent the button to switch a few times from pressed to non-pressed when the button is actually pushed.

Fig. 5

Fig. 5: Structure of the breadboard (plug-in board)

int PIN_BUTTON = 24; // PIN 18 = BCM 24
int PIN_LED = 22; // PIN 15 = BCM 22

We are all set now to get the Pi4J example JavaFX application! Let’s download the...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK