1 /* 2 * File: Octagon.java 3 * 4 * Octagon class 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.awt.Polygon; 11 import java.awt.Graphics; 12 import java.awt.Point; 13 14 // An octagon is a polygon with eight sides: 15 class Octagon extends Polygon { 16 17 // Octagon constructor #1: 18 public Octagon() { 19 // invoke the constructor of the superclass Polygon: 20 super(); 21 } 22 23 // Octagon constructor #2: 24 public Octagon( int x1, int y1, 25 int x2, int y2, 26 int x3, int y3, 27 int x4, int y4, 28 int x5, int y5, 29 int x6, int y6, 30 int x7, int y7, 31 int x8, int y8 ) { 32 33 // invoke the constructor of the superclass Polygon: 34 super(); 35 36 // the 'this' reference is optional: 37 this.addPoint( x1, y1 ); 38 this.addPoint( x2, y2 ); 39 this.addPoint( x3, y3 ); 40 this.addPoint( x4, y4 ); 41 this.addPoint( x5, y5 ); 42 this.addPoint( x6, y6 ); 43 this.addPoint( x7, y7 ); 44 this.addPoint( x8, y8 ); 45 46 } 47 48 // Octagon constructor #3: 49 public Octagon( Point p1, 50 Point p2, 51 Point p3, 52 Point p4, 53 Point p5, 54 Point p6, 55 Point p7, 56 Point p8 ) { 57 58 // invoke Octagon constructor #2: 59 this( p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, 60 p5.x, p5.y, p6.x, p6.y, p7.x, p7.y, p8.x, p8.y ); 61 62 } 63 64 // Instance methods: 65 public void draw( Graphics g ) { 66 g.drawPolygon( this ); 67 } 68 public void fill( Graphics g ) { 69 g.fillPolygon( this ); 70 } 71 72 // Overload Polygon.addPoint( int, int ): 73 public void addPoint( Point p ) { 74 super.addPoint( p.x, p.y ); 75 } 76 77 }