JavaFX 使用 GridPane 创建游戏板

标签 java javafx

当应用程序运行时,游戏板显示正常,尽管单击图 block 时文本始终显示在板的左上角 [0][0]。

我也在逻辑上挣扎。我有一个带有 makeMove 函数的 int 棋盘矩阵,它将玩家移动添加到矩阵中,尽管我无法让玩家在矩阵上移动并在图 block 索引上打印相应的“O”。

我如何创建一个可以传递行、列索引并更改棋盘矩阵并在 GUI 上打印玩家棋子的函数(例如用于 AI 移动)

当玩家点击 GUI 方 block 时,就会做出相应的 Action 。

到目前为止,我已经创建了一个 GridPane,并在 GUI 的每个索引处添加了带有文本的矩形。

我创建了一个 Board 类,它构造了一个 int 棋盘矩阵来保存玩家的移动(P1、P2、空)。

public class Board {

    private int[][] board_matrix;
    private int board_size;
    private int win_length;

    public Board(int board_size, int win_length) {
        this.board_matrix = new int[board_size][board_size];
        this.board_size = board_size;
        this.win_length = win_length;

        for (int i = 0; i < board_size; i++) {
            for (int j = 0; j < board_size; j++) {
                this.board_matrix[i][j] = 0;
            }
        }
    }

    public void make_move(int player, int x_pos, int y_pos) {
        if (player == 1) board_matrix[x_pos][y_pos] = 1;
        else board_matrix[x_pos][y_pos] = 2;
    }

public class BoardGUI_ extends Application {

    private final int BOARD_SIZE = 15;

    public Parent createBoard() {
        GridPane gameBoard = new GridPane();
        gameBoard.setPrefSize(755, 755);

        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {

                Rectangle tile = new Rectangle(50, 50);
                tile.setFill(Color.BURLYWOOD);
                tile.setStroke(Color.BLACK);

                Text text = new Text();
                text.setFont(Font.font(40));


                GridPane.setRowIndex(tile, i);
                GridPane.setColumnIndex(tile, j);

                tile.setOnMouseClicked(event -> drawMove(text));

                gameBoard.getChildren().addAll(tile, text);
            }
        }
        return gameBoard;
    }

    public void drawMove(Text text) {
        text.setText("O");
        text.setFill(Color.BLACK);
    }

最佳答案

如果您想在 Rectangle 对象之上添加 Text 对象,请将两者包装在 StackPane 中:

public Parent createBoard() {

    GridPane gameBoard = new GridPane();
    gameBoard.setPrefSize(755, 755);

    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {

            Rectangle tile = new Rectangle(50, 50);
            tile.setFill(Color.BURLYWOOD);
            tile.setStroke(Color.BLACK);

            Text text = new Text();
            text.setFont(Font.font(40));
            gameBoard.add(new StackPane(tile, text), j, i);

            //GridPane.setRowIndex(tile, i);
            //GridPane.setColumnIndex(tile, j);   
            //gameBoard.getChildren().addAll(tile, text);
            tile.setOnMouseClicked(event -> drawMove(text));
        }
    }
    return gameBoard;
}

关于JavaFX 使用 GridPane 创建游戏板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57742682/

相关文章:

java - 为什么Gson库在调试时显示空值?

java - 将 "PNG"图像文件加载到我的 Java Fx 应用程序时,Java fx 引擎失败

Windows 中的 JavaFx 应用程序未正确显示文本

java - GWT URL 路由和其他功能

java - JSF 中电话号码的自定义 validator

java - Guava 事件总线 : NullPointerException when Posting from Thread to UI

JavaFX Beans 绑定(bind)突然停止工作

JavaFX - 启动画面未显示

java - 在方法内重用变量是否安全?

java - 微调器以 XML 格式显示但在加载应用程序时不显示