Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package memory;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. import javafx.animation.FadeTransition;
  8. import javafx.application.Application;
  9. import javafx.geometry.Pos;
  10. import javafx.scene.Parent;
  11. import javafx.scene.Scene;
  12. import javafx.scene.image.Image;
  13. import javafx.scene.image.ImageView;
  14. import javafx.scene.input.MouseEvent;
  15. import javafx.scene.layout.Pane;
  16. import javafx.scene.layout.StackPane;
  17. import javafx.scene.paint.Color;
  18. import javafx.scene.shape.Rectangle;
  19. import javafx.scene.text.Text;
  20. import javafx.stage.Stage;
  21. import javafx.util.Duration;
  22.  
  23. public class Memory extends Application {
  24.  
  25.     private static final int NUM_OF_PAIRS = 8;
  26.     private static final int NUM_PER_ROW = 4;
  27.  
  28.     Image A = new Image("file:bilder/apple.png");
  29.     Image B = new Image("file:bilder/ananas.png");
  30.     Image C = new Image("file:bilder/kiwi.png");
  31.     Image D = new Image("file:bilder/melone.png");
  32.     Image E = new Image("file:bilder/banana.png");
  33.     Image F = new Image("file:bilder/pear.png");
  34.     Image G = new Image("file:bilder/raspberry.png");
  35.     Image H = new Image("file:bilder/strawberry.png");
  36.     Image I = new Image("file:bilder/cocnut.png");
  37.  
  38.     private Tile selected = null;
  39.     private int clickCount = 2;
  40.  
  41.     private Parent createContent() {
  42.  
  43.         Pane root = new Pane();
  44.         root.setPrefSize(400, 400);
  45.  
  46.         List<Tile> tiles = new ArrayList<>();
  47.         tiles.add(new Tile("A", A));
  48.         tiles.add(new Tile("A", A));
  49.         tiles.add(new Tile("B", B));
  50.         tiles.add(new Tile("B", B));
  51.         tiles.add(new Tile("C", C));
  52.         tiles.add(new Tile("C", C));
  53.         tiles.add(new Tile("D", D));
  54.         tiles.add(new Tile("D", D));
  55.         tiles.add(new Tile("E", E));
  56.         tiles.add(new Tile("E", E));
  57.         tiles.add(new Tile("F", F));
  58.         tiles.add(new Tile("F", F));
  59.         tiles.add(new Tile("G", G));
  60.         tiles.add(new Tile("G", G));
  61.         tiles.add(new Tile("H", H));
  62.         tiles.add(new Tile("H", H));
  63.  
  64.         Collections.shuffle(tiles);
  65.  
  66.         for (int i = 0; i < tiles.size(); i++) {
  67.             Tile tile = tiles.get(i);
  68.             tile.setTranslateX(100 * (i % NUM_PER_ROW));
  69.             tile.setTranslateY(100 * (i / NUM_PER_ROW));
  70.             root.getChildren().add(tile);
  71.         }
  72.  
  73.         return root;
  74.     }
  75.  
  76.     @Override
  77.     public void start(Stage primaryStage) throws Exception {
  78.         primaryStage.setScene(new Scene(createContent()));
  79.         primaryStage.show();
  80.     }
  81.  
  82.     private class Tile extends StackPane {
  83.         private Text text = new Text();
  84.         private ImageView imgView = new ImageView();
  85.  
  86.         public Tile(String value, Image img) {
  87.             Rectangle border = new Rectangle(100, 100);
  88.             border.setFill(null);
  89.             border.setStroke(Color.BLACK);
  90.  
  91.             text.setText(value);
  92.  
  93.             imgView.setImage(img);
  94.             imgView.setFitWidth(80);
  95.             imgView.setFitHeight(80);
  96.  
  97.             setAlignment(Pos.CENTER);
  98.             getChildren().addAll(border);
  99.             getChildren().addAll(imgView);
  100.  
  101.             setOnMouseClicked(this::handleMouseClick);
  102.             close();
  103.         }
  104.  
  105.         public void handleMouseClick(MouseEvent event) {
  106.             if (isOpen() || clickCount == 0)
  107.                 return;
  108.  
  109.             clickCount--;
  110.  
  111.             if (selected == null) {
  112.                 selected = this;
  113.                 open(() -> {
  114.                 });
  115.             } else {
  116.                 open(() -> {
  117.                     if (!hasSameValue(selected)) {
  118.                         selected.close();
  119.                         this.close();
  120.                     }
  121.  
  122.                     selected = null;
  123.                     clickCount = 2;
  124.                 });
  125.             }
  126.         }
  127.  
  128.         public boolean isOpen() {
  129.             return imgView.getOpacity() == 1;
  130.         }
  131.  
  132.         public void open(Runnable action) {
  133.             FadeTransition ft = new FadeTransition(Duration.seconds(0.5), imgView);
  134.             ft.setToValue(1);
  135.             ft.setOnFinished(e -> action.run());
  136.             ft.play();
  137.         }
  138.  
  139.         public void close() {
  140.             FadeTransition ft = new FadeTransition(Duration.seconds(0.5), imgView);
  141.             ft.setToValue(0);
  142.             ft.play();
  143.         }
  144.  
  145.         public boolean hasSameValue(Tile other) {
  146.             return text.getText().equals(other.text.getText());
  147.         }
  148.     }
  149.  
  150.     public static void main(String[] args) {
  151.         launch(args);
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement