Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package memory;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import javafx.animation.FadeTransition;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class Memory extends Application {
- private static final int NUM_OF_PAIRS = 8;
- private static final int NUM_PER_ROW = 4;
- Image A = new Image("file:bilder/apple.png");
- Image B = new Image("file:bilder/ananas.png");
- Image C = new Image("file:bilder/kiwi.png");
- Image D = new Image("file:bilder/melone.png");
- Image E = new Image("file:bilder/banana.png");
- Image F = new Image("file:bilder/pear.png");
- Image G = new Image("file:bilder/raspberry.png");
- Image H = new Image("file:bilder/strawberry.png");
- Image I = new Image("file:bilder/cocnut.png");
- private Tile selected = null;
- private int clickCount = 2;
- private Parent createContent() {
- Pane root = new Pane();
- root.setPrefSize(400, 400);
- List<Tile> tiles = new ArrayList<>();
- tiles.add(new Tile("A", A));
- tiles.add(new Tile("A", A));
- tiles.add(new Tile("B", B));
- tiles.add(new Tile("B", B));
- tiles.add(new Tile("C", C));
- tiles.add(new Tile("C", C));
- tiles.add(new Tile("D", D));
- tiles.add(new Tile("D", D));
- tiles.add(new Tile("E", E));
- tiles.add(new Tile("E", E));
- tiles.add(new Tile("F", F));
- tiles.add(new Tile("F", F));
- tiles.add(new Tile("G", G));
- tiles.add(new Tile("G", G));
- tiles.add(new Tile("H", H));
- tiles.add(new Tile("H", H));
- Collections.shuffle(tiles);
- for (int i = 0; i < tiles.size(); i++) {
- Tile tile = tiles.get(i);
- tile.setTranslateX(100 * (i % NUM_PER_ROW));
- tile.setTranslateY(100 * (i / NUM_PER_ROW));
- root.getChildren().add(tile);
- }
- return root;
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- primaryStage.setScene(new Scene(createContent()));
- primaryStage.show();
- }
- private class Tile extends StackPane {
- private Text text = new Text();
- private ImageView imgView = new ImageView();
- public Tile(String value, Image img) {
- Rectangle border = new Rectangle(100, 100);
- border.setFill(null);
- border.setStroke(Color.BLACK);
- text.setText(value);
- imgView.setImage(img);
- imgView.setFitWidth(80);
- imgView.setFitHeight(80);
- setAlignment(Pos.CENTER);
- getChildren().addAll(border);
- getChildren().addAll(imgView);
- setOnMouseClicked(this::handleMouseClick);
- close();
- }
- public void handleMouseClick(MouseEvent event) {
- if (isOpen() || clickCount == 0)
- return;
- clickCount--;
- if (selected == null) {
- selected = this;
- open(() -> {
- });
- } else {
- open(() -> {
- if (!hasSameValue(selected)) {
- selected.close();
- this.close();
- }
- selected = null;
- clickCount = 2;
- });
- }
- }
- public boolean isOpen() {
- return imgView.getOpacity() == 1;
- }
- public void open(Runnable action) {
- FadeTransition ft = new FadeTransition(Duration.seconds(0.5), imgView);
- ft.setToValue(1);
- ft.setOnFinished(e -> action.run());
- ft.play();
- }
- public void close() {
- FadeTransition ft = new FadeTransition(Duration.seconds(0.5), imgView);
- ft.setToValue(0);
- ft.play();
- }
- public boolean hasSameValue(Tile other) {
- return text.getText().equals(other.text.getText());
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement