Java Reference
In-Depth Information
22
pane.getChildren().add(imageView2);
add an image to pane
23
24
ImageView imageView3 =
new
ImageView(image);
create an image view
rotate an image view
add an image to pane
25
imageView3.setRotate(
90
);
26
pane.getChildren().add(imageView3);
27
28
// Create a scene and place it in the stage
29 Scene scene =
new
Scene(pane);
30 primaryStage.setTitle(
"
ShowImage
"
);
// Set the stage title
31 primaryStage.setScene(scene);
// Place the scene in the stage
32 primaryStage.show();
// Display the stage
33 }
34 }
F
IGURE
14.14
An image is displayed in three image views placed in a pane.
The program creates an
HBox
(line 14). An
HBox
is a pane that places all nodes horizon-
tallly in one row. The program creates an
Image
, and then an
ImageView
for displaying the
iamge, and places the
ImageView
in the
HBox
(line 17).
The program creates the second
ImageView
(line 19), sets its
fitHeight
and
fitWidth
properties (lines 20-21) and places the
ImageView
into the
HBox
(line 22). The program cre-
ates the third
ImageView
(line 24), rotates it 90 degrees (line 25), and places it into the
HBox
(line 26). The
setRotate
method is defined in the
Node
class and can be used for any node.
Note that an
Image
object can be shared by multiple nodes. In this case, it is shared by three
ImageView
. However, a node such as
ImageView
cannot be shared. You cannot place an
ImageView
multiple times into a pane or scene.
Note that you must place the image file in the same directory as the class file, as shown in
the following figure.
Directory
ShowImage.class
image
us.gif
If you use the URL to locate the image file, the URL protocal http:// must be present. So
the following code is wrong.
new
Image(
"www.cs.armstrong.edu/liang/image/us.gif"
);
It must be replaced by
new
Image(
"http://www.cs.armstrong.edu/liang/image/us.gif"
);
Search WWH ::
Custom Search