Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1.  
  2. import javafx.animation.AnimationTimer;
  3. import javafx.application.Application;
  4. import javafx.geometry.Rectangle2D;
  5. import javafx.scene.Group;
  6. import javafx.scene.Scene;
  7. import javafx.scene.image.ImageView;
  8. import javafx.scene.input.KeyCode;
  9. import javafx.scene.input.KeyEvent;
  10. import javafx.stage.Stage;
  11.  
  12. public class Esercitazione extends Application{
  13. Group gPersonaggio= new Group();
  14. Group gSfondo= new Group();
  15. Group gPadre=new Group();
  16.  
  17. ImageView iSfondo= new ImageView("https://previews.123rf.com/images/lianella/lianella1412/lianella141200024/34318523-Sfondo-Foresta-Gioco-per-l-applicazione-del-gioco-2d-Disegno-vettoriale-Piastrellabile-in-orizzontal-Archivio-Fotografico.jpg");
  18. ImageView iPersonaggio= new ImageView("http://www.sibeg.it/pics/976_r_900_550.jpg");
  19.  
  20. AnimationTimer sposta;
  21.  
  22. int iContatore=0;
  23. double posX=0;
  24.  
  25. public void start(Stage finestra){
  26.  
  27. gPadre.getChildren().addAll(gPersonaggio,gSfondo);
  28. gSfondo.getChildren().add(iSfondo);
  29. gPersonaggio.getChildren().add(iPersonaggio);
  30.  
  31. Rectangle2D rettangolo= new Rectangle2D(posX,0,500,500);
  32. iSfondo.setViewport(rettangolo);
  33.  
  34. Scene scena=new Scene(gPadre,500,500);
  35. finestra.setScene(scena);
  36. finestra.setTitle("esercitazione");
  37. finestra.show();
  38.  
  39. scena.setOnKeyPressed(e-> tastoPremuto(e));
  40.  
  41. }
  42.  
  43. public void tastoPremuto(KeyEvent e){
  44. if(e.getCode()== KeyCode.UP){
  45. sposta= new AnimationTimer(){
  46. public void handle(long ora){
  47. su();
  48. }
  49. };
  50. sposta.start();
  51. }
  52.  
  53. if(e.getCode()==KeyCode.DOWN){
  54. sposta = new AnimationTimer(){
  55. public void handle(long ora){
  56. giu();
  57. }
  58. };
  59. sposta.start();
  60. }
  61.  
  62. if(e.getCode()==KeyCode.SPACE){
  63. sposta= new AnimationTimer(){
  64. public void handle(long ora){
  65. scorrimento();
  66. }
  67. };
  68. sposta.start();
  69. }
  70.  
  71. }
  72.  
  73. public void su(){
  74. double posY= gPersonaggio.getTranslateY();
  75. posY -= 2.0;
  76. gPersonaggio.setTranslateY(posY);
  77. iContatore++;
  78. if(iContatore==5){
  79. sposta.stop();
  80. iContatore=0;
  81. }
  82. }
  83.  
  84. public void giu(){
  85. double posY= gPersonaggio.getTranslateY();
  86. posY += 2.0;
  87. gPersonaggio.setTranslateY(posY);
  88. iContatore++;
  89. if(iContatore==5){
  90. sposta.stop();
  91. iContatore=0;
  92. }
  93. }
  94.  
  95. public void scorrimento(){
  96. posX += 3.0;
  97. Rectangle2D rettangolo= new Rectangle2D(posX,0,500,500);
  98. iSfondo.setViewport(rettangolo);
  99. if(posX >= 1499){
  100. sposta.stop();
  101. }
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. public static void main(String[] args) {
  123. // TODO Auto-generated method stub
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement