Java Reference
In-Depth Information
LISTING 8.16
continued
private int
[] xFlame = {70, 70, 75, 80, 90, 100, 110, 115, 120,
130, 130};
private int
[] yFlame = {155, 170, 165, 190, 170, 175, 160, 185,
160, 175, 155};
//-----------------------------------------------------------------
// Constructor: Sets up the basic characteristics of this panel.
//-----------------------------------------------------------------
public
RocketPanel()
{
setBackground (Color.black);
setPreferredSize (
new
Dimension(200, 200));
}
//-----------------------------------------------------------------
// Draws a rocket using polygons and polylines.
//-----------------------------------------------------------------
public void
paintComponent (Graphics page)
{
super
.paintComponent (page);
page.setColor (Color.cyan);
page.fillPolygon (xRocket, yRocket, xRocket.length);
page.setColor (Color.gray);
page.fillPolygon (xWindow, yWindow, xWindow.length);
page.setColor (Color.red);
page.drawPolyline (xFlame, yFlame, xFlame.length);
}
}
The
xFlame
and
yFlame
arrays define the points of a polyline that are used to
create the image of flame shooting out of the tail of the rocket. Because it is drawn
as a polyline, and not a polygon, the flame is not closed or filled.
A polygon can also be defined explicitly using an object of the
Polygon
class,
which is defined in the
java.awt
package of the Java standard class library. Two
versions of the overloaded
drawPolygon
and
fillPolygon
methods take a single
Polygon
object as a parameter.
Search WWH ::
Custom Search