import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** * * Implementation of Shape as a Ellipse, plus some Ellipse-specific methods. * * @author Gene Fisher (gfisher@calpoly.edu) * */ public class Ellipse extends Shape { /** The semi-major radius */ private double major; /** The semi-minor axix */ private double minor; /** The position of this' center */ private Point position; /** Java-geometry coordinates, i.e., un-noramized from user-level * coordinates. */ int x; int y; int width; int height; /** * Construct this by intializing the data fields to the values of the given * parameters. */ public Ellipse(double major, double minor, Point position, Color color, boolean filled) { super(color, filled); this.major = major; this.minor = minor; this.position = position; jshape = new Ellipse2D.Double( position.x - major, position.y + minor, major * 2, minor * 2); } /** * Construct this via its framing rectangle, in Java-geometry coordinates. */ public Ellipse(int x, int y, int width, int height, Color color, boolean filled) { super(color, filled); position = new Point(x + width / 2, y - height / 2); major = width / 2; minor = height / 2; jshape = new Ellipse2D.Double(x, y, width, height); } /** * Return this' semi-major axis. */ public double getSemiMajorAxis() { return major; } /** * Return this' semi-minor axis. */ public double getSemiMinorAxis() { return minor; } /** * Set this' semi-major axis given value. */ public void setSemiMajorAxis(double major) { this.major = major; } /** * Set this' semi-minor axis given value. */ public void setSemiMinorAxis(double major) { this.minor = minor; } /** * Return this' position. */ public Point getPosition() { return position; } /** * Return true if super.equals is true, and the position and major axis and * minor axis of the other ellipse are the same as this ellipse. */ 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. */ Ellipse c = (Ellipse) other; return position.equals(c.position) && major == c.major && minor == c.minor; } /*-* * Specialized methods for Ellipse */ public double getArea() { return Math.PI * major * minor; } public void move(Point delta) { // Yipee, we got public data fields in java.awt.Point! position.x += delta.x; position.y += delta.y; } /** * Return the Java AWT representation of this, as an Ellipse2D.Double. */ public java.awt.Shape getAwtRep(int canvas_height) { return new Ellipse2D.Double( position.x - major, canvas_height - position.y - minor, major * 2, minor * 2); } }