Java Reference
In-Depth Information
8
9
public class
ShowPolygon
extends
Application {
10 @Override
// Override the start method in the Application class
11
public void
start(Stage primaryStage) {
12
// Create a pane, a polygon, and place polygon to pane
13 Pane pane =
new
Pane();
14 Polygon polygon =
new
Polygon();
15 pane.getChildren().add(polygon);
16 polygon.setFill(Color.WHITE);
17 polygon.setStroke(Color.BLACK);
18
create a pane
create a polygon
add polygon to pane
ObservableList<Double> list = polygon.getPoints();
get a list of points
19
20
final double
WIDTH =
200
, HEIGHT =
200
;
21
double
centerX = WIDTH /
2
, centerY = HEIGHT /
2
;
22
double
radius = Math.min(WIDTH, HEIGHT) *
0.4
;
23
24
// Add points to the polygon list
25
for
(
int
i =
0
; i <
6
; i++) {
26
list.add(centerX + radius * Math.cos(
2
* i * Math.PI /
6
));
add
x-
coordinate of a point
add
y-
coordinate of a point
27
list.add(centerY - radius * Math.sin(
2
* i * Math.PI /
6
));
28 }
29
30
// Create a scene and place it in the stage
31 Scene scene =
new
Scene(pane, WIDTH, HEIGHT);
32 primaryStage.setTitle(
"
ShowPolygon
"
);
// Set the stage title
33 primaryStage.setScene(scene);
// Place the scene in the stage
34 primaryStage.show();
// Display the stage
35 }
36 }
add pane to scene
The program creates a polygon (line 14) and adds it to a pane (line 15). The
polygon
.getPoints()
method returns an
ObservableList<Double>
(line 18), which contains
the
add
method for adding an element to the list (lines 26-27). Note that the value
passed to
add(value)
must be a
double
value. If an
int
value is passed, the
int
value
would be automatically boxed into an
Integer
. This would cause an error because the
ObservableList<Double>
consists of
Double
elements.
The loop adds six points to the polygon (lines 25-28). Each point is represented by its
x
- and
y
-coordinates. For each point, its
x
-coordinate is added to the polygon's list (line 26)
and then its
y
-coordinate is added to the list (line 27). The formula for computing the
x
- and
y
-coordinates for a point in the hexagon is illustrated in Figure 14.40a.
If you replace
Polygon
by
Polyline
, the program displays a polyline as shown in
Figure 14.40b. The
Polyline
class is used in the same way as
Polygon
except that the start-
ing and ending point are not connected in
Polyline
.
14.27
✓
✓
How do you display a text, line, rectangle, circle, ellipse, arc, polygon, and polyline?
Check
14.28
Write code fragments to display a string rotated 45 degrees in the center of the pane.
Point
14.29
Write code fragments to display a thick line of
10
pixels from (
10
,
10
) to (
70
,
30
).
14.30
Write code fragments to fill red color in a rectangle of width
100
and height
50
with
the upper-left corner at (
10
,
10
).
14.31
Write code fragments to display a round-cornered rectangle with width
100
, height
200
with the upper-left corner at (
10
,
10
), corner horizontal diameter
40
, and corner
vertical diameter
20
.
14.32
Write code fragments to display an ellipse with horizontal radius
50
and vertical
radius
100
.
14.33
Write code fragments to display the outline of the upper half of a circle with radius
50
.
Search WWH ::
Custom Search