Java Reference
In-Depth Information
As you can see, the ScalaFX variant is much more concise and readable even in this simple example. The basic
syntax for animations in ScalaFX is:
at (duration) {[property -> value]}
This statement translates to a KeyFrame that can be added to a Timeline directly. You can pass in multiple
KeyFrames to a Timeline or use multiple property->value pairs in the preceding syntax, giving you the flexibility to
specify arbitrarily complex animations.
Breaking down this example further, notice that we used a shortcut syntax to create the Duration and the
KeyValue . For the former, ScalaFX has an implicit conversion that adds a function to Doubles for ms() , s() , m() , and
h() , allowing you to create a new Duration simply by calling the respective function. By using the postfix operator
shortcut syntax in Scala, rather than calling “ 3.s() ” or “ 3.s ,” you can further abbreviate it to “ 3 s ” (where the space
between 3 and s is required).
For the latter, ScalaFX properties have an overloaded operator of “->” that takes a value and returns a KeyValue
object. In fact, not only can you specify the target value, but you can also add in a tween expression as shown here:
radius -> 0 tween EASE_OUT
This addition causes the animation to slow down as it approaches the end, resulting in a smoother transition.
Summary
As we have shown you in this chapter, you have a lot more options for writing JavaFX code than just using the Java
language. There are already several DSLs available written in popular JVM languages such as Groovy and Scala.
The great thing is that you have the choice to use the language and markup that best suits your project needs. All
of these technologies integrate cleanly with JavaFX code written in Java, and have their own benefits that you can take
advantage of based on the needs of your project.
Resources
For more information about Groovy and GroovyFX, consult the following resources.
Groovy Home Page:
http://groovy.codehaus.org/
GroovyFX Home Page:
http://groovyfx-project.github.com/
GroovyFX Announcement Blog Entry:
http://pleasingsoftware.blogspot.com/2011/08/
introducing-groovyfx-it-about-time.html
Additional information on Scala and ScalaFX can be found here:
Scala Home Page:
http://www.scala-lang.org/
ScalaFX Home Page:
http://code.google.com/p/scalafx/
ScalaFX Announcement Blog Entry:
http://javafx.steveonjava.com/javafx-2-0-and-
scala-like-milk-and-cookies/
 
Search WWH ::




Custom Search