Java Reference
In-Depth Information
How it works...
In this recipe, we have loaded and programmatically manipulated graphics objects
encapsulated in JavaFX's FXZ file. The recipe uses NetBeans to generate a UI stub
class that exposes all of the objects inside the FXZ file. Let's see how the UI stub works:
F UI stub class —the UI stub extends FXDNode . In our recipe, the stub class is named
NbGeneratedSymbols . It is a class designed to represent graphics objects from
FXZ files. The UI stub class encapsulates all top-level group nodes (mapped to layers
in original artwork) declared in the FXD file and exposes them as public properties in
the generated class.
F FXDNODE.contentLoaded() —this method is called when the class has fully
loaded all graphics objects. Here, it is overwritten to populate the public variables
that expose the content of the FXD file as object properties. Notice the use of
function getNode(id:String) , used here to retrieve a graphics node by
its id .
The other portion of this recipe deals with the usage of the generated UI stub. Let's
analyze what is going on:
First, the code declares variable symbols as an instance of
NbGeneratedSymbols class.
Next, the code pulls out the image for the target and the
triangle as ImageView instances from the symbols object.
Then, symbols receives the window's focus; this will enable it to
receive keyboard events.
Next, the code adds event-handler function
onKeypressed(e:KeyEvent) to the symbols object to process
keyboard input events. Here, Left arrow , Right arrow , and Spacebar
key events are used to provide interactivity to the game. When the
left or right arrow moves, it updates the position of both the triangle
and the target icons. When the Spacebar key is pressed, the code
hides the icon which intersects the target icon.
There's more...
Earlier in this recipe, we have seen how to generate UI stub classes from FXZ files using
NetBeans to access graphics objects inside FXZ files. If you do not use NetBeans or want
tighter control of how FXZ objects are loaded, you can use the FXD API directly, located in the
javafx.fxd package, to customize how objects are loaded from the FXZ files. You can see
listing of these techniques in files ch08/source-code/src/fxzdemo/FxdNodeLoad1.fx ,
FxdNodeLoad2.fx , FxdNodeLoad3.fx , and FxdNodeLoad4.fx.
Search WWH ::

Custom Search