On May 26, 2011, Oracle released the beta SDK for JavaFX 2.0. The major difference between previous versions is that it provides Java APIs, allowing us to use JavaFX from normal Java applications. Ok, using JavaFX from Java is good, but what about Scala? Scala programs run on the same JVM as Java, so there should be no problems to use JavaFX from.
To prove that it is possible to use JavaFX from Scala, we will try to clone Oracle’s Getting Started with Java FX application. I think you already have Scala IDE or Scala Plugin for your favorite IDE? For this experiment I will use Eclipse Indigo + Scala IDE for Eclipse + Maven Integration (available in Eclipse Marketplace).
Skeleton
Ok, let’s start. First step in will be to create an empty Scala project managed by Maven. There is already an archetype for such projects - scala-archetype-simple. All we have to do, is to fill project parameters.
Good. Project was created, but Maven created a Java project with Scala sources... that’s not good. All you have to do to convert Java project into Scala is to right-click on project in Package Explorer and select “Configure > Add Scala Nature” option from menu.
Great! Our Scala project managed by Maven is ready. Main object is found in App.scala file. Try and run it. The all-known “Hello World!” message is shown :)
Sugar
Scala + Maven is good, but not good enough. Out patient is Java FX 2.0. There is no Maven repository with Java FX 2.0 runtime libraries yet. In this case we will use local repository for this.
Before adding Maven dependency into pom.xml, an environment variable JAVAFX_HOME, pointing to the root directory where Java FX 2.0 SDK was installed.
Now, we can add Java FX 2.0 Runtime dependency into pom.xml:
<dependency> <groupid>javafx</groupid> <artifactid>javafxrt</artifactid> <version>2.0-SNAPSHOT</version> <scope>system</scope> <systempath>${env.JAVAFX_HOME}/rt/lib/jfxrt.jar</systempath> </dependency>Ready? Sure? Then we have JavaFX Runtime in our project too. Nothing can stop us now!
First steps
Doctor, patient is ready for intervention! Let’s create a blank JavaFX scene step-by-step.
A JavaFX application should extend Application class... ok, let’s extend it - using Scala. Create a new scala class that extend javafx.application.Application. The only method we need to override is def start(stage: Stage): Unit. Here is the code:
import javafx.stage.Stage import javafx.application.Application class JavaFXApplication extends Application { def start(stage: Stage): Unit = { } }Nothing difficult yet... nor later :) Each Stage should have a Scene, and each Scene should have a Group, and each Group... about each Group we will talk later. Right now let’s add to application’s stage an empty Scene with an empty Group:
val group = new Group val scene = new Scene(group, 800, 600, Color.BLACK) stage.setScene(scene) stage.setVisible(true)As simple as that!
Now, it is time to see the results. JavaFX application is launched using Application.launch() static method. Let’s edit our main object (App.scala) so we can launch JavaFXApplication class:
def main(args : Array[String]) { Application.launch(classOf[JavaFXApplication], args) }Yes, that’s all! :) Now let’s run our Scala application and see our black window.
For today it’s enough. In the next post we will add graphics, effects and animation, so our clone will be identical to Oracle’s :)
No comments:
Post a Comment