현재 세트의 카드 게임을 만들고 있습니다. 보드에있는 카드를 클릭하면 배경이 회색으로 바뀌고 (선택되었음을 나타냄) 카드를 두 번 클릭하면 배경이 흰색으로 되돌아갑니다 (선택되지 않았 음을 나타냄).). 하지만 내가 도움이 필요한 코드를 어떻게 할 지 확신하지 못하는 것은 카드의 실제 요소를 ArrayList의 일부 유형으로 저장하는 것입니다 (따라서 카드를 나중에 설정할지 여부를 결정할 수 있습니다). 한 번에 3 장의 카드 만 선택할 수 있도록하는 방법은 무엇입니까?JavaFX에서 클릭하면 ArrayList에 요소를 저장합니까?
여기까지 내 코드가 있습니다. 그 중 일부 테스트 코드가있을 수 있지만 아무것도하지 않습니다.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.transform.Rotate;
import javafx.scene.text.*;
import javafx.scene.image.Image;
import javafx.scene.paint.ImagePattern;
import java.util.ArrayList;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
public class GameFX extends Application {
public static void main(String [] args) {
Application.launch((String[]) args);
}
public void start(Stage stage) throws Exception {
// Variables
Game g = new Game();
Board b = g.getBoard();
Pane pane = new Pane();
BackgroundFill grayBG = new BackgroundFill(Color.LIGHTGRAY, new CornerRadii(1), new Insets(0.0, 0.0, 0.0, 0.0));
BackgroundFill darkGrayBG = new BackgroundFill(Color.DARKGRAY, new CornerRadii(1), new Insets(0.0, 0.0, 0.0, 0.0));
BorderPane main = new BorderPane();
Scene scene = new Scene(main);
Label label = new Label("Set Game");
Label instructionsLabel = new Label("Click on cards to select them, Double-click to deselect them, type 'A' to add three cards, type 'E' to end the game");
Label actionsLabel = new Label("(A)dd 3 cards, (E)nd Game");
Label cardsRemaining = new Label("There are " + "X " + "cards remaining in the deck");
GridPane topPane = new GridPane();
GridPane centerPane = new GridPane();
GridPane rightPane = new GridPane();
GridPane bottomPane = new GridPane();
boolean stop = false;
// Configure pane
pane.setPrefHeight(500);
pane.setPrefWidth(1000);
centerPane.setPrefHeight(500);
centerPane.setPrefWidth(700);
rightPane.setPrefWidth(300);
rightPane.setPrefHeight(500);
pane.setBackground(new Background(grayBG));
rightPane.setBackground(new Background(darkGrayBG));
// Create initial board
updateBoard(b, centerPane);
// Configure label
label.setTextFill(Color.RED);
// Add content to topPane
topPane.add(label, 0, 0);
topPane.add(instructionsLabel, 0, 1);
topPane.add(actionsLabel, 0, 2);
topPane.add(cardsRemaining, 0, 3);
// Set panes in BorderPane layout
main.setCenter(centerPane);
main.setTop(topPane);
//main.setRight(rightPane);
// Request focus and configure window
stage.setTitle("Set");
stage.setScene(scene);
stage.show();
centerPane.requestFocus();
centerPane.setOnKeyPressed(e -> {
switch (e.getCode()) {
case A: {
System.out.println("A was pressed");
if (b.numCols() < 6) {
g.add3();
}
updateBoard(b, centerPane);
break;
}
case E: {
Platform.exit();
break;
}
}
});
}
public static Pane createCard(int shape, int fill, int color, int number) {
Pane cardPane = new Pane();
Pane shapePane = new Pane();
ArrayList<Rectangle> squares = new ArrayList<Rectangle>();
ArrayList<Circle> circles = new ArrayList<Circle>();
ArrayList<Rectangle> diamonds = new ArrayList<Rectangle>();
String stripesRedURL = "stripesRed.png";
String stripesPurpleURL = "stripesPurple.png";
String stripesGreenURL = "stripesGreen.png";
Image stripesRed = new Image(stripesRedURL);
Image stripesPurple = new Image(stripesPurpleURL);
Image stripesGreen = new Image(stripesGreenURL);
BackgroundFill cardBG = new BackgroundFill(Color.WHITE,
new CornerRadii(1),
new Insets(0.0, 0.0, 0.0, 0.0));
if (shape == 0) {
for (int i=0; i < number; i++) {
circles.add(new Circle(50,20 + (i * 30),10));
}
for(Circle circle: circles) {
if (fill == 0) {
if (color == 0) {
circle.setFill(Color.RED);
} else if (color == 1) {
circle.setFill(Color.PURPLE);
} else if (color == 2) {
circle.setFill(Color.GREEN);
}
} else if (fill == 1) {
if (color == 0) {
circle.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 1) {
circle.setFill(new ImagePattern(stripesPurple, 0, 0, 40, 40, false));
} else if (color == 2) {
circle.setFill(new ImagePattern(stripesGreen, 0, 0, 40, 40, false));
}
} else if (fill == 2) {
if (color == 0) {
circle.setStroke(Color.RED);
circle.setFill(Color.WHITE);
} else if (color == 1) {
circle.setStroke(Color.PURPLE);
circle.setFill(Color.WHITE);
} else if (color == 2) {
circle.setStroke(Color.GREEN);
circle.setFill(Color.WHITE);
}
}
}
System.out.println(circles);
for(Circle circle: circles) {
shapePane.getChildren().add(circle);
}
} else if (shape == 1) {
for (int i=0; i < number; i++) {
squares.add(new Rectangle(40, 10 + (i * 30), 20, 20));
}
for(Rectangle square: squares) {
if (fill == 0) {
if (color == 0) {
square.setFill(Color.RED);
} else if (color == 1) {
square.setFill(Color.PURPLE);
} else if (color == 2) {
square.setFill(Color.GREEN);
}
} else if (fill == 1) {
if (color == 0) {
square.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 1) {
square.setFill(new ImagePattern(stripesPurple, 0, 0, 40, 40, false));
} else if (color == 2) {
square.setFill(new ImagePattern(stripesGreen, 0, 0, 40, 40, false));
}
} else if (fill == 2) {
if (color == 0) {
square.setStroke(Color.RED);
square.setFill(Color.WHITE);
} else if (color == 1) {
square.setStroke(Color.PURPLE);
square.setFill(Color.WHITE);
} else if (color == 2) {
square.setStroke(Color.GREEN);
square.setFill(Color.WHITE);
}
}
}
System.out.println(squares);
for(Rectangle square: squares) {
shapePane.getChildren().add(square);
}
} else if (shape == 2) {
for (int i=0; i < number; i++) {
diamonds.add(new Rectangle(40, 10 + (i * 30), 20, 20));
}
for(Rectangle diamond: diamonds) {
diamond.getTransforms().add(new Rotate(45,50,50));
if (fill == 0) {
if (color == 0) {
diamond.setFill(Color.RED);
} else if (color == 1) {
diamond.setFill(Color.PURPLE);
} else if (color == 2) {
diamond.setFill(Color.GREEN);
}
} else if (fill == 1) {
if (color == 0) {
diamond.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 1) {
diamond.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 2) {
diamond.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
}
} else if (fill == 2) {
if (color == 0) {
diamond.setStroke(Color.RED);
diamond.setFill(Color.WHITE);
} else if (color == 1) {
diamond.setStroke(Color.PURPLE);
diamond.setFill(Color.WHITE);
} else if (color == 2) {
diamond.setStroke(Color.GREEN);
diamond.setFill(Color.WHITE);
}
}
}
System.out.println(diamonds);
for(Rectangle diamond: diamonds) {
shapePane.getChildren().add(diamond);
}
}
cardPane.setBackground(new Background(cardBG));
cardPane.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
cardPane.setPrefHeight(100);
cardPane.setPrefWidth(100);
cardPane.getChildren().addAll(shapePane);
//If card is selected, background changes color
cardPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
//int selected = 0;
ArrayList<String> selected = new ArrayList<String>();
@Override
public void handle(MouseEvent me) {
if(me.getClickCount() == 1){
cardPane.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));
selected.add("1");
System.out.println(selected);
}
else if(me.getClickCount() == 2){
cardPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
selected.remove("1");
System.out.println(selected);
}
else{
System.out.println("nothing");
}
}
});
return cardPane;
}
public void updateBoard(Board b, GridPane pane) {
ArrayList<ArrayList<BoardSquare>> boardSquares = b.getBoardSquares();
System.out.println(boardSquares);
for (ArrayList<BoardSquare> boardRow: boardSquares) {
for (BoardSquare boardSquare: boardRow) {
Card boardCard = boardSquare.getCard();
pane.add(createCard(boardCard.getShape(), boardCard.getShading(), boardCard.getColor(), boardCard.getNumber() + 1), boardSquare.getRow(), boardSquare.getColumn());
}
}
}
}