# Drawing Polygons and Polylines

Polygons are closed multisided shapes composed of straight line segments. Polylines are sequences of connected points. Figure 12.26 discusses methods for drawing polygons and polylines. Note that some methods require a Polygon object (package java.awt). Class Polygon's constructors are also described in Fig. 12.26. The application of Fig. 12.27Fig. 12.28 draws polygons and polylines.

Figure 12.26. Graphics methods for polygons and class Polygon methods.

(This item is displayed on pages 619 - 620 in the print version)

Method

Description

Graphics methods for drawing polygons

public void drawPolygon( int xPoints[], int yPoints[], int points )

Draws a polygon. The x-coordinate of each point is specified in the xPoints array, and the y-coordinate of each point in the yPoints array. The last argument specifies the number of points. This method draws a closed polygon. If the last point is different from the first, the polygon is closed by a line that connects the last point to the first.

public void drawPolyline( int xPoints[], int yPoints[], int points )

Draws a sequence of connected lines. The x-coordinate of each point is specified in the xPoints array, and the y-coordinate of each point in the yPoints array. The last argument specifies the number of points. If the last point is different from the first, the polyline is not closed.

public void drawPolygon( Polygon p )

Draws the specified polygon.

public void fillPolygon( int xPoints[], int yPoints[], int points )

Draws a filled polygon. The x-coordinate of each point is specified in the xPoints array, and the y-coordinate of each point in the yPoints array. The last argument specifies the number of points. This method draws a closed polygon. If the last point is different from the first, the polygon is closed by a line that connects the last point to the first.

public void fillPolygon( Polygon p )

Draws the specified filled polygon. The polygon is closed.

Polygon constructors and methods

public Polygon()

Constructs a new polygon object. The polygon does not contain any points.

public Polygon( int xValues[], int yValues[], int numberOfPoints )

Constructs a new polygon object. The polygon has numberOfPoints sides, with each point consisting of an x-coordinate from xValues and a y-coordinate from yValues.

public void addPoint( int x, int y )

Adds pairs of x- and y-coordinates to the Polygon.

Figure 12.27. Polygons displayed with drawPolygon and fillPolygon.

(This item is displayed on pages 620 - 621 in the print version)

 ``` 1 // Fig. 12.27: PolygonsJPanel.java 2 // Drawing polygons. 3 import java.awt.Graphics; 4 import java.awt.Polygon; 5 import javax.swing.JPanel; 6 7 public class PolygonsJPanel extends JPanel 8 { 9 // draw polygons and polylines 10 public void paintComponent( Graphics g ) 11 { 12 super.paintComponent( g ); // call superclass's paintComponent 13 14 // draw polygon with Polygon object 15 int xValues[] = { 20, 40, 50, 30, 20, 15 }; 16 int yValues[] = { 50, 50, 60, 80, 80, 60 }; 17 Polygon polygon1 = new Polygon( xValues, yValues, 6 ); 18 g.drawPolygon( polygon1 ); 19 20 // draw polylines with two arrays 21 int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 }; 22 int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 }; 23 g.drawPolyline( xValues2, yValues2, 7 ); 24 25 // fill polygon with two arrays 26 int xValues3[] = { 120, 140, 150, 190 }; 27 int yValues3[] = { 40, 70, 80, 60 }; 28 g.fillPolygon( xValues3, yValues3, 4 ); 29 30 // draw filled polygon with Polygon object 31 Polygon polygon2 = new Polygon(); 32 polygon2.addPoint( 165, 135 ); 33 polygon2.addPoint( 175, 150 ); 34 polygon2.addPoint( 270, 200 ); 35 polygon2.addPoint( 200, 220 ); 36 polygon2.addPoint( 130, 180 ); 37 g.fillPolygon( polygon2 ); 38 } // end method paintComponent 39 } // end class PolygonsJPanel ```

Figure 12.28. Creating JFrame to display polygons.

(This item is displayed on page 621 in the print version)

``` 1 // Fig. 12.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 ); // add polygonsJPanel to frame
16 frame.setSize( 280, 270 ); // set frame size
17 frame.setVisible( true ); // display frame
18 } // end main
19 } // end class DrawPolygons
``` Lines 1516 of Fig. 12.27 create two int arrays and use them to specify the points for Polygon polygon1. The Polygon constructor call in line 17 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 18 displays polygon1 by passing it as an argument to Graphics method drawPolygon.

Lines 2122 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 23 uses Graphics method drawPolyline to display the series of connected lines specified with the arguments xValues2, yValues2 and 7 (the number of points).

Lines 2627 create two int arrays and use them to specify the points of a polygon. Array xValues3 contains the x-coordinate of each point and array yValues3 the y-coordinate of each point. Line 28 displays a polygon by passing to Graphics method fillPolygon the two arrays (xValues3 and yValues3) and the number of points to draw (4).

Common Programming Error 12.1 An ArrayIndexOutOfBoundsException is thrown if the number of points specified in the third argument to method drawPolygon or method fillPolygon is greater than the number of elements in the arrays of coordinates that specify the polygon to display.

Line 31 creates Polygon polygon2 with no points. Lines 3236 use Polygon method addPoint to add pairs of x- and y-coordinates to the Polygon. Line 37 displays Polygon polygon2 by passing it to Graphics method fillPolygon. Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Similar book on Amazon 