Java Reference
In-Depth Information
In practice, you usually need to use both layouts and bind together to create a dynamic layout. Table 5-10
explains for what situations each type of layout is most suitable.
Table 5-10. When to Use Bind Versus Layouts
Technique
Applicability
Use for resizing fixed Shapes , such as Rectangles , Ellipses , and Lines .
Easy to create layouts where Nodes overlap (also see StackPane and AnchorPane ).
Overuse can reduce performance or cause rendering artifacts.
bind
HBox/VBox
Use for vertical or horizontal alignment of nodes.
High performance; can be used for large number of Nodes .
Make use of alignment and grow constraints for more complex use cases.
FlowPane
Similar to HBox/VBox in usage.
Useful in situations where the layout should wrap lines to fit.
StackPane
Allows composition through overlapping of Nodes .
Very useful to create layered effects such as placing text over a background.
Be careful when stacking nodes where mouse events need to pass through.
Use the mouseTransparent property on nodes or consider an AnchorPane instead.
TilePane
Creates a tiled effect where all nodes are equally sized.
Can be forced to fill the parent via binding (make sure to set snapToPixel to false).
Is not a replacement for a general-purpose Grid (see GridPane ).
GridPane
Provides the most flexible layout container available.
Should be used wherever components need pixel-perfect row and column alignment.
Cannot be used to overlap components, except in conjunction with another layout.
AnchorPane
Highly specialized layout that can be used either for edge alignment or stretching.
Best layout for overlapping components (passes through mouse events cleanly).
S ome use cases better accomplished with margins/padding or other constraints.
Bringing Reversi to Life
Up to this point we have been singularly focused on layouts, which is important but doesn't make for a very interesting
game. However, the beauty of using dynamic layouts is that with a few enhancements to the game algorithm, we can
turn this static application into a dynamic, playable game.
Highlighting Legal Moves
The first step of a playable game algorithm is to make sure that pieces can be placed only on legal squares. Rather than
simply implement an algorithm in the back end, we take this as an opportunity to add a feature to the application
where it shows you all of the next available moves.
 
Search WWH ::




Custom Search