1  /*
  2   *  File:  HexagonTest.java
  3   *
  4   *  Draw a regular hexagon
  5   *
  6   *  Copyright:  Northeast Parallel Architectures Center
  7   *  
  8   */
  9  
 10  import java.applet.Applet;
 11  import java.awt.Graphics;
 12  import java.awt.Polygon;
 13  import java.awt.Color;
 14  import java.awt.Point;
 15  
 16  public class HexagonTest extends Applet {
 17  
 18     // create a reference to a hexagon:
 19     private Hexagon hexagon;
 20     
 21     public void init() {
 22     
 23        // instantiate a hexagon like this...
 24        hexagon = new Hexagon(  75,  25, 175,  25, 225, 112, 
 25                               175, 199,  75, 199,  25, 112  );
 26        // ...or like this:
 27        hexagon = new Hexagon();
 28        // add points to make a regular hexagon:
 29        hexagon.addPoint(  75,  25 );
 30        hexagon.addPoint( 175,  25 );
 31        hexagon.addPoint( 225, 112 );
 32        hexagon.addPoint( 175, 199 );
 33        hexagon.addPoint(  75, 199 );
 34        hexagon.addPoint(  25, 112 );
 35  
 36     }
 37     
 38     public void paint( Graphics g ) {
 39  
 40        // fill the hexagon:
 41        g.setColor( Color.red );
 42        hexagon.fill( g );
 43  
 44     }
 45     
 46  }
 47  
 48  // A hexagon is a polygon with six sides:
 49  class Hexagon extends MyPolygon {
 50  
 51     // Hexagon constructor #1:
 52     public Hexagon() {
 53        // invoke the constructor of the superclass MyPolygon:
 54        super();
 55     }
 56  
 57     // Hexagon constructor #2:
 58     public Hexagon( int x1, int y1, 
 59                     int x2, int y2, 
 60                     int x3, int y3, 
 61                     int x4, int y4,
 62                     int x5, int y5, 
 63                     int x6, int y6  ) {
 64   
 65        // invoke the constructor of the superclass MyPolygon:
 66        super();
 67        
 68        // the 'this' reference is optional:
 69        this.addPoint( x1, y1 );
 70        this.addPoint( x2, y2 );
 71        this.addPoint( x3, y3 );
 72        this.addPoint( x4, y4 );
 73        this.addPoint( x5, y5 );
 74        this.addPoint( x6, y6 );
 75   
 76     }
 77  
 78  }
 79  
 80  class MyPolygon extends Polygon {
 81  
 82     // Instance methods:
 83     public void draw( Graphics g ) {
 84        g.drawPolygon( this );
 85     }
 86     public void fill( Graphics g ) {
 87        g.fillPolygon( this );
 88     }
 89     
 90     // Overload Polygon.addPoint( int, int ):
 91     public void addPoint( Point p ) {
 92        super.addPoint( p.x, p.y );
 93     }
 94     
 95  }