http://cs101.org/courses/fall05/psets/catmouse/AbstractDisplayable.html |
/* * AbstractDisplayable.java */ package catandmouse; import java.awt.Rectangle; /** * A displayable that contains code for keeping track of location */ public abstract class AbstractDisplayable implements Displayable { private Rectangle bounds; /** Creates a new instance of AbstractDisplayable. * @param width the width of the displayable object * @param height the height of the displayable object */ public AbstractDisplayable(int width, int height) { bounds = new Rectangle(0, 0, width, height); } /** * Sets the location of the <i>center</i> of the object. * @param x the horizontal coordinate of the Displayable"s center * @param y the vertical coordinate of the Displayable"s center */ public void setLocation(int x, int y) { int wid = bounds.width; int hgt = bounds.height; bounds.setBounds(x-wid/2, y-hgt/2, wid, hgt); } /** * Gets the bounds of this Displayable object. This includes the top * left corner as well as the width and the height. * @return the bounds of the object as a Rectangle. */ public Rectangle getBounds() { return bounds; } }