Don't panic in Fortaleza - ScalaFX

36
JavaFX Scala

description

Apresentação em português dos motivos para escolher ScalaFX para criar aplicativos nativos para as plataformas Mac OS X, Linux e Windows. Explico em português minha caminhada atrás das alternativas no mundo do JavaFX2 após a retirada do JavaFX Script em setembro de 2010. Descubra aqui por que eu fiquei com ScalaFX como alternativa mais elegante e fácil de programar.

Transcript of Don't panic in Fortaleza - ScalaFX

Page 1: Don't panic in Fortaleza - ScalaFX

JavaFX

Scala

Page 2: Don't panic in Fortaleza - ScalaFX

Como fazer em casa algo que custa mais de U$15 mil?

Gastando apenas R$20mil?

Page 3: Don't panic in Fortaleza - ScalaFX
Page 4: Don't panic in Fortaleza - ScalaFX

Stage { title: "Hello World" width: 400 height: 250 scene: Scene { fill: Color.ALICEBLUE content: Text { font: Font { name: "Envy Code R" size: 20 } x: 105, y: 120 content: "Hello World!" } }}

JavaFX Script 1.3

Page 5: Don't panic in Fortaleza - ScalaFX

Stage { title: "Hello World" width: 400 height: 250 scene: Scene { fill: Color.ALICEBLUE content: Text { font: Font { name: "Envy Code R" size: 20 } x: 105, y: 120 content: "Hello World!" } }}

public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World"); Group root = new Group(); Scene scene = new Scene(root, 400, 250, Color.ALICEBLUE); Text text = new Text(); text.setX(105); text.setY(120); text.setFont(Font.font("Envy Code R", 20)); text.setText("Hello World!"); root.getChildren().add(text); primaryStage.setScene(scene); primaryStage.show(); }}

Page 6: Don't panic in Fortaleza - ScalaFX

Stage { title: "Hello World" width: 400 height: 250 scene: Scene { fill: Color.ALICEBLUE content: Text { font: Font { name: "Envy Code R" size: 20 } x: 105, y: 120 content: "Hello World!" } }}

Stage { Scene { Text { "Hello World!" x: 105, y: 120 font: Font { name: "Envy Code R" size: 20pt } } fill: ALICEBLUE } title: "Hello World" width: 400 height: 250}

Page 7: Don't panic in Fortaleza - ScalaFX

Stage { title: "Hello World" width: 400 height: 250 scene: Scene { fill: Color.ALICEBLUE content: Text { font: Font { name: "Envy Code R" size: 20 } x: 105, y: 120 content: "Hello World!" } }}

public void start(Stage primaryStage) { primaryStage.setTitle("Hello World"); primaryStage.setScene( SceneBuilder.create() .width(400) .height(250) .fill(Color.ALICEBLUE) .root( GroupBuilder.create().children( TextBuilder.create() .x(105) .y(120) .text("Hello World!") .font(Font.font("Envy Code R", 20)) .build() ).build() ) .build()); primaryStage.show();}

Page 8: Don't panic in Fortaleza - ScalaFX

¿ ?

Page 9: Don't panic in Fortaleza - ScalaFX

¿ ?

Page 10: Don't panic in Fortaleza - ScalaFX

¿ ?

Page 11: Don't panic in Fortaleza - ScalaFX

¿ ?

Page 12: Don't panic in Fortaleza - ScalaFX
Page 13: Don't panic in Fortaleza - ScalaFX
Page 14: Don't panic in Fortaleza - ScalaFX

scene graph

CSS styling

animations & transitions

event management

.dmg.msi.exe

.rpm

.deb

Page 15: Don't panic in Fortaleza - ScalaFX

Stage { title: "Hello World" width: 400 height: 250 scene: Scene { fill: Color.ALICEBLUE content: Text { font: Font { name: "Envy Code R" size: 20 } x: 105, y: 120 content: "Hello World!" } }}

GroovyFX.start { primaryStage -> def sg = new SceneGraphBuilder() sg.stage( title: 'Hello World', show: true) { scene( fill: aliceblue, width: 400, height: 250) { text( x: 105, y: 120, text: "Hello World!" font: '20pt "Envy Code R"') } }}

Page 16: Don't panic in Fortaleza - ScalaFX

Stage { title: "Hello World" width: 400 height: 250 scene: Scene { fill: Color.ALICEBLUE content: Text { font: Font { name: "Envy Code R" size: 20 } x: 105, y: 120 content: "Hello World!" } }}

object HelloWorld extends JFXApp { stage = new JFXApp.PrimaryStage { title = "Hello World" scene = new Scene(400, 250) { fill = Color.ALICEBLUE content = new Text { font = Font("Envy Code R", 20) x = 105; y = 120 text = "Hello World!" } } }}

Page 17: Don't panic in Fortaleza - ScalaFX

I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003, I'd probably have never created Groovy.

James Strachan

Page 18: Don't panic in Fortaleza - ScalaFX

James Strachan

Page 19: Don't panic in Fortaleza - ScalaFX

Scala is statically typed and compiles down to the same fast bytecode as Java so it's usually about as fast as Java.

Page 20: Don't panic in Fortaleza - ScalaFX

Scala is statically typed and compiles down to the same fast bytecode as Java so it's usually about as fast as Java.

Scala has type inference - so it's typically as concise as Ruby/Groovy but that everything has static types.

Page 21: Don't panic in Fortaleza - ScalaFX

Scala is statically typed and compiles down to the same fast bytecode as Java so it's usually about as fast as Java.

Scala has type inference - so it's typically as concise as Ruby/Groovy but that everything has static types.

Scala has high order functions and closure support along with sequence comprehensions so you can write beautifully concise code.

Page 22: Don't panic in Fortaleza - ScalaFX

Scala is statically typed and compiles down to the same fast bytecode as Java so it's usually about as fast as Java.

Scala has type inference - so it's typically as concise as Ruby/Groovy but that everything has static types.

Scala does take a little bit of getting used to - I confess the first few times I looked at Scala it wasn't that pleasing on the eye.

Scala has high order functions and closure support along with sequence comprehensions so you can write beautifully concise code.

Page 23: Don't panic in Fortaleza - ScalaFX

Static

ally ty

ped

JVM bytecode

Type inference

High order functions

Closu

res

Sequence comprehension Not t

hat p

leasin

g on

the

eye

Beautifully concise code

Page 24: Don't panic in Fortaleza - ScalaFX

Por que Scala?

> Compartilha muitas funcionalidades do JavaFX Script que tornam a programação de interfaces mais fácil:

Checagem estática de tipos – Encontre seus erros em tempo de compilaçãoClosures / traits – Misture os comportamentos e passe-os como referênciaDeclarativa – Expresse a interface como ela deve aparecer

> Scala também permite implementar suas DSLs!Conversões implícitas – extensão de classes typesafeSobrecarga de operadores – com regras de precedênciaDelayedInit / @specialized – funcionalides avançadas da linguagem

Page 25: Don't panic in Fortaleza - ScalaFX

object VanishingCircles extends JFXApp { stage = new JFXApp.PrimaryStage { title = "Vanishing Circles" width = 800 height = 600 scene = new Scene { fill = BLACK content = for (i <- 0 until 50) yield new Circle { centerX = random * 800 centerY = random * 600 radius = 150 fill = color(random, random, random, 0.2) effect = new BoxBlur(10, 10, 3) } } }}

Classe base para aplicações ScalaFX

Page 26: Don't panic in Fortaleza - ScalaFX

object VanishingCircles extends JFXApp { stage = new JFXApp.PrimaryStage { title = "Vanishing Circles" width = 800 height = 600 scene = new Scene { fill = BLACK content = for (i <- 0 until 50) yield new Circle { centerX = random * 800 centerY = random * 600 radius = 150 fill = color(random, random, random, 0.2) effect = new BoxBlur(10, 10, 3) } } }}

Definição Declarativa do Stage

Page 27: Don't panic in Fortaleza - ScalaFX

object VanishingCircles extends JFXApp { stage = new JFXApp.PrimaryStage { title = "Vanishing Circles" width = 800 height = 600 scene = new Scene { fill = BLACK content = for (i <- 0 until 50) yield new Circle { centerX = random * 800 centerY = random * 600 radius = 150 fill = color(random, random, random, 0.2) effect = new BoxBlur(10, 10, 3) } } }}

Definições das propriedades no corpo

Page 28: Don't panic in Fortaleza - ScalaFX

object VanishingCircles extends JFXApp { stage = new JFXApp.PrimaryStage { title = "Vanishing Circles" width = 800 height = 600 scene = new Scene { fill = BLACK content = for (i <- 0 until 50) yield new Circle { centerX = random * 800 centerY = random * 600 radius = 150 fill = color(random, random, random, 0.2) effect = new BoxBlur(10, 10, 3) } } }}

Criação de Sequência via Comprehension

Page 29: Don't panic in Fortaleza - ScalaFX

Animação em ScalaFX

val timeline = new Timeline { cycleCount = INDEFINITE autoReverse = true keyFrames = for (circle <- circles) yield at (40 s) { Set( circle.centerX -> random * stage.width.get, circle.centerY -> random * stage.height.get ) }}timeline.play

Sintaxe de animação como no JavaFX Script:

at (duração) {keyframes}

Page 30: Don't panic in Fortaleza - ScalaFX

Animação em ScalaFX

val timeline = new Timeline { cycleCount = INDEFINITE autoReverse = true keyFrames = for (circle <- circles) yield at (40 s) { Set( circle.centerX -> random * stage.width.get, circle.centerY -> random * stage.height.get ) }}timeline.play Sobrecarga de operador para

sintaxe de animação

Page 31: Don't panic in Fortaleza - ScalaFX

Animação em ScalaFX

val timeline = new Timeline { cycleCount = INDEFINITE autoReverse = true keyFrames = for (circle <- circles) yield at (40 s) { Set( circle.centerX -> random * stage.width tween EASE_BOTH, circle.centerY -> random * stage.height tween EASE_IN ) }}timeline.play Sintaxe tween

opcional

Page 32: Don't panic in Fortaleza - ScalaFX

Event Listeners em ScalaFX

> Suportado usando sintaxe Closure embutida> Argumentos opcionais para tratamento de eventos> 100% tipagem forte

Sintaxe compacta{body}

onMouseClicked = {

Timeline(at(3 s){radius->0}).play

}

Page 33: Don't panic in Fortaleza - ScalaFX

Event Listeners em ScalaFX

> Suportado usando sintaxe Closure embutida> Argumentos opcionais para tratamento de eventos> 100% tipagem forte

onMouseClicked = { (e: MouseEvent) =>

Timeline(at(3 s){radius->0}).play

}

Evento = parametro opcional {(event) => body}

Page 34: Don't panic in Fortaleza - ScalaFX

Binding em ScalaFX

Adição/Subtração/Multiplicação/Divisão Infixas:

height <== rect1.height + rect2.height

Operadores de Agregação:

width <== max(rect1.width, rect2.width, rect3.width)

Expressões Condicionais:

strokeWidth <== when (hover) choose 4 otherwise 0

Expressões Compostas:

text <== when (rect.hover || circle.hover && !disabled) choose textField.text + " is enabled" otherwise "disabled"

Page 35: Don't panic in Fortaleza - ScalaFX

2001Scala começou

2003/2004Scala v1.0

2006Scala v2.0

2013Scala 2.10.1 (última)

Page 36: Don't panic in Fortaleza - ScalaFX

Alain Béarez

« francês »

(* ~10×106 ms Unix Epoch)