import java.awt.*; import java.awt.geom.*; /** * * Implementation of Shape as a Rectangle, plus some Rectangle-specific * methods. * * @author Gene Fisher (gfisher@calpoly.edu) * */ public class Rectangle extends ConvexPolygon { /** * Construct this by intializing the data fields to the values of given * parameters. */ public Rectangle(double width, double height, Point position, Color color, boolean filled) { super(new Point[] {position, new Point((int) (position.x + width), position.y), new Point((int) (position.x + width), (int) (position.y + height)), new Point(position.x, (int) (position.y + height))}, color, filled); } /** * Return this' width. */ public double getWidth() { Point a = getVertex(0); Point b = getVertex(1); return b.x - a.x; } /** * Set this' width to the given width. */ public void setWidth(double width) { int diff = (int) (width - getWidth()); Point a = getVertex(1); Point b = getVertex(2); a.translate(diff, 0); b.translate(diff, 0); setVertex(1, a); setVertex(2, b); } /** * Return this' height. */ public double getHeight() { Point a = getVertex(0); Point b = getVertex(3); return b.y - a.y; } /** * Set this' height to the given height. */ public void setHeight(double height) { int diff = (int) (height - getHeight()); Point a = getVertex(2); Point b = getVertex(3); a.translate(0, diff); b.translate(0, diff); setVertex( 2, a ); setVertex( 3, b ); } /** * Return this' position. */ public Point getPosition() { return getVertex(0); } /** * Return the Java AWT representation of this, as a Rectangle2D.Double. */ public java.awt.Shape getAwtRep(int canvas_height) { /* * This is what DrawingCanvas.drawRectangle does. Attempts to use the * parent ConvexPolygon vertices failed miserably. */ return new Rectangle2D.Double( getPosition().x, canvas_height - getPosition().y, (int) getWidth(), (int) getHeight()); } }