1 /* 2 * File: TriangleTest.java 3 * 4 * Drawing triangles 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 TriangleTest extends Applet { 15 16 // create references to two triangles: 17 private Triangle triangle1, triangle2; 18 19 public void init() { 20 21 // instantiate an isosceles triangle: 22 triangle1 = new Triangle( 50, 25, 75, 100, 25, 100 ); 23 // instantiate a right triangle: 24 triangle2 = new Triangle( 125, 25, 175, 100, 125, 100 ); 25 26 } 27 28 public void paint( Graphics g ) { 29 30 // draw the first triangle: 31 triangle1.draw( g ); 32 33 // fill the second triangle: 34 triangle2.fill( g ); 35 36 } 37 38 } 39 40 // A triangle is a polygon with three sides: 41 class Triangle extends Polygon { 42 43 // Triangle constructor #1: 44 public Triangle() { 45 // invoke the constructor of the superclass Polygon: 46 super(); 47 } 48 49 // Triangle constructor #2: 50 public Triangle( int x1, int y1, 51 int x2, int y2, 52 int x3, int y3 ) { 53 54 // invoke the constructor of the superclass Polygon: 55 super(); 56 57 // the 'this' reference is optional: 58 this.addPoint( x1, y1 ); 59 this.addPoint( x2, y2 ); 60 this.addPoint( x3, y3 ); 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 }