Java Reference
In-Depth Information
21
pane.getChildren().add(circle);
add circle to pane
22
23 // Create a scene and place it in the stage
24 Scene scene = new Scene(pane, 200 , 200 );
25 primaryStage.setTitle( " ShowCircle " ); // Set the stage title
26 primaryStage.setScene(scene); // Place the scene in the stage
27
add pane to scene
primaryStage.show(); // Display the stage
display stage
28 }
29 }
main method omitted
(0, 0)
(0, 0)
(100, 100)
(100, 100)
(a)
(b)
F IGURE 14.5
(a) A circle is displayed in the center of the scene. (b) The circle is not
centered after the window is resized.
The program creates a Circle (line 12) and sets its center at (100, 100) (lines 13-14),
which is also the center for the scene, since the scene is created with the width and height of
200 (line 24). The radius of the circle is set to 50 (line 15). Note that the measurement units
for graphics in Java are all in pixels .
The stroke color (i.e., the color to draw the circle) is set to black (line 16). The fill color
(i.e., the color to fill the circle) is set to white (line 17). You may set the color to null to
specify that no color is set.
The program creates a Pane (line 20) and places the circle in the pane (line 21). Note that
the coordinates of the upper left corner of the pane is ( 0 , 0 ) in the Java coordinate system, as
shown in Figure 14.6a, as opposed to the conventional coordinate system where (0, 0) is at the
center of the window, as shown in Figure 14.6b. The x -coordinate increases from left to right
and the y -coordinate increases downward in the Java coordinate system.
The pane is placed in the scene (line 24) and the scene is set in the stage (line 26). The circle
is displayed in the center of the stage, as shown in Figure 14.5a. However, if you resize the
window, the circle is not centered, as shown in Figure 14.5b. In order to display the circle cen-
tered as the window resizes, the x - and y -coordinates of the circle center need to be reset to the
center of the pane. This can be done by using property binding, introduced in the next section.
pixels
set color
x
Y axis
(0, 0)
X axis
y
( x , y )
(0, 0)
X axis
Conventional
Coordinate
System
Java Coordinate
System
Y axis
(a)
(b)
F IGURE 14.6
The Java coordinate system is measured in pixels, with (0, 0) at its
upper-left corner.
 
Search WWH ::




Custom Search