Java Reference
In-Depth Information
The initial stage is created by the JavaFX runtime, and passed to you via the start() method, as described in the
previous paragraph. The Stage class has a set of properties and methods. Some of these properties and methods, as
shown in the following code snippet from the listing, are as follows.
A scene that contains the graphical nodes in the UI
A title that appears in the title bar of the window (when deployed on the desktop)
Stage
The visibility of the
stage.setScene(scene);
stage.setTitle("Hello Earthrise");
stage.show();
A Scene is the top container in the JavaFX scene graph. A Scene holds the graphical elements that are displayed
on the Stage . Every element in a Scene is a graphical node, which is any class that extends javafx.scene.Node . The
scene graph is a hierarchical representation of the Scene . Elements in the scene graph may contain child elements,
and all of them are instances of the Node class.
The Scene class contains a number of properties, such as its width and height. A Scene also has a property named
root that holds the graphical elements that are displayed in the Scene , in this case a Group instance that contains an
ImageView instance (which displays an image) and a Group instance. Nested within the latter Group is a Text instance
(which is a graphical element, usually called a graphical node, or simply node).
Notice that the root property of the Scene contains an instance of the Group class. The root property may
contain an instance of any subclass of javafx.scene.Node , and typically contains one capable of holding its own set
of Node instances. Take a look at the JavaFX API documentation that we showed you how to access in the “Use the
Official Specifications” section earlier and check out the Node class to see the properties and methods available to any
graphical node. Also, take a look at the ImageView class in the javafx.scene.image package and the Group class in the
javafx.scene package. In both cases, they inherit from the Node class.
We can't emphasize enough the importance of having the JavaFX api documentation handy while reading this
book. as classes, variables, and functions are mentioned, it's a good idea to look at the documentation to get more
information. in addition, this habit helps you become more familiar with what is available to you in the api.
Tip
Displaying Images
As shown in the following code, displaying an image entails using an ImageView instance in conjunction with an
Image instance.
Image image = new Image (" http://projavafx.com/images/earthrise.jpg " );
ImageView imageView = new ImageView(image);
The Image instance identifies the image resource and loads it from the URL assigned to its URL variable. Both of
these classes are located in the javafx.scene.image package.
Displaying Text
In the example, we created a Text Node as follows:
Text textRef = new Text(message);
 
 
Search WWH ::




Custom Search