Don't panic in Fortaleza - ScalaFX

643 views

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
643
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Don't panic in Fortaleza - ScalaFX

  1. 1. JavaFX Scala
  2. 2. Como fazer em casa algo que custa mais de U$15 mil? Gastando apenas R$20mil?
  3. 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!" } } } JavaFX Script 1.3
  4. 4. 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(); } }
  5. 5. 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 }
  6. 6. 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(); }
  7. 7. ¿ ?
  8. 8. ¿ ?
  9. 9. ¿ ?
  10. 10. ¿ ?
  11. 11. scene graph CSS styling animations & transitions event management .dmg .msi .exe .rpm .deb
  12. 12. 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"') } } }
  13. 13. 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!" } } } }
  14. 14. 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
  15. 15. James Strachan
  16. 16. Scala is statically typed and compiles down to the same fast bytecode as Java so it's usually about as fast as Java.
  17. 17. 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.
  18. 18. 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.
  19. 19. 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.
  20. 20. Statically typed JVM bytecode Type inference High order functions Closures Sequence comprehension Notthatpleasingontheeye Beautifully concise code
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 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) } } } } Criação de Sequência via Comprehension
  26. 26. 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}
  27. 27. 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
  28. 28. 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
  29. 29. 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 }
  30. 30. 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}
  31. 31. 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"
  32. 32. 2001 Scala começou 2003/2004 Scala v1.0 2006 Scala v2.0 2013 Scala 2.10.1 (última)
  33. 33. Alain Béarez « francês » (* ~10×106 ms Unix Epoch)

×