Java Reference
In-Depth Information
Groovy is a great choice for those getting started with JVM languages, because its syntax most closely matches the
Java language. In fact, with a few minor changes all Java programs are also valid Groovy programs! However, to get
the full advantage of the language features in Groovy you need to make use of a DSL written for your target use case,
such as GroovyFX. GroovyFX is an open source project that lets you write JavaFX code in the Groovy language that
looks like the code in Listing 13-2.
Listing 13-2. Vanishing Circles Application Written in Groovy Using the GroovyFX DSL
GroovyFX.start { primaryStage ->
def sg = new SceneGraphBuilder()
def rand = new Random().&nextInt
def circles = []
sg.stage(title: 'Vanishing Circles', show: true) {
scene(fill: black, width: 800, height: 600) {
50.times {
circles << circle(centerX: rand(800), centerY: rand(600), radius: 150, stroke: white,
strokeWidth: bind('hover', converter: {val -> val ? 4 : 0})) {
fill rgb(rand(255), rand(255), rand(255), 0.2)
effect boxBlur(width: 10, height: 10, iterations: 3)
onMouseClicked { e ->
timeline {
at(3.s) { change e.source.radiusProperty() to 0 }
}.play()
}
}
}
}
timeline(cycleCount: Timeline.INDEFINITE, autoReverse: true) {
circles.each { circle ->
at (40.s) {
change circle.centerXProperty() to rand(800)
change circle.centerYProperty() to rand(600)
}
}
}.play()
}
}
This GroovyFX code has the same functionality as the earlier JavaFX examples, but is significantly shorter
and more expressive. Also, as your application grows you will get even more benefit from using a DSL such as this,
allowing you to write more complex and feature-rich applications with less code.
Some of the Groovy language features that this code takes advantage of include the following:
Groovy builder pattern : Groovy makes it particularly easy to build powerful and concise
builder code, as demonstrated with the GroovyFX SceneGraphBuilder.
Named parameters : Remembering the order of arguments to methods and constructors with
long argument lists is hard, but named parameters allow you to be explicit and change the
order for your convenience.
 
Search WWH ::




Custom Search