package csa120.shape; import java.awt.Graphics2D; import java.awt.BasicStroke; /** * A Rectangle is a subclass of a {@link FillableShape}. The * 'location' point inherited from {@link Shape} will be interpreted * as the center of the rectangle. * * @author (Michael H. Goldwasser) * @version (23 January 2004) */ public class Rectangle extends FillableShape { /** * The width of the rectangle, measured in pixels. */ protected int width; /** * The height of the rectangle, measured in pixels. */ protected int height; /** * Constructor for objects of class Rectangle * * @param w original width * @param h original height */ public Rectangle(int w, int h) { super(); // this is redundant, as the default constructor // for parent class would be called by default width = w; height = h; } /** * Sets width of rectangle * * @param newWidth the new value */ public void setWidth(int newWidth) { width = newWidth; } /** * Returns current width of rectangle * * @return the width */ public int getWidth() { return width; } /** * Sets height of rectangle * * @param newHeight the new value */ public void setHeight(int newHeight) { height = newHeight; } /** * Returns current height of rectangle * * @return the height */ public int getHeight() { return height; } /** * Draws the Rectangle on the given Graphics2D object. * * @param g the Graphics2D object upon which to draw */ public void draw(Graphics2D g) { draw(g,0,0); } /** * Draws the Rectangle on the given Graphics2D object, * with additional offset as given. * * @param g the Graphics2D object upon which to draw * @param deltaX additional offset to be considered * @param deltaY additional offest to be considered */ public void draw(Graphics2D g, int deltaX, int deltaY) { if (filled) { g.setColor(fillColor); g.fillRect(deltaX+location.x-width/2,deltaY+location.y-height/2, width,height); } g.setStroke(new BasicStroke(borderThickness)); g.setColor(borderColor); g.drawRect(deltaX+location.x-width/2,deltaY+location.y-height/2, width,height); } } // end of class Rectangle