Java Reference
In-Depth Information
36
polygon2.addPoint(
200
,
220
);
polygon2.addPoint(
130
,
180
);
g.fillPolygon(polygon2);
37
38
39
}
40
}
// end class PolygonsJPanel
Fig. 13.27
|
Polygons displayed with
drawPolygon
and
fillPolygon
. (Part 2 of 2.)
1
// Fig. 13.28: DrawPolygons.java
2
// Drawing polygons.
3
import
javax.swing.JFrame;
4
5
public
class
DrawPolygons
6
{
7
// execute application
8
public
static
void
main(String[] args)
9
{
10
// create frame for PolygonsJPanel
11
JFrame frame =
new
JFrame(
"Drawing Polygons"
);
12
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
13
14
PolygonsJPanel polygonsJPanel =
new
PolygonsJPanel();
15
frame.add(polygonsJPanel);
16
frame.setSize(
280
,
270
);
17
frame.setVisible(
true
);
18
}
19
}
// end class DrawPolygons
Result of line 28
Result of line 18
Result of line 37
Result of line 23
Fig. 13.28
|
Drawing polygons.
Lines 16-17 of Fig. 13.27 create two
int
arrays and use them to specify the points for
Polygon
polygon1
. The
Polygon
constructor call in line 18 receives array
xValues
, which
contains the
x
-coordinate of each point; array
yValues
, which contains the
y
-coordinate
of each point; and 6 (the number of points in the polygon). Line 19 displays
polygon1
by
passing it as an argument to
Graphics
method
drawPolygon
.
Lines 22-23 create two
int
arrays and use them to specify the points for a series of
connected lines. Array
xValues2
contains the
x
-coordinate of each point and array
yValues2
the
y
-coordinate of each point. Line 24 uses
Graphics
method
drawPolyline
to