Page 1 of 1

Always Fullscreen/Black Background with JavaFX & Nashorn

Posted: Sat May 02, 2015 10:52 am
by mob-i-l
I have succeeded to run some JavaScript-examples that uses Nashorn and JavaFX, but they don't run in a window in Raspbian LXDE, and the background is completely black, and one can't switch to a Linux console using Ctrl+Alt+F2 and back using Alt+F7. The way to stop the original examples was to login using SSH from another computer and kill the processes. Is there another way to switch back to X Windows without quitting the program? Now I've implemented quit(), but I would still prefer if one could run these JavaFX-programs in X with a normal close button. E.g. Minecraft PI Edition runs in a window but still writes directly to the screen.

You can save the following code as HelloWorldSimple.js and make executable using chmod +x HelloWorldSimple.js and then run using ./HelloWorldSimple.js:

Code: Select all

#!/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/bin/jjs -fx
var Button = javafx.scene.control.Button;
var StackPane = javafx.scene.layout.StackPane;
var Scene = javafx.scene.Scene;

$STAGE.title = "Hello World!";
var button = new Button();
button.text = "Say 'hello, world'";
button.onAction = function() { print("hello, world"); quit(); };
var root = new StackPane();
root.children.add(button);
$STAGE.scene = new Scene(root, 300, 250);
$STAGE.show();
Original from https://docs.oracle.com/javase/8/docs/t ... avafx.html .

You can save the following code as HelloWorld.js and make executable using chmod +x HelloWorld.js and then run using ./HelloWorld.js:

Code: Select all

#!/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/bin/jjs -fx
var Group = javafx.scene.Group;
var Scene = javafx.scene.Scene;
var Text = javafx.scene.text.Text;
var Platform = Java.type("javafx.application.Platform");
var Timer    = Java.type("java.util.Timer");

$STAGE.scene = new Scene(new Group(new Text(25, 25, "Hello World!")));
$STAGE.title = "Hello World!";
$STAGE.show();
setTimeout(function() { print("Quitting..."); quit(); }, 5000);

function setTimeout(func, milliseconds) {
    var timer = new Timer("setTimeout", true);
    timer.schedule(function() Platform.runLater(func), milliseconds);
    return timer;
}
Original from https://docs.oracle.com/javafx/2/api/ja ... Stage.html and https://blogs.oracle.com/nashorn/entry/ ... _functions .

One solution is to use one Raspberry Pi as JavaFX display and log in from another computer (which could be another Raspberry Pi) using SSH. Still, it's rather good one can use JavaFX from JavaScript. I use Raspberry Pi 2 B.

I made an AWT-version of HelloWorldSimple.js called AWTHelloWorldSimple.js and that runs in a window:

Code: Select all

#!/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/bin/jjs -strict=true -fv
//#!/usr/bin/env rhino
var Button = java.awt.Button;
var Frame = java.awt.Frame;
var Dimension = java.awt.Dimension;

frame = new Frame("Hello World!");
button = new Button("Say 'hello, world'");
button.addActionListener(function() { print("hello, world"); quit(); })
frame.add(button);
frame.setSize(new Dimension(200,100));
frame.show();
for(;;)
    java.lang.Thread.sleep(1000);
Make executable using chmod +x AWTHelloWorldSimple.js and then run using ./AWTHelloWorldSimple.js. This was a useful reference: http://web.mit.edu/javascript/doc/rhino/scriptjava.html