Shape.java
import java.text.*; // for NumberFormat
/**
A base class for shapes (Rectangle, Circle, etc.).
@author Walt Leipold
*/
public abstract class Shape implements PrinterPlottable
{
/**
Construct an abstract <code>Shape</code>.
@param x x-coordinate of <code>Shape</code> location
@param y y-coordinate of <code>Shape</code> location
*/
public Shape(double x,double y)
{
this.x = x;
this.y = y;
}
/**
Move this <code>Shape</code> to a new location.
@param new_x x-coordinate of new location
@param new_y y-coordinate of new location
*/
public void translate(double new_x,double new_y)
{
x = new_x;
y = new_y;
}
/**
@return the x-coordinate of this <code>Shape</code>
*/
public double getX()
{
return x;
}
/**
@return the y-coordinate of this <code>Shape</code>
*/
public double getY()
{
return y;
}
/**
@return a <code>String</code> describing this shape
*/
public abstract String toString();
/**
Draw this <code>Shape</code> on a plot context using a
specified character.
@param pc the <code>PlotContext</code> for drawing
@param drawChar character to use to fill the <code>Shape</code>
*/
public abstract void draw(PlotContext pc,char drawChar);
/**
Format a double-precision number for <code>toString()</code>
methods in subclasses.
@param x the number to format
@return the number formatted as a <code>String</code>
*/
public String formatNum(double x)
{
return nf.format(x);
}
private static NumberFormat nf = null;
static {
nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(5);
}
private double x,y;
}