javajavafxalignmentborderpanepanes

JavaFX Node Alignment in BorderPanes


The issue that I'm having is when I attempt to add the tiles to the center of a border pane and then add that border pane to the center of a root border pane, the alignment is far from correct.

Here is where I created a main or "root" pane that I named mainPane. Then I created bottomPane and centerPane to hold items that will eventually be added to the bottom and center of mainPane:

BorderPane mainPane = new BorderPane();

BorderPane bottomPane = new BorderPane();
BorderPane centerPane = new BorderPane();

Button quitButton = new Button("Quit Game");
bottomPane.setRight(quitButton);

Text gameWinLabel = new Text();
gameWinLabel.setText("You win!");
gameWinLabel.setFont(Font.font("Times New Roman", 50));
gameWinLabel.setVisible(false);
bottomPane.setCenter(gameWinLabel);

Then I wrote the code that would generate the tiles for the memory game:

char c = 'A';
List<Tile> tiles = new ArrayList<>();
for (int i = 0; i < NUM_OF_PAIRS; i++) {
    tiles.add(new Tile(String.valueOf(c)));
    tiles.add(new Tile(String.valueOf(c)));
    c++;
}

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));
    centerPane.getChildren().add(tile);
}

Lastly, I anchored the container panes to mainPane:

mainPane.setBottom(bottomPane);
mainPane.setCenter(centerPane);

This is the current output: This is the current output

The ultimate goal is to have the game centered in the center of the mainPane.

Thanks in advance for any advice!


Solution

  • To avoid a lot of struggle, I adjusted the size of mainPane to fit the size of the game board. I realize this isn't really an answer to the alignment, but for making the game board nicer (Which was my goal), I think my solution is ok.

    Thanks for your time and help anyway!