【问题标题】:JavaFX creating a game board with GridPaneJavaFX 使用 GridPane 创建游戏板
【发布时间】:2019-09-01 01:47:41
【问题描述】:

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

我也在与逻辑作斗争。我有一个带有 makeMove 函数的 int 棋盘矩阵,可以将玩家移动到矩阵中,尽管我无法让玩家在矩阵上移动并在图块索引上打印相应的“O”。

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

而且当玩家点击 GUI 方块时,也会做出相应的动作。

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

我创建了一个 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);
    }

【问题讨论】:

  • 所有文本元素都放在单元格(0,0) 中。请注意,您可以使用add 方法来缩短分配索引所需的代码,而不是使用static 方法来一一分配索引:gameBoard.add(tile, j, i);
  • 贴出来的代码id不清楚(这两个类有什么关系?)请发帖minimal reproducible example。还将您的帖子集中在一个问题上。

标签: java javafx


【解决方案1】:

如果您想在 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;
}

【讨论】:

    【解决方案2】:

    在您的循环中,您正在设置“平铺”节点的行和列,而不是“文本”节点。所以你的所有文本节点都在 0,0。

    您似乎希望瓷砖成为 c0der 建议的 StackPanes。文本节点位于图块顶部。

    我认为创建一个可能从 StackPane 派生的 Tile 对象并使用它来管理每个图块是有意义的。它将负责显示的背景颜色和文本。目前尚不清楚是否需要 Rectangle。您可以设置 StackPane 的背景和边框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-03
      • 2021-11-13
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      相关资源
      最近更新 更多