Java Reference
In-Depth Information
and Fields ; for now, you can take my word that it simply creates an instance of the given sub-
class, which we store in a Plotter reference named “r” and use to draw the plot:
plotter/PlotDriver.java
public
public class
class PlotDriver
PlotDriver {
/** Construct a Plotter driver, and try it out. */
public
public static
void main ( String [] argv ) {
Plotter r ;
iif ( argv . length != 1 ) {
System . err . println ( "Usage: PlotDriver driverclass" );
return
static void
return ;
}
try
try {
Class <?> c = Class . forName ( argv [ 0 ]);
Object o = c . newInstance ();
iif (!( o instanceof
instanceof Plotter ))
throw
new ClassNotFoundException ( "Not instanceof Plotter" );
r = ( Plotter ) o ;
} catch
throw new
catch ( ClassNotFoundException e ) {
System . err . println ( "Sorry, class " + argv [ 0 ] +
" not a plotter class" );
return ;
} catch
return
catch ( Exception e ) {
e . printStackTrace ();
return
return ;
}
r . penDown ();
r . penColor ( 1 );
r . moveTo ( 200 , 200 );
r . penColor ( 2 );
r . drawBox ( 123 , 200 );
r . rmoveTo ( 10 , 20 );
r . penColor ( 3 );
r . drawBox ( 123 , 200 );
r . penUp ();
r . moveTo ( 300 , 100 );
r . penDown ();
r . setFont ( "Helvetica" , 14 );
r . drawString ( "Hello World" );
r . penColor ( 4 );
r . drawBox ( 10 , 10 );
}
}
Search WWH ::




Custom Search