Java Reference
In-Depth Information
The completed layout makes use of a TilePane for splitting the background, a StackPane for layering nodes,
and binding to make the contents resize with the Scene . To prevent wrapping artifacts on fractional tile sizes, we set
the snapToPixels property to false. This allows fractional tile widths and prevents rounding errors from causing our
layout to wrap to the next line.
The end result is that the layout resizes correctly when the size of the Scene is changed, including the StackPane
with a black background, which always occupies half the area, as shown in Figure 5-6 .
Figure 5-6. Result of running the node alignment example
Using FlowPane and Boxes for Directional Alignment
In the previous sections we showed how you can use StackPane and TilePane to create dynamic nested UIs, but what
if you simply want to arrange nodes along a vertical or horizontal line? This is where the directional layouts, HBox , VBox ,
and FlowPane , come in. They allow you to lay out a string of nodes at their preferred size with or without wrapping.
To demonstrate directional node alignment, we show you how to implement the next piece of the Reversi UI: the
player score box. There are two score boxes for the players, each with very similar content:
Score: The number of pieces of the player's color on the board
Player Color: The color of the player's pieces
Turns Remaining: The number of turns remaining
Before starting on the UI, this is a good time to flush out the model with additional methods to capture these
requirements. Listing 5-5 shows an example implementation of getScore and getTurnsRemaining that returns exactly
what we need to populate the player score box UI.
Listing 5-5. Additional Model Methods to Implement Player Score Back End
public NumberExpression getScore(Owner owner) {
NumberExpression score = new SimpleIntegerProperty();
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
score = score.add(Bindings.when(board[i][j].isEqualTo(owner)).then(1).otherwise(0));
}
}
return score;
}
public NumberBinding getTurnsRemaining(Owner owner) {
NumberExpression emptyCellCount = getScore(Owner.NONE);
return Bindings.when(turn.isEqualTo(owner))
.then(emptyCellCount.add(1).divide(2))
.otherwise(emptyCellCount.divide(2));
}
 
Search WWH ::




Custom Search