The Polygon class encapsulates a description of a closed two-dimensional region within a coordinate space. This region is bounded by an arbitrary number of line segments, each of which is one side (or edge) of the polygon. Internally, a polygon comprises a list of (x, y) coordinate pairs in which each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon. The first and final pairs of (x, y) points are joined by a line segment that closes the polygon.
The two constructors given below are used to create a Polygon object.
public Polygon()
Constructs an empty polygon.
public Polygon( int [] xpoints, int [] ypoints, int npoints)
Constructs and initializes a Polygon with specified points. Parameters xpoints and ypoints are arrays representing x -coordinates and y -coordinates, and npoints indicates the number of points.
To append a point to the polygon, use the addPoint(int x, int y) method. The Polygon class has the public data fields xpoints , ypoints , and npoints , which represent the array of x -coordinates and y -coordinates, and the total number of points.
Here is an example of creating a polygon and adding points into it:
Polygon polygon = new Polygon(); polygon.addPoint( 40 , 20 ); polygon.addPoint( 70 , 40 ); polygon.addPoint( 60 , 80 ); polygon.addPoint( 45 , 45 ); polygon.addPoint( 20 , 60 );
To draw or fill a polygon, use one of the following methods :
drawPolygon(Polygon polygon); fillPolygon(Polygon polygon); drawPolygon( int [] xpoints, int [] ypoints, int npoints); fillPolygon( int [] xpoints, int [] ypoints, int npoints);
For example:
int x[] = { 40 , 70 , 60 , 45 , 20 }; int y[] = { 20 , 40 , 80 , 45 , 60 }; g.drawPolygon(x, y, x.length);
The drawing method opens the polygon by drawing lines between point (x[i], y[i]) and point (x[i+1], y[i+1]) for i = 0, ... , x.length-1 ; it closes the polygon by drawing a line between the first and last points (see Figure 13.14(a)).
To draw a polyline, use the drawPolyline(int[] x, int[] y, int nPoints) method, which draws a sequence of connected lines defined by arrays of x and y coordinates. For example, the following code draws the polyline, as shown in Figure 13.14(b):
int x[] = { 40 , 70 , 60 , 45 , 20 }; int y[] = { 20 , 40 , 80 , 45 , 60 }; g.drawPolygon(x, y, x.length);
Listing 13.7 is an example of how to draw a hexagon, with the output shown in Figure 13.15.
1 import javax.swing.JFrame; 2 import javax.swing.JPanel; 3 import java.awt.Graphics; 4 import java.awt.Polygon; 5 6 public class DrawPolygon extends JFrame { 7 public DrawPolygon() { 8 setTitle( "DrawPolygon" ); 9 add( new PolygonsPanel()); 10 } 11 12 /** Main method */ 13 public static void main(String[] args) { 14 DrawPolygon frame = new DrawPolygon(); 15 frame.setLocationRelativeTo( null ); // Center the frame 16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 frame.setSize( 200 , 250 ); 18 frame.setVisible( true ); 19 } 20 } 21 22 // Draw a polygon in the panel 23 class PolygonsPanel extends JPanel { 24 protected void paintComponent(Graphics g) { 25 super .paintComponent(g); 26 27 int xCenter = getWidth() / 2 ; 28 int yCenter = getHeight() / 2 ; 29 int radius = ( int )(Math.min(getWidth(), getHeight()) * . 4 ); 30 31 // Create a Polygon object 32 Polygon polygon = new Polygon(); 33 34 // Add points to the polygon 35 polygon.addPoint) (xCenter + radius, yCenter); 36 polygon.addPoint) (( int )(xCenter + radius * 37 Math.cos( 2 * Math.PI / 6 )), ( int )(yCenter “ radius * 38 Math.sin( 2 * Math.PI / 6 ))); 39 polygon.addPoint(( int )(xCenter + radius * 40 Math.cos( 2 * 2 * Math.PI / 6 )), ( int )(yCenter “ radius * 41 Math.sin( 2 * 2 * Math.PI / 6 ))); 42 polygon.addPoint(( int )(xCenter + radius * 43 Math.cos( 3 * 2 * Math.PI / 6 )), ( int )(yCenter “ radius * 44 Math.sin( 3 * 2 * Math.PI / 6 ))); 45 polygon.addPoint(( int )(xCenter + radius * 46 Math.cos( 4 * 2 * Math.PI / 6 )), ( int )(yCenter “ radius * 47 Math.sin( 4 * 2 * Math.PI / 6 ))); 48 polygon.addPoint(( int )(xCenter + radius * 49 Math.cos( 5 * 2 * Math.PI / 6 )), ( int )(yCenter “ radius * 50 Math.sin( 5 * 2 * Math.PI / 6 ))); 51 52 // Draw the polygon 53 g.drawPolygon(polygon); 54 } 55 } |