ScalaFX version of GroovyFX demo

by 3/05/2012 11:25:00 AM 2 comments
So GroovyFX, a DSL for creating JavaFX applications in Groovy, is out. There's a nice little snippet on the front page of the http://groovyfx.org for creating a logo application. I figured I'd take a quick stab at writing it in ScalaFX.

Hello, World ... UPDATED on March, 22, 2012

A simple and complete "Hello, World" program is show below. I tried to follow the general usage of ScalaFX. This version is updated from my original post using the latest ScalaFX code, which now includes classes that were previously missing (i.e. the Text and DropShadow classes).
import javafx.scene.paint.Color._
import javafx.scene.paint.LinearGradientBuilder
import scala.collection.JavaConverters._
import scalafx.application.JFXApp
import scalafx.geometry.Insets
import scalafx.Includes._
import scalafx.scene.effect.DropShadow
import scalafx.scene.effect.DropShadow._
import scalafx.scene.layout.HBox
import scalafx.scene.layout.HBox._
import scalafx.scene.paint.Stops
import scalafx.scene.Scene
import scalafx.scene.text.Text
import scalafx.scene.text.Text._
import scalafx.stage.Stage


object HelloScalaFX extends JFXApp {

    stage = new Stage {
        title = "ScalaFX Hello World"
        width = 650
        height = 450
        scene = new Scene {
            fill = BLACK
            root = new HBox {
                padding = Insets(80)

                children += new Text("Scala") {
                    style = "-fx-font-size: 80pt"
                    fill = LinearGradientBuilder.create().endX(0).
                            stops(Stops(PALEGREEN, SEAGREEN).asJava).build()
                }

                children += new Text("FX") {
                    style = "-fx-font-size: 80pt"
                    fill = LinearGradientBuilder.create().endX(0).
                            stops(Stops(CYAN, DODGERBLUE).asJava).build()
                    effect = new DropShadow {
                        radius = 25
                        spread = 0.25
                        color = DODGERBLUE
                    }
                }

            }
        }

    }
}

Hello, World - ORIGINAL POST

A simple and complete "Hello, World" program is show below. I tried to follow the general usage of ScalaFX with a few notable digressions. ScalaFX is still a work in progress so there isn't a Scala version of all JavaFX components. For example, I had to use the straight JavaFX versions of LinearGradientBuilder, DropShadow, and Text
import javafx.scene.effect.{DropShadow}
import javafx.scene.paint.Color._
import javafx.scene.paint.{LinearGradientBuilder}
import javafx.scene.text.Text
import scala.collection.JavaConverters._
import scalafx.application.JFXApp
import scalafx.geometry.Insets
import scalafx.Includes._
import scalafx.scene.layout.HBox
import scalafx.scene.layout.HBox._
import scalafx.scene.paint.Stops
import scalafx.scene.Scene
import scalafx.stage.Stage

object HelloScalaFX extends JFXApp {

    stage = new Stage {
        title = "ScalaFX Hello World"
        width = 650
        height = 450
        scene = new Scene {
            fill = BLACK
            root = new HBox {
                padding = Insets(80)

                val text0 = new Text("Scala")
                text0.setStyle("-fx-font-size: 80pt")
                text0.setFill(LinearGradientBuilder.create().endX(0).stops(
                    Stops(PALEGREEN, SEAGREEN).asJava).build())
                children += text0

                val text1 = new Text("FX")
                text1.setStyle("-fx-font-size: 80pt")
                text1.setFill(LinearGradientBuilder.create().endX(0).stops(
                    Stops(CYAN, DODGERBLUE).asJava).build())
                text1.setEffect(new DropShadow {
                    setRadius(25)
                    setSpread(0.25)
                    setColor(DODGERBLUE)
                })
                children += text1
            }
        }
    }
}

Brian Schlining

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

2 comments:

Stephen Chin said...

Brian -- I added this example to the ScalaFX demo package and did a few more refinements (including using a new LinearGradient class:
http://code.google.com/p/scalafx/source/browse/demo/scalafx/HelloScalaFX.scala

If you have any other ideas on how to improve the APIs to make coding ScalaFX applications easier, let us know.

Brian Schlining said...

Hi Stephen, that's great! I hope to use ScalaFX in a project for work next week. Once I use it 'in anger' I'll have a better idea of ApI improvements. I'll be sure to let you know. BTW, love the project, thanks for all your hard work.