Java Reference
In-Depth Information
Similar to a polygon, a polyline contains a series of points connected by line
segments. Polylines differ from polygons in that the first and last coordinates are
not automatically connected when it is drawn. Since a polyline is not closed, it
cannot be filled. Therefore there is only one method, called drawPolyline , used
to draw a polyline.
As with the drawPolygon method, the first two parameters of the drawPolyline
method are both arrays of integers. Taken together, the first two parameters
represent the ( x, y ) coordinates of the end points of the line segments of the
polyline. The third parameter is the number of points in the coordinate list.
The program shown in Listing 8.15 uses polygons to draw a rocket. In the
RocketPanel class, shown in Listing 8.16, the arrays called xRocket and yRocket
define the points of the polygon that make up the main body of the rocket. The first
point in the arrays is the upper tip of the rocket, and they progress clockwise from
there. The xWindow and yWindow arrays specify the points for the polygon that form the
window in the rocket. Both the rocket and the window are drawn as filled polygons.
LISTING 8.15
//********************************************************************
// Rocket.java Author: Lewis/Loftus
//
// Demonstrates the use of polygons and polylines.
//********************************************************************
import javax.swing.JFrame;
public class Rocket
{
//-----------------------------------------------------------------
// Creates the main frame of the program.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Rocket");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
RocketPanel panel = new RocketPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible( true );
}
}
 
Search WWH ::




Custom Search