1 /* 2 * File: HexagonTest4.java 3 * 4 * Drawing a hexagon in a square 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 15 public class HexagonTest4 extends Applet { 16 17 // create reference to a polygon: 18 private Polygon p1, p2; 19 20 public void init() { 21 22 // instantiate a polygon object: 23 p1 = new Polygon(); 24 25 // add points to make a square: 26 p1.addPoint( 25, 25 ); 27 p1.addPoint( 175, 25 ); 28 p1.addPoint( 175, 175 ); 29 p1.addPoint( 25, 175 ); 30 31 // instantiate a polygon object: 32 p2 = new Polygon(); 33 34 // add points to make a hexagon: 35 p2.addPoint( 59, 25 ); 36 p2.addPoint( 141, 25 ); 37 p2.addPoint( 175, 100 ); 38 p2.addPoint( 141, 175 ); 39 p2.addPoint( 59, 175 ); 40 p2.addPoint( 25, 100 ); 41 42 } 43 44 public void paint( Graphics g ) { 45 46 // draw a square: 47 g.setColor( Color.yellow ); 48 g.drawPolygon( p1 ); 49 50 // draw a hexagon: 51 g.setColor( Color.red ); 52 g.drawPolygon( p2 ); 53 54 } 55 56 }