1 /* 2 * File: QuadrilateralTest2.java 3 * 4 * Drawing quadrilaterals 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 14 public class QuadrilateralTest2 extends Applet { 15 16 // create references to four polygons: 17 private Polygon p1, p2, p3, p4; 18 19 public void init() { 20 21 // instantiate a Polygon object: 22 p1 = new Polygon(); 23 24 // add points to make a rectangle (a kind of quadrilateral): 25 p1.addPoint( 25, 25 ); 26 p1.addPoint( 125, 25 ); 27 p1.addPoint( 125, 75 ); 28 p1.addPoint( 25, 75 ); 29 30 // instantiate a Polygon object: 31 p2 = new Polygon(); 32 33 // add points to make a parallelogram (another kind of quadrilateral): 34 p2.addPoint( 165, 25 ); 35 p2.addPoint( 265, 25 ); 36 p2.addPoint( 250, 75 ); 37 p2.addPoint( 150, 75 ); 38 39 // instantiate a Polygon object: 40 p3 = new Polygon(); 41 42 // add points to make a square (a kind of rectangle): 43 p3.addPoint( 50, 100 ); 44 p3.addPoint( 100, 100 ); 45 p3.addPoint( 100, 150 ); 46 p3.addPoint( 50, 150 ); 47 48 // instantiate a Polygon object: 49 p4 = new Polygon(); 50 51 // add points to make a rhombus (a kind of parallelogram): 52 p4.addPoint( 195, 110 ); 53 p4.addPoint( 245, 110 ); 54 p4.addPoint( 215, 150 ); 55 p4.addPoint( 165, 150 ); 56 57 // // Here's another way to do it: 58 // 59 // // vertices of a rectangle (a kind of quadrilateral): 60 // int xValues1[] = { 25, 125, 125, 25 }; 61 // int yValues1[] = { 25, 25, 75, 75 }; 62 // 63 // // vertices of a parallelogram (another kind of quadrilateral): 64 // int xValues2[] = { 165, 265, 250, 150 }; 65 // int yValues2[] = { 25, 25, 75, 75 }; 66 // 67 // // vertices of a square (a kind of rectangle): 68 // int xValues3[] = { 50, 100, 100, 50 }; 69 // int yValues3[] = { 100, 100, 150, 150 }; 70 // 71 // // vertices of a rhombus (a kind of parallelogram): 72 // int xValues4[] = { 195, 245, 215, 165 }; 73 // int yValues4[] = { 110, 110, 150, 150 }; 74 // 75 // // instantiate the rectangle: 76 // p1 = new Polygon( xValues1, yValues1, 4 ); 77 // // instantiate the parallelogram: 78 // p2 = new Polygon( xValues2, yValues2, 4 ); 79 // // instantiate the square: 80 // p3 = new Polygon( xValues3, yValues3, 4 ); 81 // // instantiate the rhombus: 82 // p4 = new Polygon( xValues4, yValues4, 4 ); 83 84 } 85 86 public void paint( Graphics g ) { 87 88 // draw two quadrilaterals: 89 g.drawPolygon( p1 ); 90 g.drawPolygon( p3 ); 91 92 // draw two filled quadrilaterals: 93 g.fillPolygon( p2 ); 94 g.fillPolygon( p4 ); 95 96 } 97 98 }