/* * File: HexagonTest.java * * Draw a regular hexagon * * Copyright: Northeast Parallel Architectures Center * */ import java.applet.Applet; import java.awt.Graphics; import java.awt.Polygon; import java.awt.Color; import java.awt.Point; public class HexagonTest extends Applet { // create a reference to a hexagon: private Hexagon hexagon; public void init() { // instantiate a hexagon like this... hexagon = new Hexagon( 75, 25, 175, 25, 225, 112, 175, 199, 75, 199, 25, 112 ); // ...or like this: hexagon = new Hexagon(); // add points to make a regular hexagon: hexagon.addPoint( 75, 25 ); hexagon.addPoint( 175, 25 ); hexagon.addPoint( 225, 112 ); hexagon.addPoint( 175, 199 ); hexagon.addPoint( 75, 199 ); hexagon.addPoint( 25, 112 ); } public void paint( Graphics g ) { // fill the hexagon: g.setColor( Color.red ); hexagon.fill( g ); } } // A hexagon is a polygon with six sides: class Hexagon extends MyPolygon { // Hexagon constructor #1: public Hexagon() { // invoke the constructor of the superclass MyPolygon: super(); } // Hexagon constructor #2: public Hexagon( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x5, int y5, int x6, int y6 ) { // invoke the constructor of the superclass MyPolygon: super(); // the 'this' reference is optional: this.addPoint( x1, y1 ); this.addPoint( x2, y2 ); this.addPoint( x3, y3 ); this.addPoint( x4, y4 ); this.addPoint( x5, y5 ); this.addPoint( x6, y6 ); } } class MyPolygon extends Polygon { // Instance methods: public void draw( Graphics g ) { g.drawPolygon( this ); } public void fill( Graphics g ) { g.fillPolygon( this ); } // Overload Polygon.addPoint( int, int ): public void addPoint( Point p ) { super.addPoint( p.x, p.y ); } }