Wednesday, October 17, 2012

Getting ScalaFX 2.2 demo up and running on OS X (with Oracle Java 7u7)

News 20-Oct-2012 - JDK 7u9 and a JavaFX 2.2.3 samples are out:


Use those files instead of the 7u7 mentioned below. The performance of some JavaFX Ensemble demos is now way better on the NVidia 9400 GPU (but not all). Also, there are new demos included in the bundle.



JavaFX is a 2d/3d GPU-accelerated graphics library that is going to be replacing the Swing as the main Java application UI library.

ScalaFX is a Scala language binding to JavaFX.

JavaFX development kit is distributed as an integral part of the general Java 7.x development kit (JDK), by Oracle.

Below are the steps to get a basic ScalaFX project started, developing on OS X.

You will need:
- OS X 10.7.3 or later ("Lion" or later) (a JDK 7u7 requirement)
- Basic experience on command line usage on OS X
- HomeBrew (or similar) command line tool installation system ('brew' is great and easy to install)
- Willingness to accept the Oracle 7.x JDK license agreement



Getting ScalaFX and JavaFX 2.1 running on OS X

Java SE 7 Update 7 (or later)

Download and install "Java SE 7u7" (or later) from Oracle:

Take these files:
jdk-7u7-macosx-x64.tar.gz
javafx_samples-2_2_0-macosx-universal.zip

The JDK (development kit) contains the JRE (runtime engine) so just a single install is sufficient.

Installation FAQ:

Note for users with Apple Java 6:

JDK 7 Installation for Mac OS X:

In short, just install the .pkg file by double-clicking. It will not replace the Apple Java 6 (in case you have Java enabled). You will need Java 7 for JavaFX 2.2 to function on OS X.

<<
You can determine which version of the JDK is the default by typing java -version in a Terminal window. If the installed version is 7u6, you will see a string that includes the text 1.7.0_06. For example:
    % java -version
    java version "1.7.0_06-ea"
    Java(TM) SE Runtime Environment (build 1.7.0_06-ea-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 23.2-b04, mixed mode)
To run a different version of Java, either specify the full path, or use the java_home tool:
    /usr/libexec/java_home -v 1.7.0_06 --exec javac -version
<<

JavaFX samples

Likely you already downloaded these (see URL and filename above).

Extract the files to some suitable directory.

To launch the .jar files, right-click and select 'Open in Jar Launcher > Run (or Open or something)'. This seems the way to bypass safety system preventing to launch .jar by a simple double click.

On OS X, they don't (yet) support launching from the HTML files. Ignore them. )

The UI responsiveness may be sluggish at times. This may be because of lack of optimization for hardware (GPU) rendering. Oracle site says any GPU supported by Lion should do, but in practice my MacBook Air (integrated Core i7 Intel GPU) performs way better than 2009 Mac Mini with NVidia. Likely the situation will improve once Oracle gets more OS X tuning done. Hope so.


BrickBraker.jar

Nice retro game.



Ensamble.jar

Try making this into full screen mode (the '+' icon at upper right). Unfortunately it seems the 'real' full screen mode (Apple-F) is not supported by JavaFX (or this demo)?

The 3D-xylophone was probably hot in (…no, it never was). Now it mainly displays the lack of antialiasing in its rendering.


"Advanced media" demo looks like animated gifs when played in full-screen on my Mini (but good on MacBook Air).

"Audio spectrum data" demos are great - check the lyrics! :)

The song is available also in Youtube.


ScalaFX compilation


First, get 'hg' (Mercurial) and 'sbt' to the Mac (i.e. using HomeBrew):

    $ brew install hg sbt

This makes a copy of 'scalafx' repository (to local folder 'scalafx'):

    $ hg clone https://code.google.com/p/scalafx/

The instructions in  'README.txt' are outdated. Do this instead:

    $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/
    $ sbt clean compile package

(without the 'package' sbt does not create the target/*.jar file)

To launch a demo of fading circles (makes other things on your computer crawl so don't leave it on :) ):

    $ sbt run





Making your own project

Create these files:

    build.sbt
    lib/
        scalafx-1.0-SNAPSHOT.jar
    src/
        mydemo.scala

'build.sbt' you can adopt from the one used in ScalaFX samples.

'lib/scalafx-1.0-SNAPSHOT.jar' is a copy from the 'target' directory of scalafx compilation.

'mydemo.scala' is whatever file you wish to use (again, something from the scalafx 'demo' directory would be fine).

    $ sbt run

Please make a comment if you got things working, with this walkthrough.


No comments: