Rectangle.java

/**
A simple <code>Rectangle</code> class for homework #2.

@author Walt Leipold
*/
public class Rectangle extends Shape implements PrinterPlottable
{
    /**
    Construct a <code>Rectangle</code> object.

    @param x x-coordinate of one corner
    @param y y-coordinate of one corner
    @param w width of <code>Rectangle</code>
    @param h height of <code>Rectangle</code>
    */
    public Rectangle(double x,double y,double w,double h)
    {
        // Call the superclass constructor *first* (required by
        // the language), then fix up the location afterwards.
        super(x,y);
        this.width = w;
        this.height = h;
    }


    /**
    @return the width of this <code>Rectangle</code>
    */
    public double getWidth()
    {
        return width;
    }


    /**
    @return the height of this <code>Rectangle</code>
    */
    public double getHeight()
    {
        return height;
    }


    /**
    @return a <code>String</code> describing this <code>Line</code>
    */
    public String toString()
    {
        return getClass().getName() + "[" +
            "x=" + formatNum(getX()) + "," +
            "y=" + formatNum(getY()) + "," +
            "w=" + formatNum(width) + "," +
            "h=" + formatNum(height) + "]";
    }


    /**
    Draw this <code>Rectangle</code> on the given plot context
    using a specified character.

    @param pc the <code>PlotContext</code> to draw on
    @param drawChar the character to draw with
    */
    public void draw(PlotContext pc,char drawChar)
    {
        int x1 = (int)Math.round(min(getX(),getX()+width));
        int x2 = (int)Math.round(max(getX(),getX()+width));
        int y1 = (int)Math.round(min(getY(),getY()+height));
        int y2 = (int)Math.round(max(getY(),getY()+height));
        for (int i=x1; i<=x2; i++)
            for (int j=y1; j<=y2; j++)
                pc.setPoint(i,j,drawChar);
    }


    private double min(double a,double b) { return (a<b) ? a : b; }
    private double max(double a,double b) { return (a>b) ? a : b; }

    private double width,height;
}