javafx your way: building javafx applications with alternative languages

79
JavaFX Your Way Building JavaFX Applications with Alternative Languages Stephen Chin GXS [email protected] tweet: @steveonjava Jonathan Giles Oracle [email protected] tweet: @jonathangiles

Upload: stephen-chin

Post on 09-Jul-2015

11.858 views

Category:

Technology


3 download

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

JavaFX Your WayBuilding JavaFX Applications with Alternative Languages

Stephen ChinGXS

[email protected]

tweet: @steveonjava

Jonathan GilesOracle

[email protected]: @jonathangiles

Page 2: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

JavaFX Your WayBuilding JavaFX Applications with Alternative Languages

Stephen ChinGXS

[email protected]

tweet: @steveonjava

Jonathan GilesOracle

[email protected]: @jonathangiles

Page 3: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

Meet the Presenters…

Steve Jonathan

3

Motorcyclist

Family Man

Page 4: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

Overall Presentation Goal

Demonstrate the future potential of the JavaFX platform.

Page 8: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

JavaFX With Java

Page 11: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

JavaFX With JRuby

Page 27: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

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

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

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

36

JavaFX With Clojure

Artwork by Augusto Sellhorn http://sellmic.com/

Page 34: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

JavaFX With Groovy

Page 50: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

Example 1: Simple FX Script to Groovy

Page 52: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

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

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

Example 2: FX Script Animation in Groovy

Page 58: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

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

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

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

67

JavaFX With Scala

Page 65: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

Page 79: JavaFX Your Way: Building JavaFX Applications with Alternative Languages

83

Stephen [email protected]

tweet: @steveonjava

Jonathan [email protected]

tweet: @jonathangiles