javafx your way: building javafx applications with alternative languages
DESCRIPTION
JavaFX is more than a language. It is also a platform for building immersive applications with graphics, animation, and rich media. In this session, you will see how you can leverage JavaFX from a host of different JVM languages, including Java, JRuby, Groovy, Scala, and Clojure.TRANSCRIPT
![Page 1: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/1.jpg)
JavaFX Your WayBuilding JavaFX Applications with Alternative Languages
Stephen ChinGXS
tweet: @steveonjava
Jonathan GilesOracle
[email protected]: @jonathangiles
![Page 2: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/2.jpg)
JavaFX Your WayBuilding JavaFX Applications with Alternative Languages
Stephen ChinGXS
tweet: @steveonjava
Jonathan GilesOracle
[email protected]: @jonathangiles
![Page 3: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/3.jpg)
Meet the Presenters…
Steve Jonathan
3
Motorcyclist
Family Man
![Page 4: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/4.jpg)
Disclaimer: This is proof of concept
THE FOLLOWING IS INTENDED TO OUTLINE OUR GENERAL
![Page 5: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/5.jpg)
THE FOLLOWING IS INTENDED TO OUTLINE OUR GENERAL PRODUCT DIRECTION. IT IS INTENDED FOR INFORMATION PURPOSES ONLY, AND MAY NOT BE INCORPORATED INTO ANY CONTRACT. IT IS NOT A COMMITMENT TO DELIVER ANY MATERIAL, CODE, OR FUNCTIONALITY, AND SHOULD NOT BE RELIED UPON IN MAKING PURCHASING DECISION. THE DEVELOPMENT, RELEASE, AND TIMING OF ANY FEATURES OR FUNCTIONALITY DESCRIBED FOR ORACLE'S PRODUCTS REMAINS AT THE SOLE DISCRETION OF ORACLE.
![Page 6: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/6.jpg)
Disclaimer #2: This is code-heavy
THE FOLLOWING IS INTENDED TO STIMULATE CREATIVE USE OF
![Page 7: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/7.jpg)
Overall Presentation Goal
Demonstrate the future potential of the JavaFX platform.
![Page 8: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/8.jpg)
Agenda
> Catch up on latest news
> JavaFX in Java
> Explore alternative languages
JRuby
Clojure
Groovy
Scala
![Page 9: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/9.jpg)
Todays News
• JavaFX Script no longer required to write JavaFXapplications
• Benefits:
– Easier integration with business logic on JVM
– Access to generics, annotations, (closures), etc
– Java has great IDE support
• Downsides:
– JavaFX Script was kind to us
![Page 10: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/10.jpg)
JavaFX With Java
![Page 11: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/11.jpg)
JavaFX in Java
> JavaFX API follows JavaBeans approach
> Similar in feel to other UI toolkits (Swing, etc)
> Researching approaches to minimize boilerplate
![Page 12: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/12.jpg)
Binding
> Unquestionably the biggest JavaFX Script innovation
> Researching ways to incorporate into Java
> Will not be as succinct as JavaFX Script
> Genius‟ are deployed in underground labs pondering this right now
![Page 13: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/13.jpg)
Observable Pseudo-Properties
> Supports watching for changes to properties
> Implemented via anonymous inner classes
> Maybe closures in the future
![Page 14: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/14.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {
});
![Page 15: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/15.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {
});
Before: „addChangingListener‟
After: „addChangedListener‟
![Page 16: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/16.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {
});
The property we are wanting to
watch
![Page 17: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/17.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {
});
Listener for each primitive type,
and Objects in general
![Page 18: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/18.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {public void handle(Bean bean, PropertyReference pr, boolean oldHover) {
}});
Rectangle is a Bean
![Page 19: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/19.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {public void handle(Bean bean, PropertyReference pr, boolean oldHover) {
}});
Refers to the
Rectangle.hover „property‟
![Page 20: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/20.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {public void handle(Bean bean, PropertyReference pr, boolean oldHover) {
}});
For performance reasons,
this is the old value.
![Page 21: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/21.jpg)
Observable Pseudo-Properties
Rectangle rect = new Rectangle();rect.setX(40);rect.setY(40);rect.setWidth(100);rect.setHeight(200);
rect.addChangedListener(Rectangle.HOVER, new BooleanListener() {public void handle(Bean bean, PropertyReference pr, boolean oldHover) {rect.setFill(rect.isHover() ? Color.GREEN : Color.RED);
}});
![Page 22: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/22.jpg)
Sequences in Java
> Sequence class available
Essentially an observable List
> Public API is still sequence-based
> Internal code can use lighter collections API
![Page 23: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/23.jpg)
Example Application
public class HelloStage implements Runnable {
public void run() {Stage stage = new Stage();stage.setTitle("Hello Stage");stage.setWidth(600);stage.setHeight(450);
Scene scene = new Scene();scene.setFill(Color.LIGHTGREEN);
stage.setScene(scene);stage.setVisible(true);
}
public static void main(String[] args) {FX.start(new HelloStage());
}}
![Page 24: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/24.jpg)
Summary
> The JVM has a modern UI toolkit coming to it
> Total port to Java – no hacks or kludges
> Many languages to choose from
> Alternate languages == exciting possibilities
> Choose the best language for your needs
![Page 25: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/25.jpg)
Major Question
28
How can alternative languages make developing
JavaFX user interfaces easier & more productive?
![Page 26: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/26.jpg)
JavaFX With JRuby
![Page 27: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/27.jpg)
Why JRuby?
> Direct access to Java APIs
> Dynamic Typing
> Closures
> „Closure conversion‟ for interfaces
![Page 28: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/28.jpg)
Java in JRuby- Accessing Properties
timeline.setAutoReverse(true)
timeline.autoReverse = true
timeline.auto_reverse = true
timeline.getKeyFrames().add(kf)
timeline.key_frames.add(kf)
timeline.key_frames.add kf
![Page 29: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/29.jpg)
JRuby Example 1: Simple Stage
require 'java'
FX = Java::javafx.lang.FX
Stage = Java::javafx.stage.Stage
Scene = Java::javafx.scene.Scene
Color = Java::javafx.scene.paint.Color
class HelloStage
include java.lang.Runnable
def run
.....
end
end
FX.start(HelloStage.new);
stage = Stage.newstage.title = 'Hello Stage (JRuby)'stage.width = 600stage.height = 450
scene = Scene.newscene.fill = Color::LIGHTGREENstage.scene = scene
stage.visible = true;
![Page 30: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/30.jpg)
JRuby Example 2
rect = Rectangle.new
rect.x = 25
rect.y = 40
rect.width = 100
rect.height = 50
rect.fill = Color::RED
scene.content.add(rect)
timeline = Timeline.new
timeline.repeat_count = Timeline::INDEFINITE
timeline.auto_reverse = true
kv = KeyValue.new(rect, Rectangle::X, 200);
kf = KeyFrame.new(Duration.valueOf(500), kv);
timeline.key_frames.add kf;
timeline.play();
![Page 31: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/31.jpg)
JRuby Closure Conversion
rect.add_changed_listener(Rectangle::HOVER) do |bean, pr, bln|
rect.fill = rect.hover ? Color::GREEN : Color::RED;
end
34
![Page 32: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/32.jpg)
JRuby Swiby
require 'swiby'
class HelloWorldModel
attr_accessor :saying
end
model = HelloWorldModel.new
model.saying = "Hello World"
Frame {
title "Hello World“
width 200
content {
Label {
text bind(model,:saying)
}
}
visible true
}35
![Page 33: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/33.jpg)
36
JavaFX With Clojure
Artwork by Augusto Sellhorn http://sellmic.com/
![Page 34: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/34.jpg)
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!
37
(def hello (fn [] "Hello world"))
(hello)
![Page 35: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/35.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}
38
(plus macros that are syntactic sugar wrapping the above)
![Page 36: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/36.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)
39
![Page 37: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/37.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)
40
Create a Function for
the Application
![Page 38: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/38.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)
41
Initialize the Stage and
Scene Variables
![Page 39: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/39.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)
42
Call Setter Methods
on Scene and Stage
![Page 40: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/40.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)
43
Java Constant Syntax
Java Method Syntax
![Page 41: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/41.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)
44
![Page 42: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/42.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)
45
doto form:
(doto symbol(.method params))
equals:
(.method symbol params)
![Page 43: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/43.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)
46
![Page 44: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/44.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)
47
Let replaced with
inline declarations
![Page 45: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/45.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)
48
Doto allows nested
data structures
![Page 46: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/46.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)
49
Now a nested
Rectangle fits!
![Page 47: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/47.jpg)
Closures in Clojure
50
> Inner classes can be created using proxy
(.addChangeListener rect Rectangle/HOVER(proxy [BooleanListener] [](handle [b, p, o]
(.setFill rect(if (.isHover rect) Color/GREEN Color/RED)))))
![Page 48: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/48.jpg)
Closures in Clojure
> Inner classes can be created using proxy
51
(.addChangeListener rect Rectangle/HOVER(proxy [BooleanListener] [](handle [b, p, o]
(.setFill rect(if (.isHover rect) Color/GREEN Color/RED)))))
Proxy form:
(proxy [class] [args] fs+)f => (name [params*] body)
![Page 49: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/49.jpg)
JavaFX With Groovy
![Page 50: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/50.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 51: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/51.jpg)
Example 1: Simple FX Script to Groovy
![Page 52: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/52.jpg)
Step 1: Lazy conversion to Groovy
class HelloStage implements Runnable {
void run() {Stage stage = new 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) {FX.start(new HelloStage());
}}
![Page 53: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/53.jpg)
Step 2: Slightly More Groovy
class HelloStage implements Runnable {
void run() {new Stage(
title: "Hello Stage (Groovy)",width: 600,height: 450,visible: true,scene: new Scene(
fill: Color.LIGHTSKYBLUE,)
);}
static void main(args) {FX.start(new HelloStage());
}}
![Page 54: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/54.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 55: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/55.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 56: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/56.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 57: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/57.jpg)
Example 2: FX Script Animation in Groovy
![Page 58: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/58.jpg)
Step 1: JavaFX Script
def timeline = Timeline {repeatCount: Timeline.INDEFINITEautoReverse: truekeyFrames: [KeyFrame {
time: 750msvalues : [rect1.x => 200.0 tween Interpolator.LINEAR,rect2.y => 200.0 tween Interpolator.LINEAR,circle1.radius => 200.0 tween Interpolator.LINEAR
]}
];}
timeline.play();
![Page 59: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/59.jpg)
Step 1a: JavaFX Script Simplification
def 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 60: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/60.jpg)
Step 2: Java-ish Groovy Animations
final Timeline timeline = new Timeline(
repeatCount: Timeline.INDEFINITE,
autoReverse: true
)
final KeyValue kv1 = new KeyValue (rect1, Rectangle.X, 200);
final KeyValue kv2 = new KeyValue (rect2, Rectangle.Y, 200);
final KeyValue kv3 = new KeyValue (circle1, Circle.RADIUS, 200);
final KeyFrame kf = new KeyFrame(Duration.valueOf(750), kv1, kv2, kv3);
timeline.getKeyFrames().add(kf);
timeline.play();
![Page 61: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/61.jpg)
Step 3: JavaFX Animation in Groovy (Using Builders)timeline = timeline(repeatCount: Timeline.INDEFINITE, autoReverse: true) {
keyframes {
keyframe(time: 750) {
keyvalue(target: rect1, property: Rectangle.Y, endValue: 200);
keyvalue(target: rect2, property: Rectangle.X, endValue: 200);
keyvalue(target: circle1, property: Circle.RADIUS, endValue: 200);
}
}
}
timeline.play();
![Page 62: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/62.jpg)
Groovy Closures- With interface coercion
def f = {
bean, pr, bln -> rect.setFill(rect.isHover() ? Color.GREEN : Color.RED);
} as BooleanListener;
rect.addChangedListener(Rectangle.HOVER, f);
![Page 63: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/63.jpg)
Groovy Closures- Interfaces with multiple methods
def keyValueTarget = [
getType: { Type.FLOAT },
unwrap: { this },
getValue: { Float.valueOf(rect.getX()) },
setValue: { Object value -> rect.setX((Float)value) }
] as KeyValueTarget;
![Page 64: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/64.jpg)
67
JavaFX With Scala
![Page 65: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/65.jpg)
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
2010
• Scala 2.8.0 (latest)
68
![Page 66: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/66.jpg)
Why Scala?
> Shares many language features with JavaFXScript 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!
69
![Page 67: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/67.jpg)
Java vs. Scala DSL
public class HelloStage implements Runnable {
public void run() {Stage stage = new 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) {FX.start(new HelloStage());
}}
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {
title = "Hello Stage"width = 600height = 450scene = new Scene {
fill = Color.LIGHTGREENcontent = List(new Rectangle {
x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
70
22 Lines
545 Characters
17 Lines
324 Characters
![Page 68: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/68.jpg)
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {title = "Hello Stage"width = 600height = 450scene = new Scene {fill = Color.LIGHTGREENcontent = List(new Rectangle {x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
71
![Page 69: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/69.jpg)
72
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {title = "Hello Stage"width = 600height = 450scene = new Scene {fill = Color.LIGHTGREENcontent = List(new Rectangle {x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {title = "Hello Stage"width = 600height = 450scene = new Scene {fill = Color.LIGHTGREENcontent = List(new Rectangle {x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
Base class for JavaFX
applications
![Page 70: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/70.jpg)
73
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {title = "Hello Stage"width = 600height = 450scene = new Scene {fill = Color.LIGHTGREENcontent = List(new Rectangle {x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
Declarative Stage
definition
![Page 71: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/71.jpg)
74
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {title = "Hello Stage"width = 600height = 450scene = new Scene {fill = Color.LIGHTGREENcontent = List(new Rectangle {x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
Inline property
definitions
![Page 72: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/72.jpg)
75
object HelloJavaFX extends JavaFXApplication {def stage = new Stage {title = "Hello Stage"width = 600height = 450scene = new Scene {fill = Color.LIGHTGREENcontent = List(new Rectangle {x = 25y = 40width = 100height = 50fill = Color.RED
})}
}}
List Construction
Syntax
![Page 73: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/73.jpg)
Animation in Scala
def timeline = new Timeline {repeatCount = INDEFINITEautoReverse = truekeyFrames = List(
new KeyFrame(50) {values = List(new KeyValue(rect1, Rectangle.X -> 300),new KeyValue(rect2, Rectangle.Y -> 500),new KeyValue(rect2, Rectangle.Width -> 150)
)}
)}
76
![Page 74: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/74.jpg)
def timeline = new Timeline {repeatCount = INDEFINITEautoReverse = truekeyFrames = List(
new KeyFrame(50) {values = List(new KeyValue(rect1, Rectangle.X -> 300),new KeyValue(rect2, Rectangle.Y -> 500),new KeyValue(rect2, Rectangle.WIDTH -> 150)
)}
)}
def timeline = new Timeline {repeatCount = INDEFINITEautoReverse = truekeyFrames = List(
new KeyFrame(50) {values = List(new KeyValue(rect1, Rectangle.X -> 300),new KeyValue(rect2, Rectangle.Y -> 500),new KeyValue(rect2, Rectangle.WIDTH -> 150)
)}
)}
Animation in Scala
77
Duration set by
Constructor Parameter
![Page 75: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/75.jpg)
Animation in Scala
78
Operator overloading for
animation syntax
def timeline = new Timeline {repeatCount = INDEFINITEautoReverse = truekeyFrames = List(
new KeyFrame(50) {values = List(new KeyValue(rect1, Rectangle.X -> 300),new KeyValue(rect2, Rectangle.Y -> 500),new KeyValue(rect2, Rectangle.WIDTH -> 150)
)}
)}
![Page 76: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/76.jpg)
Closures in Scala
79
> Closures are also supported in Scala
> And they are 100% type-safe
rect.addChangedListener(Node.HOVER, (b, p, o) => {rect.fill = if (rect.hover) Color.GREEN else Color.RED
})
![Page 77: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/77.jpg)
Closures in Scala
> Closures are also supported in Scala
> And they are 100% type-safe
80
rect.addChangedListener(Node.HOVER, (b, p, o) => {rect.fill = if (rect.hover) Color.GREEN else Color.RED
})
Compact syntax
(params) => {body}
rect.addChangedListener(Node.HOVER, (b, p, o) => {rect.fill = if (rect.hover) Color.GREEN else Color.RED
})
![Page 78: JavaFX Your Way: Building JavaFX Applications with Alternative Languages](https://reader034.vdocuments.site/reader034/viewer/2022042508/559e7a961a28abf0318b46a4/html5/thumbnails/78.jpg)
Conclusion
> JavaFX as Java APIs is great
> Usable in alternate languages
> Over time improved support is possible
E.g. Groovy builders, Scala DSL
Remember: This is a proof of concept only – you can not leave this session and do this today.