Mini-curso JavaFX Aula2

3,408 views
3,384 views

Published on

Slides da 2a aula do mini-curso de JavaFX que ministrei na UFPB

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,408
On SlideShare
0
From Embeds
0
Number of Embeds
1,716
Actions
Shares
0
Downloads
206
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mini-curso JavaFX Aula2

  1. 1. Raphael Marques Mestrando em Informática da UFPB jose.raphael.marques@gmail.com raphaelmarques.wordpress.com
  2. 2. 2
  3. 3. var seq1 : Integer[ ] = [1,2,3,4,5,6,7,8,9,10]; println(seq1); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] var seq2 = [1..10]; println(seq2); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] var seq3 = [1..10 step 2]; println(seq3); //[ 1, 3, 5, 7, 9 ] var seq4 = [10..1 step -2]; println(seq4); //[ 10, 8, 6, 4, 2 ] 3
  4. 4. var seq1 = [1..3 step 0.5]; println(seq1); //[ 1.0, 1.5, 2.0, 2.5, 3.0 ] var seq2 = [1.5..3]; println(seq2); //[ 1.5, 2.5 ] var seq3 : (Integer[ ])[ ] = [1,2,3,4,5,6]; println(seq3); //[ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ] ] var seq4 : (Integer[ ])[ ] = [ [1..3] , [4..6] ]; println(seg4); //[ [ 1, 2, 3 ], [ 4, 5, 6 ] ] 4
  5. 5. var seq1 : Integer[] = 0; println(seq1); //[ 0 ] var seq2 = [1..5]; println(seq2); //[ 1, 2, 3, 4, 5 ] seq2[0] = 0; println(seq2); //[ 0, 2, 3, 4, 5 ] 5
  6. 6. var seq1 = [1..5]; var seq2 = seq1; seq2[0] = 0; println(seq1); //[ 1, 2, 3, 4, 5 ] println(seq2); //[ 0, 2, 3, 4, 5 ] 6
  7. 7. var uteis = ["seg","ter","qua","qui","sex"]; var fds = ["sab","dom"]; var semana = [uteis,fds]; var semana2 = [uteis,["sab","dom"]]; var semana3 = [uteis,"sab","dom"]; 7
  8. 8. var n = [1..10]; var pares = n[i | i mod 2 == 0]; println(pares); //[ 2, 4, 6, 8, 10 ] println(sizeof pares); //5 8
  9. 9. var n = [1..5]; //[ 1, 2, 3, 4, 5 ] insert 4 into n; //[ 1, 2, 3, 4, 5, 4 ] insert 7 before n[3]; //[ 1, 2, 3, 7, 4, 5, 4 ] insert 8 after n[3]; //[ 1, 2, 3, 7, 8, 4, 5, 4 ] delete 4 from n; //[ 1, 2, 3, 7, 8, 5 ] delete n[2] from n; //[ 1, 2, 7, 8, 5 ] delete n[2]; //[ 1, 2, 8, 5 ] delete n; //[ ] 9
  10. 10. var n = [1..5]; //[ 1, 2, 3, 4, 5 ] n = reverse n; //[ 5, 4, 3, 2, 1 ] var n2 = [5..1 step -1]; //[ 5, 4, 3, 2, 1 ] println(n == n2); //true 10
  11. 11. var n = [1..10]; //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] n[0..4] //[ 1, 2, 3, 4, 5 ] n[0..<4] //[ 1, 2, 3, 4 ] n[5..] //[ 6, 7, 8, 9, 10 ] n[0..<] //[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 11
  12. 12. 12
  13. 13.  Tudo em JavaFX é uma expressão  loops, condicionais, blocos  Em alguns casos as expressões tem tipo Void.  while, classes  A ultima expressão de um bloco é o retorno dele. 13
  14. 14. var x = { var a = 5; a * 2; }; println(x); //10 14
  15. 15. var x = 10; var x = 10; var a : Number; var a = 0; var a = 0; if(x > 0){ var b = if(x > 0){ var b = a = 1; a = 1; a = 1; }else{ }else{ println(b);//1.0 a = 2; a = 2; } }; println(a); //1 println(a); //1 println(b);//1 15
  16. 16. var x = 0; var x = 0; while(x < 10){ while(x < 10) x++; x++; } println(x); //10 println(x); //10 16
  17. 17. for(i in [1..5]){ var a = for(i in [1..5]) print("[{i}]"); i*i; } println(a); //[1][2][3][4][5] //[ 1, 4, 9, 16, 25 ] var a = [1..5]; var b = for(i in [1,4,9,16,25]) for(i in a) Math.sqrt(i); print("[{i}]"); println(b); //[1][2][3][4][5] //[ 1.0, 2.0, 3.0, 4.0, 5.0 ] 17
  18. 18. 18
  19. 19. class Conta{ var nome: String; var numero: Integer; var saldo = 1000; init{ } function deposito(valor: Integer):Void{ saldo += valor; } override function toString(){ "Conta({nome},{numero},${saldo})"; } } 19
  20. 20. var conta = Conta{ nome: "Raphael" numero: 123 }; println(conta); //Conta(Raphael,123,$1000) 20
  21. 21. class MyClass extends Conta { } class MyClass extends Serializable { } class MyClass extends Conta, Serializable { } 21
  22. 22. mixin class Terrestre{ function andar(){ println("andando"); } } mixin class Aquativo{ function nadar(){ println("nadando"); } } class Jacare extends Terrestre, Aquativo{ } var jacare = Jacare{}; jacare.andar(); jacare.nadar(); 22
  23. 23.  default  Visível apenas no script  package  Visível no mesmo pacote  protected  Visível no mesmo pacote ou subclasses.  public  Sem restrições 23
  24. 24.  public-read  Sem restrição para leitura  Só pode ser escrito dentro do script  Para permissão de escrita: ▪ package public-read ▪ protected public-read  public-init  Pode ser inicializado na criação do objeto, em outros casos é igual ao public-read 24
  25. 25. arquivo MyClass.fx: public var x = 1; public class MyClass{ public var y = 2; } outro arquivo: MyClass.x = 0; var obj = MyClass{}; obj.y = 0; 25
  26. 26. 26
  27. 27. class Donut extends CustomNode { public var x = 0.0; public var y = 0.0; public var raioExterno = 10.0; public var raioInterno = 5.0; public var fill: Paint = Color.BLACK; public var stroke: Paint; ... } 27
  28. 28. class Donut extends CustomNode { ... public override function create(): Node { Group{ content: bind ShapeSubtract{ a: Circle{radius: raioExterno} b: Circle{radius: raioInterno} fill: bind fill stroke: bind stroke translateX: bind x translateY: bind y } } } } 28
  29. 29. class Donut extends CustomNode { ... public override function create(): Node { Group{ content: bind ShapeSubtract{ a: Circle{radius: raioExterno} b: Circle{radius: raioInterno} fill: bind fill stroke: bind stroke translateX: bind x translateY: bind y } } } } 29
  30. 30. class Donut extends CustomNode { ... public override function create(): Node { Group{ content: bind ShapeSubtract{ a: Circle{radius: raioExterno} b: Circle{radius: raioInterno} fill: bind fill stroke: bind stroke translateX: bind x translateY: bind y } } } } 30
  31. 31. class Donut extends CustomNode { ... public override function create(): Node { Group{ content: bind ShapeSubtract{ a: Circle{radius: raioExterno} b: Circle{radius: raioInterno} fill: bind fill stroke: bind stroke translateX: bind x translateY: bind y } } } } 31
  32. 32. class Donut extends CustomNode { ... public override function create(): Node { Group{ content: bind ShapeSubtract{ a: Circle{radius: raioExterno} b: Circle{radius: raioInterno} fill: bind fill stroke: bind stroke translateX: bind x translateY: bind y } } } } 32
  33. 33. class Donut extends CustomNode { ... public override function create(): Node { Group{ content: bind ShapeSubtract{ a: Circle{radius: raioExterno} b: Circle{radius: raioInterno} fill: bind fill stroke: bind stroke translateX: bind x translateY: bind y } } } } 33
  34. 34. Donut{ x: 100 y: 100 raioExterno: 80 raioInterno: 50 fill: Color.RED stroke: Color.BLACK } 34
  35. 35. Donut{ x: 100 y: 100 raioExterno: 80 raioInterno: 50 fill: Color.RED stroke: Color.BLACK } 35
  36. 36. 36
  37. 37.  Criar um projeto chamado Praticas  Site:  http://sites.google.com/site/joseraphaelmarques/  Link lateral: eventos  Baixar os 4 arquivos no pacote praticas 37
  38. 38. 38
  39. 39. 39
  40. 40. 40
  41. 41. 0 X Y vel angle (x,y) 41
  42. 42. 0 X vel+acel Y vel angle (x,y) 42
  43. 43. 0 X vel Y vel-acel angle (x,y) 43
  44. 44. 0 X Y vy vel angle (x,y) vx 44
  45. 45. 0 X turn left Y turn right (x,y) 45
  46. 46.  Criar um projeto chamado RaceFX  Site:  http://sites.google.com/site/joseraphaelmarques/  Link lateral: RaceFX  Link: Parte 1  Baixar para o pacote racefx:  bluecar.png  redcar.png  Game.fx 46
  47. 47. 47
  48. 48. public def REDCAR = Image { url: "{__DIR__}redcar.png" }; public def BLUECAR = Image { url: "{__DIR__}bluecar.png" }; def RAD180 = Math.PI; def RAD360 = 2*Math.PI; def ACEL = 100; def TURN = Math.PI; def VMIN = -50; def VMAX = 250; def FRICTION = 50; 48
  49. 49. public class Car extends CustomNode{ //posicao public var x: Number; public var y: Number; public var angle: Number; public var vel: Number; public-init var image: Image; //controles public var left = false; public var right = false; public var up = false; public var down = false; ... } 49
  50. 50. public class Car extends CustomNode{ ... public override function create(): Node { return ImageView { image: image translateX: bind x – 15 translateY: bind y - 7.5 rotate: bind -Math.toDegrees(angle); }; } ... } 50
  51. 51. public class Car extends CustomNode{ ... function updateTurn(factor: Number):Void{ if(left != right){//xor var t = if(left) TURN else –TURN; angle = (angle + t*factor) mod RAD360; } } ... } 51
  52. 52. public class Car extends CustomNode{ ... function updateAcel(factor: Number):Void{ if(down){//freiando vel = Math.max(VMIN, vel - ACEL*factor); }else if(up){//acelerando vel = Math.min(VMAX, vel + ACEL*factor); }else{//desacelerando if(vel > 0){ vel = Math.max(0, vel - FRICTION*factor); }else{ vel = Math.min(0, vel + FRICTION*factor); } } } ... } 52
  53. 53. public class Car extends CustomNode{ ... function updateMove(factor: Number):Void{ var vx = Math.cos(angle) * vel * factor; var vy = -Math.sin(angle) * vel * factor; var i = x + vx; var j = y + vy; x = i; y = j; } ... } 53
  54. 54. public class Car extends CustomNode{ ... public function update(time: Long):Void{ var factor = time / 1000.0; if(factor == 0) return; updateTurn(factor); updateAcel(factor); if(vel != 0){ updateMove(factor); } } ... } 54
  55. 55. 55
  56. 56. public class Game extends CustomNode{ public-read var car1 = Car{}; public-read var car2 = Car{}; var lastTime: Long; function update():Void{} var timeline = Timeline {} public function reset():Void{} public function play(){} var bg = Rectangle{}; init{} public override function create():Node{} } 56
  57. 57. public class Game extends CustomNode{ ... public override function create():Node{ Group{ content: [ bg, car1, car2, ] } } ... } 57
  58. 58. public class Game extends CustomNode{ ... function checkKey(code: KeyCode, value: Boolean){ if(code == KeyCode.VK_LEFT){ car1.left = value; }else if ... } function onKeyPressed(e:KeyEvent):Void{ checkKey(e.code, true); } function onKeyReleased(e:KeyEvent):Void{ checkKey(e.code, false); } ... } 58
  59. 59. 59
  60. 60. var game = Game{}; Stage { title: "RaceFX" scene: Scene { width: 640 height: 480 content: game } } 60
  61. 61. 61
  62. 62. 62

×