// Rect.java // by Scot Drysdale on 4/9/00 to show simple rectangle objects. // Modified on 4/14/00 to add additional methods. import java.awt.*; // Definition for the class "Rect". public class Rect { private int myX, myY; // x and y coords of Rect's upper left corner private int myWidth, myHeight; // Rect's width and height private Color myColor; // Rect's color // Constructor for Rect. Called to create a Rect object with upper left // corner (x, y), width "width", height "height", and color rectColor. public Rect(int x, int y, int width, int height, Color rectColor) { myX = x; myY = y; myWidth = width; myHeight = height; myColor = rectColor; } // Have the Rect object draw itself on the page passed as a parameter. public void draw(Graphics page) { // Set the color. page.setColor(myColor); // Draw the Rect. page.drawRect(myX, myY, myWidth, myHeight); } // Have the Rect object fill itself on the page passed as a parameter. public void fill(Graphics page) { // Set the color. page.setColor(myColor); // Draw the Rect. page.fillRect(myX, myY, myWidth, myHeight); } // Have the Rect object move itself by deltaX, deltaY. public void move(int deltaX, int deltaY) { myX += deltaX; myY += deltaY; } // Set the x value of the upper left corner of Rect to x public void setX(int x) { myX = x; } // Set the y value of the upper left corner of Rect to y public void setY(int y) { myY = y; } // Return the x value of the upper left corner of Rect public int getX() { return myX; } // Return the y value of the upper left corner of Rect public int getY() { return myY; } // Set the width of the Rect to width public void setWidth(int width) { myWidth = width; } // Set the height of the Rect to height public void setHeight(int height) { myHeight = height; } // Set the color of the rect to clr public void setColor(Color clr) { myColor = clr; } // Shrink a rectangle by amount in both directions. public void shrink(int amount) { myX += amount; myY += amount; myHeight -= 2 * amount; myWidth -= 2 * amount; } // Have the Rect object return its area. public double areaOf() { return myWidth * myHeight; } }