Java Reference
In-Depth Information
Chapter 5
Building Dynamic UI Layouts in JavaFX
When I am working on a problem, I never think about beauty. I think only of how to solve the
problem. But when I have finished, if the solution is not beautiful, I know it is wrong.
—Buckminster Fuller
JavaFX has facilities for creating dynamic layouts that allow you to easily create beautiful-looking UIs that scale to
any resolution and are backed by clean code. At your disposal you have the simple, yet elegant, binding facility;
powerful custom layouts built on top of the Pane and Region classes; and the built-in layouts that include HBox , VBox ,
AnchorPane , BorderPane , FlowPane , TilePane , StackPane , GridPane , and TextFlow .
In this chapter we show how you can leverage these dynamic layout mechanisms to build complicated UIs with
zero static positioning.
Introducing JavaFX Reversi
To demonstrate the power of dynamic layout in JavaFX, the goal of this chapter is to build a fully functional version
of the popular Reversi game. Reversi is a game of strategy where players take turns on an eight-by-eight game board
placing black and white pieces. The objective of the game is to have the most pieces on the board by surrounding your
opponent's pieces and flipping them over to your color.
Originally invented in 1880 by Lewis Waterman and James Mollett, Reversi gained considerable popularity in
nineteenth-century England, and was one of the first titles published by German game manufacturer Ravensburger.
It is more commonly known today as Othello, which is trademarked and sold by Pressman.
The rules of Reversi are extremely simple, which lets us focus on the JavaFX layout. To make things a little more
challenging, we are going to bring Reversi into the twenty-first century with a modern RIA-style interface and fully
resizable layout.
Board Layout and Basic Rules
Reversi is a turn-based game where two players choose white and black sides. Each player gets 32 pieces to play; the
first player is black.
The initial board setup has four pieces placed in alternating cells in the center of the board (see Figure 5-1 ).
 
Search WWH ::




Custom Search