don't panic in fortaleza - scalafx
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
JavaFX
Scala
Como fazer em casa algo que custa mais de U$15 mil?
Gastando apenas R$20mil?
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
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(); }}
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}
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();}
¿ ?
¿ ?
¿ ?
¿ ?
scene graph
CSS styling
animations & transitions
event management
.dmg.msi.exe
.rpm
.deb
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"') } }}
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!" } } }}
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
James Strachan
Scala is statically typed and compiles down to the same fast bytecode as Java so it's usually about as fast as Java.
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 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.
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.
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
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
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
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
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
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
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}
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
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
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
}
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}
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"
2001Scala começou
2003/2004Scala v1.0
2006Scala v2.0
2013Scala 2.10.1 (última)
Alain Béarez
« francês »
(* ~10×106 ms Unix Epoch)