Chapter 6
Section: Triangles and Polygons

WE'VE SEEN many ways to do shapes in Java, but sill no way to do a triangle or a pentagon or other polygon. The Java API includes a method that will draw a figure with any number of sides. In order to draw such a figure, we need to specify the coordinates for each corner of the polygon. In this section, we'll look at how to specify the coordinates and create the polygon

Arrays

In order to draw a polygon, we need a way to pass the coordinates of all of the the vertices to a polygon drawing method. In Java, there are many ways to store a collection of objects or primitive data types, but one of the simplest is the array. The Java array can hold any primitive data type or objects of any class type, the catch is that the array can only hold values of one particular data type. An integer array cannot hold any Strings nor doubles, for instance. An example of an array that we've seen since the beginning is the array of Strings that is passed to the main method of every Java application. When we wrote:
public static void main( String[] args )
the parameter args represents an array of String objects.

The easiest way to create a Java array is to declare the array variable and then initialize it with the individual values placed in a comma seperated list enclosed within curly braces.

double[] myDoubles = {32.56, 0.00012,
                      3.14159};

The Java array allows access to each element through indexing using the square brackets: []. You won't need to know this to draw polygons, however.

Drawing Polygons

The Graphics class provides several Polygon drawing methods. drawPolygon() is used to draw the outline of a polygon, while fillPolygon() fills in a polygon with the current color.

drawPolygon(int[] xPoints, int[] yPoints, int numberOfPoints) -- Draws the polygon by drawing the line segments connecting the points whose x and y coordinates are specified in the xPoints and yPoints array. The parameter numberOfPoints is the number of vertices in the Polygon (same as the number of coordinates specified in the xPoints & yPoints arrays).

fillPolygon(int[] xPoints, int[] yPoints, int numberOfPoints) -- Fills in the polygon created by drawing the line segments connecting the points whose x and y coordinates are specified in the xPoints and yPoints array. corner is at (x,y), and the width and height of the rectangle are as specified. Use the current drawing color for the fill. The parameter numberOfPoints is the number of vertices in the Polygon (same as the number of coordinates specified in the xPoints & yPoints arrays).

 

We'll use fillPolygon to draw a stop sign. A stop sign is an 8 sided polygon, so we'll need to create 2 int arrays each containing 8 values.

   1:import java.applet.Applet;
   2:import java.awt.Graphics;
   3:import java.awt.Color;
   4:/**
   5: * @author blindemann
   6: * Draw a small Stop Sign on the screen (text to come later)
   7: */
   8:public class StopSign extends Applet {
   9:   public void paint( Graphics g ) {
  10:      // Using the points (0,9), (9,0), (21,0), (30,9) ...
  11:      // initialize the xPoints and yPoints array...
  12:      int[] xPoints = {0, 9, 21, 30, 30, 21, 9, 0 };
  13:      int[] yPoints = {9, 0, 0,  9,  21, 30, 30, 21 };
  14:      int numPoints = 8;
  15:      // Set the drawing color to RED
  16:      g.setColor(Color.RED);
  17:      // Draw a filled in polygon
  18:      g.fillPolygon(xPoints, yPoints, numPoints );    
  19:   } // end paint
  20:} // end StopSign

[ Next Section | Previous Section | Chapter Index | Main Index ]