// RightTriangle.java
import java.awt.Graphics;
/**
* Class of objects representing right triangles
* that can be drawn and their area calculated.
*
* @author D. Nixon
*/
public class RightTriangle extends ShapeOutline {
/** height of triangle in pixels */
private int height;
/** count of RightTriangle objects instantiated so far */
private static int objectCount = 0;
/**
* Creates a RightTriangle with the specified
* dimensions.
*
* @param w integer specifying width in pixels
* @param h integer specifying height in pixels
* @exception NegativeSizeException if
* either <code>w</code> or
* <code>h</code> < 0.
*/
public RightTriangle( int w, int h )
{
super(w);
if ( h < 0 )
throw new NegativeSizeException(
"RightTriangle height " + h + " must be >= 0.");
height = h;
// So that getCount will know how many
// RightTriangle objects have been created:
objectCount++;
} // constructor RightTriangle(int, int)
/**
* Draws this RightTriangle at a specified location
* using the specified Graphics context
*
* @param g the Graphics context used
* @param pixelsFromLeft integer distance from left
* side of display area to left side of
* drawing of this RightTriangle
* @param pixelsFromTop integer distance from top
* of display area to top of drawing of
* this RightTriangle
*/
public void draw( Graphics g,
int pixelsFromLeft,
int pixelsFromTop )
{
g.drawLine(pixelsFromLeft, pixelsFromTop,
pixelsFromLeft, pixelsFromTop + height);
g.drawLine(pixelsFromLeft, pixelsFromTop,
pixelsFromLeft + getWidth(),
pixelsFromTop + height);
g.drawLine(pixelsFromLeft, pixelsFromTop + height,
pixelsFromLeft + getWidth(),
pixelsFromTop + height);
} // method draw(Graphics, int, int)
/**
* Gets this RightTriangle's height in pixels
*
* @return integer height
*/
public int getHeight() { return height; }
/**
* Determines approximate number of pixels
* enclosed by this RightTriangle
*
* @return floating-point area of this RightTriangle,
* in pixels
*/
public float getArea()
{
return(getWidth() * height / 2.0f);
} // method getArea()
/**
* Determines whether this RightTriangle
* is equal to another object.
*
* @param other the other object.
* @return <code>true</code> if <code>other</code>
* is a RightTriangle with the same
* width and height as this one,
* <code>false</code> otherwise.
*/
public boolean equals(Object other)
{
return ( super.equals(other)
&& height == ((RightTriangle) other).height );
} // method equals
/**
* Returns a brief string describing
* this RightTriangle, stating its class,
* width, and height.
*/
public String toString()
{
return (super.toString() + " height=" + height);
} // method toString
/**
* Returns text to be inserted after "width"
* in the string returned by
* <code>toDescriptorString</code>.
* Contains the height and commas to
* make the resulting string returned by
* <code>toDescriptorString</code>
* grammatically correct.
*/
protected String otherDimensionDescriptor()
{
return (", height " + height + ",");
} // method otherDimensionDescriptor()
/**
* Gets a count of RightTriangle objects instantiated so far.
*
* @return integer number of RightTriangle objects
*/
public static int getCount()
{
return objectCount;
} // method getCount()
} // class RightTriangle