Java Reference
In-Depth Information
13
Pane pane =
new
Pane();
create a pane
14
15
// Create rectangles and add to pane
16 Rectangle r1 =
new
Rectangle(
25
,
10
,
60
,
30
);
17 r1.setStroke(Color.BLACK);
18 r1.setFill(Color.WHITE);
19 pane.getChildren().add(
new
Text(
10
,
27
,
"r1"
));
20
create a rectangle
r1
set
r1
's properties
pane.getChildren().add(r1);
add
r1
to pane
21
22 Rectangle r2 =
new
Rectangle(
25
,
50
,
60
,
30
);
23 pane.getChildren().add(
new
Text(
10
,
67
,
"r2"
));
24
create rectangle
r2
pane.getChildren().add(r2);
add
r2
to pane
25
26 Rectangle r3 =
new
Rectangle(
25
,
90
,
60
,
30
);
27 r3.setArcWidth(
15
);
28 r3.setArcHeight(
25
);
29 pane.getChildren().add(
new
Text(
10
,
107
,
"r3"
));
30
create rectangle
r3
set
r3
's arc width
set
r3
's arc height
pane.getChildren().add(r3);
31
32
for
(
int
i =
0
; i <
4
; i++) {
33 Rectangle r =
new
Rectangle(
100
,
50
,
100
,
30
);
34 r.setRotate(i *
360
/
8
);
35 r.setStroke(Color.color(Math.random(), Math.random(),
36 Math.random()));
37 r.setFill(Color.WHITE);
38
create a rectangle
rotate a rectangle
pane.getChildren().add(r);
add rectangle to pane
39 }
40
41
// Create a scene and place it in the stage
42 Scene scene =
new
Scene(pane,
250
,
150
);
43 primaryStage.setTitle(
"ShowRectangle"
);
// Set the stage title
44 primaryStage.setScene(scene);
// Place the scene in the stage
45 primaryStage.show();
// Display the stage
46 }
47 }
The program creates multiple rectangles. By default, the fill color is black. So a rectangle
is filled with black color. The stroke color is white by default. Line 17 sets stroke color of
rectangle
r1
to black. The program creates rectangle
r3
(line 26) and sets its arc width and arc
height (lines 27-28). So
r3
is displayed as a rounded rectangle.
The program repeatedly creates a rectangle (line 33), rotates it (line 34), sets a random
stroke color (lines 35-36), its fill color to white (line 37), and adds the rectangle to the pane
(line 38).
If line 37 is replaced by the following line
r.setFill(
null
);
the rectangle is not filled with a color. So they are displayed as shown in Figure 14.31c.
14.11.4
Circle
and
Ellipse
You have used circles in several examples early in this chapter. A circle is defined by its
parameters
centerX
,
centerY
, and
radius
. The
Circle
class defines a circle. The UML
diagram for the
Circle
class is shown in Figure 14.32.
An ellipse is defined by its parameters
centerX
,
centerY
,
radiusX
, and
radiusY
, as
shown in Figure 14.34a. The
Ellipse
class defines an ellipse. The UML diagram for the
Ellipse
class is shown in Figure 14.33. Listing 14.17 gives an example that demonstrates
ellipses, as shown in Figure 14.34b.
Search WWH ::
Custom Search