// Wheel.java
import java.awt.Graphics;
/**
* Class of objects representing wheels
* that can be drawn and their area calculated.
*
* @author D. Nixon
*/
public class Wheel extends Circle {
/** number of spokes of wheel */
private int spokes;
/** count of Wheel objects instantiated so far */
private static int objectCount = 0;
/**
* Creates a Wheel with the specified
* dimensions.
*
* @param w integer specifying width in pixels
* @param s number of spokes
* @exception NegativeSizeException if
* either <code>w</code> or
* <code>s</code> < 0.
*/
public Wheel( int w, int s )
{
super(w);
if ( s < 0 )
throw new NegativeSizeException(
"Wheel spokes " + s + " must be >= 0.");
spokes = s;
// So that getCount will know how many
// Wheel objects have been created:
objectCount++;
} // constructor Wheel(int, int)
/**
* Draws this Wheel 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 Wheel
* @param pixelsFromTop integer distance from top
* of display area to top of drawing of
* this Wheel
*/
public void draw( Graphics g,
int pixelsFromLeft,
int pixelsFromTop )
{
// Draw the rim of this Wheel:
super.draw(g, pixelsFromLeft, pixelsFromTop);
// Draw the spokes of this Wheel:
int radius = getWidth() / 2;
int centerX = pixelsFromLeft + radius;
int centerY = pixelsFromTop + radius;
for ( int i = 0; i < spokes; i++ ) {
int tipX = centerX
+ (int) Math.round(radius*Math.sin(i*2*Math.PI/spokes));
int tipY = centerY
- (int) Math.round(radius*Math.cos(i*2*Math.PI/spokes));
g.drawLine(centerX, centerY, tipX, tipY);
} // for i
} // method draw(Graphics, int, int)
/**
* Gets this Wheel's number of spokes
*
* @return integer spokes
*/
public int getSpokes() { return spokes; }
/**
* Determines whether this Wheel
* is equal to another object.
*
* @param other the other object.
* @return <code>true</code> if <code>other</code>
* is a Wheel with the same width
* and number of spokes as this one,
* <code>false</code> otherwise.
*/
public boolean equals(Object other)
{
return ( super.equals(other)
&& spokes == ((Wheel) other).spokes );
} // method equals
/**
* Returns a brief string describing
* this Wheel, stating its class,
* width, and number spokes.
*/
public String toString()
{
return (super.toString() + " spokes=" + spokes);
} // method toString
/**
* Returns text to be inserted after "width"
* in the string returned by
* <code>toDescriptorString</code>.
* Contains the number of spokes and commas
* to make the resulting string returned by
* <code>toDescriptorString</code>
* grammatically correct.
*/
protected String otherDimensionDescriptor()
{
return (", " + spokes + " spokes,");
} // method otherDimensionDescriptor()
/**
* Gets a count of Wheel objects instantiated so far.
*
* @return integer number of Wheel objects
*/
public static int getCount()
{
return objectCount;
} // method getCount()
} // class Wheel