javafx 2.0 with alternative languages - groovy, clojure, scala, fantom, and visage
DESCRIPTION
Presented at GeeCON 2011: JavaFX Script is going away, but the JavaFX Platform is getting a new face with pure Java APIs. In this session, you will see how you can leverage the new JavaFX 2.0 APIs from a host of different JVM languages, including JRuby, Clojure, Groovy, and Scala.TRANSCRIPT
![Page 1: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/1.jpg)
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage Stephen ChinChief Agile Methodologist, [email protected]: @steveonjava
![Page 2: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/2.jpg)
About the Presenter
Stephen Chin
Motorcyclist
Family ManChief Agile Methodologist, GXS
Author, Pro JavaFX Platform
Java Champion
OSCON Java Conference Chair
![Page 3: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/3.jpg)
Disclaimer: This is code-heavy
![Page 4: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/4.jpg)
JavaFX With Java
![Page 5: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/5.jpg)
Programming Languages
> JavaFX 2.0 APIs are now in Java Pure Java APIs for all of JavaFX Expose JavaFX Binding, Sequences as Java APIs
> Embrace all JVM languages JRuby, Clojure, Groovy, Scala Fantom, Mira, Jython, etc.
> JavaFX Script is no longer supported by Oracle Existing JavaFX Script based applications will
continue to run Visage is the open-source successor to the JavaFX
Script language
![Page 6: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/6.jpg)
JavaFX in Java
> JavaFX API follows JavaBeans approach
> Similar in feel to other UI toolkits (Swing, etc)
> Uses builder pattern to minimize boilerplate
![Page 7: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/7.jpg)
Example Application
public class HelloStage extends Application {
@Override public void start(Stage stage) { stage.setTitle("Hello Stage"); stage.setWidth(600); stage.setHeight(450);
Group root = new Group(); Scene scene = new Scene(root); scene.setFill(Color.LIGHTGREEN);
stage.setScene(scene); stage.setVisible(true); }
public static void main(String[] args) { Launcher.launch(HelloStage.class, args); }}
![Page 8: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/8.jpg)
Binding
> Unquestionably the biggest JavaFX Script innovation
> Supported via a PropertyBinding class
> Lazy invocation for high performance
> Static construction syntax for simple cases e.g.: bindTo(<property>)
![Page 9: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/9.jpg)
Observable Pseudo-Properties
> Supports watching for changes to properties
> Implemented via anonymous inner classes
> Will take advantage of closures in the future
![Page 10: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/10.jpg)
Observable Pseudo-Properties
final Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.hoverProperty().addListener(new ChangeListener<Boolean>() {
});
![Page 11: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/11.jpg)
Observable Pseudo-Properties
final Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.hoverProperty().addListener(new ChangeListener<Boolean>() {
});
The property we want to watch
![Page 12: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/12.jpg)
Observable Pseudo-Properties
final Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.hoverProperty().addListener(new ChangeListener<Boolean>() {
});
Only one listener used with generics to specify the data type
![Page 13: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/13.jpg)
Observable Pseudo-Properties
final Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.hoverProperty().addListener(new ChangeListener<Boolean>() { public void changed(ObservableValue<? extends Boolean> property, Boolean oldValue, Boolean value) {
}});
Refers to the Rectangle.hoverProperty()
![Page 14: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/14.jpg)
Observable Pseudo-Properties
final Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.hoverProperty().addListener(new ChangeListener<Boolean>() { public void changed(ObservableValue<? extends Boolean> property, Boolean oldValue, Boolean value) { rect.setFill(rect.isHover() ? Color.GREEN : Color.RED); }});
![Page 15: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/15.jpg)
Sequences in Java
> Replaced with an Observable List
> Public API is based on JavaFX sequences
> Internal code can use lighter collections API
> JavaFX 2.0 also has an Observable Map
![Page 16: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/16.jpg)
JavaFX With JRuby
![Page 17: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/17.jpg)
Why JRuby?
> Direct access to Java APIs
> Dynamic Typing
> Closures> ‘Closure conversion’ for interfaces
![Page 18: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/18.jpg)
Java in JRuby - Accessing Properties
timeline.setAutoReverse(true)timeline.autoReverse = truetimeline.auto_reverse = true
timeline.getKeyFrames().add(kf)timeline.key_frames.add(kf)timeline.key_frames.add kf
![Page 19: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/19.jpg)
JRuby Example 1: Simple Stage
require 'java'
Application = Java::javafx.application.ApplicationLauncher = Java::javafx.application.LauncherStage = Java::javafx.stage.StageScene = Java::javafx.scene.SceneColor = Java::javafx.scene.paint.Color
class HelloStage < Application
def start(stage) ..... endend
Launcher.launch(HelloStage.new);
stage.title = 'Hello Stage (JRuby)'stage.width = 600stage.height = 450
scene = Scene.newscene.fill = Color::LIGHTGREENstage.scene = scene
stage.visible = true;
![Page 20: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/20.jpg)
JRuby Example 2
rect = Rectangle.newrect.x = 25rect.y = 40rect.width = 100rect.height = 50rect.fill = Color::REDscene.content.add(rect)
timeline = Timeline.newtimeline.repeat_count = Timeline::INDEFINITEtimeline.auto_reverse = truekv = KeyValue.new(rect, Rectangle::X, 200);kf = KeyFrame.new(Duration.valueOf(500), kv);timeline.key_frames.add kf;timeline.play();
![Page 21: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/21.jpg)
JRuby Closure Conversion
rect.hoverProperty.addListener() do |prop, oldVal, newVal| rect.fill = rect.hover ? Color::GREEN : Color::RED;end
21
![Page 22: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/22.jpg)
JRuby Swiby
require 'swiby'
class HelloWorldModel attr_accessor :sayingendmodel = HelloWorldModel.new model.saying = "Hello World"
Frame { title "Hello World“ width 200 content { Label { text bind(model,:saying) } } visible true}
22
![Page 23: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/23.jpg)
23
JavaFX With Clojure
Artwork by Augusto Sellhorn http://sellmic.com/
![Page 24: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/24.jpg)
24
A Little About Clojure
> Started in 2007 by Rich Hickey> Functional Programming Language> Derived from LISP> Optimized for High Concurrency
> … and looks nothing like Java!
(def hello (fn [] "Hello world"))(hello)
![Page 25: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/25.jpg)
Clojure Syntax in One Slide
Symbols
> numbers – 2.178> ratios – 355/113> strings – “clojure”, “rocks”> characters – \a \b \c \d> symbols – a b c d> keywords – :alpha :beta> boolean – true, false> null - nil
Collections(commas optional)
> Lists(1, 2, 3, 4, 5)> Vectors[1, 2, 3, 4, 5]> Maps{:a 1, :b 2, :c 3, :d 4}> Sets#{:a :b :c :d :e}
25
(plus macros that are syntactic sugar wrapping the above)
![Page 26: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/26.jpg)
Clojure GUI Example
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true)))(javafxapp)
26
![Page 27: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/27.jpg)
Clojure GUI Example
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true)))(javafxapp)
27
Create a Function for the Application
![Page 28: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/28.jpg)
Clojure GUI Example
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true)))(javafxapp)
28
Initialize the Stage and Scene Variables
![Page 29: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/29.jpg)
Clojure GUI Example
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true)))(javafxapp)
29
Call Setter Methods on Scene and Stage
![Page 30: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/30.jpg)
Clojure GUI Example
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true)))(javafxapp)
30
Java Constant Syntax
Java Method Syntax
![Page 31: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/31.jpg)
Simpler Code Using doto
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (doto scene (.setFill Color/LIGHTGREEN)) (doto stage (.setWidth 600) (.setHeight 450) (.setScene scene) (.setVisible true))))(javafxapp)
31
![Page 32: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/32.jpg)
Simpler Code Using doto
(defn javafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (doto scene (.setFill Color/LIGHTGREEN)) (doto stage (.setWidth 600) (.setHeight 450) (.setScene scene) (.setVisible true))))(javafxapp)
32
doto form: (doto symbol (.method params)) equals: (.method symbol params)
![Page 33: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/33.jpg)
Refined Clojure GUI Example
(defn javafxapp [] (doto (Stage. "JavaFX Stage") (.setWidth 600) (.setHeight 450) (.setScene (doto (Scene.) (.setFill Color/LIGHTGREEN) (.setContent (list (doto (Rectangle.) (.setX 25) (.setY 40) (.setWidth 100) (.setHeight 50) (.setFill Color/RED)))))) (.setVisible true)))(javafxapp)
33
![Page 34: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/34.jpg)
Refined Clojure GUI Example
(defn javafxapp [] (doto (Stage. "JavaFX Stage") (.setWidth 600) (.setHeight 450) (.setScene (doto (Scene.) (.setFill Color/LIGHTGREEN) (.setContent (list (doto (Rectangle.) (.setX 25) (.setY 40) (.setWidth 100) (.setHeight 50) (.setFill Color/RED)))))) (.setVisible true)))(javafxapp)
34
Let replaced with inline declarations
![Page 35: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/35.jpg)
Refined Clojure GUI Example
(defn javafxapp [] (doto (Stage. "JavaFX Stage") (.setWidth 600) (.setHeight 450) (.setScene (doto (Scene.) (.setFill Color/LIGHTGREEN) (.setContent (list (doto (Rectangle.) (.setX 25) (.setY 40) (.setWidth 100) (.setHeight 50) (.setFill Color/RED)))))) (.setVisible true)))(javafxapp)
35
Doto allows nested data structures
![Page 36: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/36.jpg)
Refined Clojure GUI Example
(defn javafxapp [] (doto (Stage. "JavaFX Stage") (.setWidth 600) (.setHeight 450) (.setScene (doto (Scene.) (.setFill Color/LIGHTGREEN) (.setContent (list (doto (Rectangle.) (.setX 25) (.setY 40) (.setWidth 100) (.setHeight 50) (.setFill Color/RED)))))) (.setVisible true)))(javafxapp)
36
Now a nested Rectangle fits!
![Page 37: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/37.jpg)
Closures in Clojure
37
> Inner classes can be created using proxy
(.addListener hoverProperty (proxy [ChangeListener] [] (handle [p, o, v] (.setFill rect (if (.isHover rect) Color/GREEN Color/RED)))))
![Page 38: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/38.jpg)
Closures in Clojure
> Inner classes can be created using proxy
38
(.addListener hoverProperty (proxy [ChangeListener] [] (handle [p, o, v] (.setFill rect (if (.isHover rect) Color/GREEN Color/RED)))))
Proxy form: (proxy [class] [args] fs+) f => (name [params*] body)
![Page 39: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/39.jpg)
JavaFX With Groovy
![Page 40: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/40.jpg)
Features of Groovy
> Tight integration with Java Very easy to port from Java to Groovy
> Declarative syntax Familiar to JavaFX Script developers
> Builders
![Page 41: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/41.jpg)
Example 1: Simple FX Script to Groovy
![Page 42: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/42.jpg)
Step 1: Lazy conversion to Groovy
class HelloStage extends Application {
void start(stage) { stage.setTitle("Hello Stage (Groovy)“); stage.setWidth(600); stage.setHeight(450);
Scene scene = new Scene(); scene.setFill(Color.LIGHTSKYBLUE); stage.setScene(scene);
stage.setVisible(true); }
static void main(args) { Launcher.launch(HelloStage.class, args); }}
![Page 43: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/43.jpg)
Step 2: Slightly More Groovy
class HelloStage extends Application {
void start(stage) { new Stage( title: "Hello Stage (Groovy)", width: 600, height: 450, visible: true, scene: new Scene( fill: Color.LIGHTSKYBLUE, ) ); }
static void main(args) { Launcher.launch(HelloStage.class, args); }}
![Page 44: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/44.jpg)
Slight Aside: Groovy Builders
> Groovy builders make writing custom DSLs easy
> For the next slide, I am using a builder I defined
> Hopefully the community will improve upon this
![Page 45: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/45.jpg)
Step 3: Using a Groovy Builder
FxBuilder.build { stage = stage( title: "Hello World", width: 600, height: 450, scene: scene(fill: Color.LIGHTSKYBLUE) { ... } )
stage.visible = true;}
![Page 46: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/46.jpg)
Step 4: With Content
FxBuilder.build { stage = stage( title: "Hello Rectangle (Groovy FxBuilder 2)", width: 600, height: 450, scene: scene(fill: Color.LIGHTSKYBLUE) { rectangle( x: 25, y: 40, width: 100, height: 50, fill: Color.RED ) } )
stage.visible = true;}
![Page 47: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/47.jpg)
Example 2: FX Script Animation in Groovy
![Page 48: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/48.jpg)
Step 1: JavaFX Script
def timeline = Timeline { repeatCount: Timeline.INDEFINITE autoReverse: true keyFrames: [ KeyFrame { time: 750ms values : [ rect1.x => 200.0 tween Interpolator.LINEAR, rect2.y => 200.0 tween Interpolator.LINEAR, circle1.radius => 200.0 tween Interpolator.LINEAR ] } ];}
timeline.play();
![Page 49: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/49.jpg)
Step 1a: JavaFX Script Simplificationdef timeline = Timeline { repeatCount: Timeline.INDEFINITE autoReverse: true keyFrames: at (750ms) { rect1.x => 200.0 tween Interpolator.LINEAR; rect2.y => 200.0 tween Interpolator.LINEAR; circle1.radius => 200.0 tween Interpolator.LINEAR; }}
timeline.play();
![Page 50: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/50.jpg)
Step 2: Java-ish Groovy Animations
final Timeline timeline = new Timeline( repeatCount: Timeline.INDEFINITE, autoReverse: true)
final KeyValue kv1 = new KeyValue (rect1.x(), 200);final KeyValue kv2 = new KeyValue (rect2.y(), 200);final KeyValue kv3 = new KeyValue (circle1.radius(), 200);
final KeyFrame kf = new KeyFrame(Duration.valueOf(750), kv1, kv2, kv3);
timeline.getKeyFrames().add(kf);
timeline.play();
![Page 51: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/51.jpg)
Step 3: JavaFX Animation Groovy DSL (courtesy of Jim Clarke – work in progress)
timeline = timeline(repeatCount: Timeline.INDEFINITE, autoReverse: true) {
at 750.ms update values { change rect1.y() to 200 change rect2.x() to 200 change circle.radius() to 200 }}
timeline.play();
![Page 52: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/52.jpg)
Groovy Closures - With interface coercion
def f = { p, o, v -> rect.setFill(rect.isHover() ? Color.GREEN : Color.RED);} as ChangeListener;
rect.hoverProperty().addListener(f);
![Page 53: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/53.jpg)
53
JavaFX With Scala
![Page 54: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/54.jpg)
54
What is Scala
> Started in 2001 by Martin Odersky> Compiles to Java bytecodes> Pure object-oriented language> Also a functional programming language
2001• Scala Started
2003/2004• Scala v1.0
2006• Scala v2.0
2011• Scala 2.9.0 (latest)
![Page 55: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/55.jpg)
55
Why Scala?
> Shares many language features with JavaFX Script that make GUI programming easier: Static type checking – Catch your errors at compile
time Closures – Wrap behavior and pass it by reference Declarative – Express the UI by describing what it
should look like> Scala also supports DSLs!
![Page 56: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/56.jpg)
Java vs. Scala DSLpublic class HelloStage extends Application
{
public void start(Stage stage) { stage.setTitle("Hello Stage"); stage.setWidth(600); stage.setHeight(450); Scene scene = new Scene(); scene.setFill(Color.LIGHTGREEN); Rectangle rect = new Rectangle(); rect.setX(25); rect.setY(40); rect.setWidth(100); rect.setHeight(50); rect.setFill(Color.RED); stage.add(rect); stage.setScene(scene); stage.setVisible(true); }
public static void main(String[] args) { Launcher.launch(HelloStage.class, args); }}
object HelloJavaFX extends JavaFXApplication {
def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
56
21 Lines541 Characters
17 Lines324 Characters
![Page 57: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/57.jpg)
object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
57
![Page 58: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/58.jpg)
58
object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
Base class for JavaFX applications
![Page 59: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/59.jpg)
59
object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
Declarative Stage definition
![Page 60: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/60.jpg)
60
object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
Inline property definitions
![Page 61: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/61.jpg)
61
object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } }}
List Construction Syntax
![Page 62: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/62.jpg)
Animation in Scala
def timeline = new Timeline { repeatCount = INDEFINITE autoReverse = true keyFrames = List( new KeyFrame(time: 50) { values = List( new KeyValue(rect1.x() -> 300), new KeyValue(rect2.y() -> 500), new KeyValue(rect2.width() -> 150) ) } ) }
62
![Page 63: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/63.jpg)
def timeline = new Timeline { repeatCount = INDEFINITE autoReverse = true keyFrames = List( new KeyFrame(time: 50) { values = List( new KeyValue(rect1.x() -> 300), new KeyValue(rect2.y() -> 500), new KeyValue(rect2.width() -> 150) ) } ) }
Animation in Scala
63
Duration set by Constructor Parameter
![Page 64: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/64.jpg)
Animation in Scala
64
Operator overloading for animation syntax
def timeline = new Timeline { repeatCount = INDEFINITE autoReverse = true keyFrames = List( new KeyFrame(time: 50) { values = List( new KeyValue(rect1.x() -> 300), new KeyValue(rect2.y() -> 500), new KeyValue(rect2.width() -> 150) ) } ) }
![Page 65: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/65.jpg)
Closures in Scala
65
> Closures are also supported in Scala> And they are 100% type-safe
rect.hoverProperty().addListener((p, o, v) => { rect.fill = if (rect.hover) Color.GREEN else Color.RED
})
![Page 66: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/66.jpg)
Closures in Scala
> Closures are also supported in Scala> And they are 100% type-safe
66
Compact syntax(params) => {body}
rect.hoverProperty().addListener((p, o, v) => { rect.fill = if (rect.hover) Color.GREEN else Color.RED
})
![Page 67: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/67.jpg)
Other JVM Languages to Try
> Jython Started by Jim Hugunin High Performance Python
> Mirah Invented by Charles Nutter Originally called Duby Local Type Inference, Static and Dynamic Typing
> Fantom Created by Brian and Andy Frank Originally called Fan Built-in Declarative Syntax Portable to Java and .NET Local Type Inference, Static and Dynamic Typing
67
![Page 68: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/68.jpg)
Fantom Code Example
Void main() { Stage { title = "Hello Stage" width = 600 height = 450 Scene { fill = Color.LIGHTGREEN Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED } } }.open}
68
![Page 69: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/69.jpg)
timeline := Timeline { repeatCount = Timeline.INDEFINITE autoReverse = true KeyFrame { time = 50ms KeyValue(rect1.x() -> 300), KeyValue(rect2.y() -> 500), KeyValue(rect2.width() -> 150) }}
Animation in Fantom
69
Fantom has a built-in Duration type
And also supports operator overloading
![Page 70: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/70.jpg)
Announcing Project Visage
70
> Visage project goals: Compile to JavaFX Java APIs Evolve the Language (Annotations, Maps, etc.) Support Other Toolkits
> Come join the team!> For more info: http://visage-lang.org/
> “Visage is a domain specific language (DSL) designed for the express purpose of writing user interfaces.”
![Page 71: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/71.jpg)
How about JavaFX on… Visage
Stage { title: "Hello Stage" width: 600 height: 450 scene: Scene { fill: Color.LIGHTGREEN content: Rectangle { x: 25 y: 40 width: 100 height: 50 fill: Color.RED } }}
71
![Page 72: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/72.jpg)
How about JavaFX on… Visage
Stage { title: "Hello Stage" width: 600 height: 450 scene: Scene { fill: Color.LIGHTGREEN content: Rectangle { x: 25 y: 40 width: 100 height: 50 fill: Color.RED } }}
72
![Page 73: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/73.jpg)
How about JavaFX on… Visage
Stage { title: "Hello Stage" width: 600 height: 450 Scene { fill: Color.LIGHTGREEN Rectangle { x: 25 y: 40 width: 100 height: 50 fill: Color.RED } }}
73
![Page 74: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/74.jpg)
Conclusion
> You can write JavaFX applications in pure Java> JavaFX is also usable in alternate languages> Over time improved support is possible
Groovy Builders, Scala DSL, Visage
Remember: This is a proof of concept only – you can not leave this session and do this today.
![Page 75: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/75.jpg)
JavaFX 2.0 Product Timeline
CYQ1 2011
JavaFX 2.0 EA(Early Access)
CYQ2 2011 CYQ3 2011
JavaFX 2.0 Beta JavaFX 2.0 GA(General Availability)
Copyright 2010 Oracle
JavaFX Beta in Late May!
![Page 76: JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage](https://reader036.vdocument.in/reader036/viewer/2022062312/5550edf0b4c905387d8b5127/html5/thumbnails/76.jpg)
Pro JavaFX 2 Platform Coming Soon!
> Coming 2nd half of this year> All examples rewritten in Java> Will cover the new JavaFX 2.0
APIs
76