如何在场景的根 Group 内更新 GridPane

huangapple 未分类评论51阅读模式
英文:

How to update a GridPane inside of a Scene's root Group

问题

@Override
public void start(Stage primaryStage) throws Exception
{
    primaryStage.setTitle("Chess");
    primaryStage.setResizable(false);

    Player whitePlayer = new CPUPlayer(ChessColor.WHITE);
    Player blackPlayer = new CPUPlayer(ChessColor.BLACK);

    Board board = new Board();
    Scene scene = new Scene(board.root, 400.0, 400.0);

    Square startSquare = board.getSquare(1, 4);
    Square endSquare = board.getSquare(3, 4);

    board.printBoard();

    Move move = new Move(whitePlayer, startSquare, endSquare, board, MoveType.NORMAL);
    move.movePiece();

    board.printBoard();
    
    primaryStage.setScene(scene);
    primaryStage.show();     
}

public GridPane setPieces()
{
    GridPane pieces = new GridPane();
    int printRow = 0;

    for (int i = 7; i >= 0; i--)
    {
        for (int j = 0; j < 8; j++)
        {
            pieces.add(new ImageView(rightImageForPiece(getSquare(i, j).getPiece())), j, printRow);   
        }
        printRow++;
    }

    return pieces;
} 

public void setRoot()
{
    Group root = new Group();

    root.getChildren().add(squares);
    root.getChildren().add(setPieces());
    root.getChildren().add(buttons);

    this.root = root;
}

Board()
{
    setNewBoard();

    this.buttons = setButtons();
    this.squares = setSquares();
    this.pieces = setPieces();
    setRoot();
}

For the images referenced in your text, you can find them through the provided links:

Proof that the board can be displayed properly
Proof backend changes

英文:

I have coded a console-based chess game in Java, and now want to make it into a GUI game using JavaFx. In the main Scene, the root is a Group containing three GridPanes: one Rectangle GridPane for the colored squares, one ImageView GridPane for the piece sprites, and a final Button GridPane so that the user can select a piece on the board and move it somewhere.

Right now, all that I want to happen is for my scene to take in a board position, and then display the appropriate board every time as moves are made.

The code to display a board position works, as whenever I initialize the Scene root with the starting board position Group, the pieces are displayed properly. The issue is that after a player makes a move, no changes are made to what is displayed.

Proof that the board can be displayed properly

Although, after I make a move on the board, nothing changes. The move is made on the actual board object in the backend, but the board that is displayed does not update. It just stays the same as it was above.

Proof backend changes

Right now, the code in my start method looks like this:

@Override
public void start(Stage primaryStage) throws Exception
{
    primaryStage.setTitle(&quot;Chess&quot;);
    primaryStage.setResizable(false);

    Player whitePlayer = new CPUPlayer(ChessColor.WHITE);
    Player blackPlayer = new CPUPlayer(ChessColor.BLACK);

    Board board = new Board();
    Scene scene = new Scene(board.root, 400.0, 400.0);

    Square startSquare = board.getSquare(1, 4);
    Square endSquare = board.getSquare(3, 4);

    //printing these out to prove move actually happens on board object
    board.printBoard();

    Move move = new Move(whitePlayer, startSquare, endSquare, board, MoveType.NORMAL);
    move.movePiece();
    //after this I have tried updating the entire Group containing all the GridPanes, 
    //as well as just the GridPane containing the piece ImageViews. Neither did anything.

    
    //printing these out to prove move actually happens on board object
    board.printBoard();
    
    primaryStage.setScene(scene);
    primaryStage.show();     
}

This is how I set the GridPane for the piece sprites:

public GridPane setPieces()
{
    GridPane pieces = new GridPane();
    int printRow = 0;

    for (int i = 7; i &gt;= 0; i--)
    {
        for (int j = 0; j &lt; 8; j++)
        {
            pieces.add(new ImageView(rightImageForPiece(getSquare(i, j).getPiece())), j, printRow);   
        }
        printRow++;
    }

    return pieces;
} 

This is how I set the Group root:

public void setRoot()
{
    Group root = new Group();

    root.getChildren().add(squares);
    root.getChildren().add(setPieces());
    root.getChildren().add(buttons);

    this.root = root;
}

And this is the constructor for the Board:

Board()
{
    //just for the backend
    setNewBoard();

    //for javafx
    this.buttons = setButtons();
    this.squares = setSquares();
    this.pieces = setPieces();
    setRoot();
}

In summary, is there a way, once I have set the Group root of my scene, to update an individual GridPane so that it displays changes made on the chess board.

If there is any additional information needed, just let me know since this is my first time asking a question here.

答案1

得分: 0

我解决了这个问题,我只需要在另一个线程中添加更新棋盘的后端。

英文:

I solved the problem, I just needed to add the backend that updates the board to another thread.

huangapple
  • 本文由 发表于 2020年5月29日 04:48:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/62074273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定