import java.awt.*; import java.awt.geom.*; /** * * Implementation of Shape as a Line, plus some Line-specific * methods. To be lazy, it extends Rectagle, just so it does hav * * @author Gene Fisher (gfisher@calpoly.edu) * */ public class Line extends Shape { /** Starting point of this line. */ public Point p1; /** Ending point of this line. */ public Point p2; /** * Construct this by intializing the data fields to the values of given * parameters. Also construct the Java representation. */ public Line(Point p1, Point p2, Color color, boolean filled) { super(color, filled); this.p1 = p1; this.p2 = p2; } /** * Return true if super.equals is true, and two points of f the other line * are the same as this line. */ public boolean equals(Object other) { if (super.equals(other) == false) { return false; } /* * Note that this cast and subsequent code are safe, since super.equals * checks that other is not null and is the same class as this. */ Line l = (Line) other; return p1.equals(l.p1) && p2.equals(l.p2); } /** * Return 0 unconditionally as the area of a line. */ public double getArea() { return 0.0; } /** * Increment this' position by the values of the given point. */ public void move(Point delta) { p1.x += delta.x; p1.y += delta.y; p2.x += delta.x; p2.y += delta.y; } /** * Return the Java AWT representation of this as a Line2D.Double. */ public java.awt.Shape getAwtRep(int canvas_height) { return new Line2D.Double( p1.x, canvas_height - p1.y, p2.x, canvas_height - p2.y); } }