Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package model;
  2.  
  3. import com.sun.jdi.request.DuplicateRequestException;
  4. import javafx.animation.ScaleTransition;
  5. import javafx.scene.image.Image;
  6. import javafx.scene.image.ImageView;
  7. import javafx.scene.input.MouseEvent;
  8. import javafx.scene.layout.Pane;
  9.  
  10. import javafx.util.Duration;
  11.  
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Iterator;
  16. import java.util.Random;
  17.  
  18. public class CardPane extends Pane {
  19.  
  20.  
  21. private ImageView front = new ImageView(new Image("view/img/front.jpg"));;
  22. private ImageView back;
  23. private int id;
  24. private static ArrayList <Integer> randomList = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
  25.  
  26.  
  27.  
  28.  
  29. public static void setDelayTime(Duration delayTime) {
  30. CardPane.delayTime = delayTime;
  31. }
  32. public static void sizeDownPool(int e){
  33.  
  34. Iterator itr = randomList.iterator();
  35. while (itr.hasNext())
  36. {
  37. int x = (Integer)itr.next();
  38. if (x == e)
  39. itr.remove();
  40. }
  41.  
  42.  
  43.  
  44. }
  45. public int getid() {
  46. return id;
  47. }
  48. private static Duration delayTime;
  49. private static boolean flip;
  50.  
  51. public static void setFlip(boolean f){
  52. CardPane.flip = f;
  53. }
  54.  
  55. public ImageView getFront() {
  56. return front;
  57. }
  58.  
  59. public ImageView getBack() {
  60. return back;
  61. }
  62.  
  63. private int generateRandomNumber(){
  64. if (randomList.size() > 0){
  65. Random rand = new Random();
  66. return randomList.get(rand.nextInt(randomList.size()));
  67. }
  68. else return 1;
  69.  
  70. }
  71. private ImageView generateRandomBack(){
  72. this.id = generateRandomNumber();
  73. return new ImageView(new Image(String.format("view/img/footballers/%d.jpg", id)));
  74. }
  75.  
  76. public CardPane(){
  77. front.setPreserveRatio(false);
  78.  
  79. front.setFitWidth(106);
  80. front.setFitHeight(107);
  81. back = generateRandomBack();
  82. back.setPreserveRatio(false);
  83. back.setFitHeight(107);
  84. back.setFitWidth(106);
  85.  
  86. getChildren().addAll(back, front);
  87.  
  88. setOnMouseClicked((MouseEvent t) -> {
  89. if (front.getScaleX() == 1 && flip){
  90. frontToBack();
  91. GameSystem.addToCheck(id);
  92. backToFront(GameSystem.flipDuration());
  93.  
  94. }
  95. }
  96. );
  97.  
  98. }
  99. private void frontToBack(){
  100. ScaleTransition hideFront = new ScaleTransition(Duration.millis(500), front);
  101. ScaleTransition showBack = new ScaleTransition(Duration.millis(500), back);
  102.  
  103. hideFront.setFromX(1);
  104. hideFront.setToX(0);
  105.  
  106. back.setScaleX(0);
  107. showBack.setFromX(0);
  108. showBack.setToX(1);
  109.  
  110. hideFront.setOnFinished(t -> showBack.play());
  111.  
  112. hideFront.play();
  113. }
  114. private void backToFront (boolean isScored){
  115. ScaleTransition showFront = new ScaleTransition(Duration.millis(500), front);
  116. ScaleTransition hideBack = new ScaleTransition(Duration.millis(500), back);
  117.  
  118.  
  119. hideBack.setFromX(1);
  120. hideBack.setToX(0);
  121.  
  122.  
  123. front.setScaleX(0);
  124. showFront.setFromX(0);
  125. showFront.setToX(1);
  126. if (isScored){
  127. hideBack.setDelay(Duration.INDEFINITE);
  128. }else{
  129. hideBack.setDelay(delayTime);
  130. }
  131.  
  132. hideBack.setOnFinished(t -> {
  133. showFront.play();
  134.  
  135. });
  136.  
  137. hideBack.play();
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement