Tuesday, December 3, 2013

Run JavaFX on Raspberry Pi

This post try to develope JavaFX Application on Windows 8 and run on Raspberry Pi.

Development host PC: 
OS - Windows 8 Pro
Development Tools: NetBeans IDE 7.4
JDK: jdk 1.7.0_45

Raspberry Pi:
Model B with 8G SD Card
OS: Raspbian wheezy 2013-09-25
Memory Split: 64
Overclock: Medium 900MHz
Run in Text Console mode
java version 1.8.0-ea (build 1.8.0-ea-b117)

Steps:
- New a JavaFX Application in Netbeans, named "JavaFXHelloPi".

- Modify the Java code to add a button to exit.
package javafxhellopi;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXHelloPi extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        Button btnExit = new Button();
        btnExit.setText("Exit");
        btnExit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        });

        VBox vBox = new VBox();
        vBox.getChildren().addAll(btn, btnExit);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}


- It run on Windows 8 like this:

- ftp the created jar, JavaFXHelloPi\dist\JavaFXHelloPi.jar, to Raspberry Pi.

- ssh to Raspberry Pi, run the jar by entering the command:
$ java -jar JavaFXHelloPi.jar



Remark:
It's just a personal experiment. For official steps, refer: https://wiki.openjdk.java.net/display/OpenJFX/OpenJFX+on+the+Raspberry+Pi

More example of JavaFX on Pi:
JavaFX animation on raspberry Pi

No comments: