Java Reference
In-Depth Information
/** Draw a box of width w and height h */
public
public void
void drawBox ( int
int w , int
int h ) {
penDown ();
rmoveTo ( w , 0 );
rmoveTo ( 0 , h );
rmoveTo (- w , 0 );
rmoveTo ( 0 , - h );
penUp ();
}
/** Draw a box given an AWT Dimension for its size */
public
public void
void drawBox ( java . awt . Dimension d ) {
drawBox ( d . width , d . height );
}
/** Draw a box given an AWT Rectangle for its location and size */
public
public void
void drawBox ( java . awt . Rectangle r ) {
moveTo ( r . x , r . y );
drawBox ( r . width , r . height );
}
/** Show the current location; useful for
* testing, if nothing else.
*/
public
public Point getLocation () {
return
return new
new Point ( curx , cury );
}
}
Note the variety of abstract methods. Those related to motion, pen control, or drawing are
left abstract, due to the number of different methods for dealing with them. However, the
method for drawing a rectangle ( drawBox ) has a default implementation, which simply puts
the currently selected pen onto the paper at the last-moved-to location, draws the four sides,
and raises the pen. Subclasses for “smarter” plotters will likely override this method, but sub-
classes for less-evolved plotters will probably use the default version. This method also has
two overloaded convenience methods for cases where the client has an AWT Dimension for
the size or an AWT Rectangle for the location and size.
To demonstrate one of the subclasses of this program, consider the following simple “driver”
program. This is intended to simulate a larger graphics application such as GnuPlot. The
Class.forName() near the beginning of main is discussed in Finding and Using Methods
Search WWH ::




Custom Search