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.
16. 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!"
}
}
}
}
17. 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
19. Scala is statically typed and compiles
down to the same fast bytecode as
Java so it's usually about as fast as
Java.
20. 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.
21. 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.
22. 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.
23. Statically typed
JVM bytecode
Type inference
High order functions
Closures
Sequence comprehension
Notthatpleasingontheeye
Beautifully concise code
24. 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ção
Closures / traits – Misture os comportamentos e passe-os como
referência
Declarativa – Expresse a interface como ela deve aparecer
> Scala também permite implementar suas DSLs!
Conversões implícitas – extensão de classes typesafe
Sobrecarga de operadores – com regras de precedência
DelayedInit / @specialized – funcionalides avançadas da
linguagem
25. 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
26. 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
27. 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
28. 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
29. 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}
30. 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
31. 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
32. 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
}
33. 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}
34. 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"