// TwoDimFig.java // This file gives the top-level class in an example // that illustrates inheritance, interfaces, and // simple graphics. // S. Tanimoto, 10 Feb 2000. import java.awt.*; public abstract class TwoDimFig implements SizeFunctions, Paintable { protected Color c; public abstract String describe(); public abstract double area(); public abstract double volume(); public abstract int nVertices(); public abstract int nFaces(); public void setColor(Color c) { this.c = c; } public abstract void paint(Graphics g); } //---------------------------------------------- // SizeFunctions.java // This file lists the protocol for the SizeFunctions interface. // S. Tanimoto, 10 Feb 2000. interface SizeFunctions { public double area(); public double volume(); public int nVertices(); public int nFaces(); } //---------------------------------------------- // Paintable.java // This file lists the protocol for the Paintable interface. // S. Tanimoto, 10 Feb 2000 import java.awt.*; interface Paintable { public void setColor(Color c); public void paint(Graphics g); } //---------------------------------------------- // Circle.java // This file provides the class Circle. // S. Tanimoto, 10 Feb. 2000 import java.awt.*; public class Circle extends TwoDimFig { protected double r; Circle(double r) { this.r = r; } public String describe() { return "a circle with radius " + r; } public int nVertices() { return 0; } public int nFaces() { return 1; } public double area() { return r * r * 3.14159; } public double volume() { return 0.0; } public void paint(Graphics g) { g.setColor(c); g.drawOval(0, 0, (int)r, (int)r); } } //---------------------------------------------- // Polygon.java // This file provides the class Polygon. // S. Tanimoto, 10 Feb. 2000 import java.awt.*; import java.util.*; public class Polygon extends TwoDimFig { protected Vector vertices; Polygon() {} Polygon(int n) { vertices = new Vector(); int R = 100; double ANGLE = 2.0 * 3.14159 / n; for(int i = 0; i < n; i++) { Point p = new Point((int)(R + R*(Math.cos(i * ANGLE))), (int)(R + R*(Math.sin(i * ANGLE)))); vertices.addElement(p); } } public String describe() { return "a polygon with " + vertices.size() + " vertices"; } public double area() { if (vertices.size() < 3) return 0.0; Enumeration enum = vertices.elements(); Point firstVertex = (Point) enum.nextElement(); Point lastVertex = firstVertex; double total = 0.0; while(enum.hasMoreElements()) { Point thisVertex = (Point) enum.nextElement(); total += areaUnderSegment(lastVertex, thisVertex); lastVertex = thisVertex; } total += areaUnderSegment(lastVertex, firstVertex); return total; } private double areaUnderSegment(Point p1, Point p2) { return (p1.x - p2.x) * (p1.y + p2.y) / 2; } public double volume() { return 0.0; } public int nVertices() { return vertices.size(); } public int nFaces() { return 1; } public void paint(Graphics g) { g.setColor(c); Enumeration enum = vertices.elements(); Point firstVertex = (Point) enum.nextElement(); Point lastVertex = firstVertex; while(enum.hasMoreElements()) { Point thisVertex = (Point) enum.nextElement(); g.drawLine(lastVertex.x, lastVertex.y , thisVertex.x, thisVertex.y); lastVertex = thisVertex; } g.drawLine(lastVertex.x, lastVertex.y , firstVertex.x, firstVertex.y); } } //---------------------------------------------- // Triangle.java // This file provides the class Triangle. // S. Tanimoto, 10 Feb. 2000 import java.awt.*; import java.util.*; public class Triangle extends Polygon { Triangle(Point p1, Point p2, Point p3) { vertices = new Vector(); vertices.addElement(p1); vertices.addElement(p2); vertices.addElement(p3); } public String describe() { if (isIsosceles()) return "an isosceles triangle"; else return "a triangle"; } public boolean isIsosceles() { Point p1 = (Point) vertices.elementAt(0); Point p2 = (Point) vertices.elementAt(1); Point p3 = (Point) vertices.elementAt(2); int d12 = dist(p1,p2); int d23 = dist(p2,p3); int d31 = dist(p3,p1); return ((d12 == d23) || (d23 == d31) || (d12 == d31)); } private int dist(Point p1, Point p2) { return (int)((double) Math.sqrt((double)(((p1.x - p2.x)*(p1.x - p2.x)) + ((p1.y - p2.y)*(p1.y - p2.y)))) ); } } //---------------------------------------------- // TestApplet.java // This file provides the class TestApplet. // S. Tanimoto, 10 Feb. 2000 import java.awt.*; import java.util.*; import java.applet.*; public class TestApplet extends Applet { TwoDimFig f1, f2, f3; public void init() { f1 = new Circle(25.0); f1.setColor(Color.red); f2 = new Polygon(4); f2.setColor(Color.blue); f3 = new Triangle(new Point(100,100), new Point(100,200), new Point(200,100)); f3.setColor(Color.orange); } public void paint(Graphics g) { System.out.println("Now painting " + f1.describe() + " with area " + f1.area()); f1.paint(g); System.out.println("Now painting " + f2.describe() + " with area " + f2.area()); f2.paint(g); System.out.println("Now painting " + f3.describe() + " with area " + f3.area()); f3.paint(g); } } //----------------------------------------------