Java Reference
In-Depth Information
Example 11•18: GraphicsExampleFrame.java (continued)
// Tell the PrinterJob that we successfully printed the page
return PAGE_EXISTS;
}
}
/**
* The main program. Use Java reflection to load and instantiate
* the specified GraphicsExample classes, then create a
* GraphicsExampleFrame to display them.
**/
public static void main(String[] args) {
GraphicsExample[] examples = new GraphicsExample[args.length];
// Loop through the command line arguments
for(int i=0; i < args.length; i++) {
// The class name of the requested example
String classname = args[i];
// If no package is specified, assume it is in this package
if (classname.indexOf('.') == -1)
classname = "com.davidflanagan.examples.graphics."+args[i];
// Try to instantiate the named GraphicsExample class
try {
Class exampleClass = Class.forName(classname);
examples[i] = (GraphicsExample) exampleClass.newInstance();
}
catch (ClassNotFoundException e) { // unknown class
System.err.println("Couldn't find example: " + classname);
System.exit(1);
}
catch (ClassCastException e) { // wrong type of class
System.err.println("Class " + classname +
" is not a GraphicsExample");
System.exit(1);
}
catch (Exception e) { // class doesn't have a public constructor
// catch InstantiationException,IllegalAccessException
System.err.println("Couldn't instantiate example: " +
classname);
System.exit(1);
}
}
// Now create a window to display the examples in, and make it visible
GraphicsExampleFrame f = new GraphicsExampleFrame(examples);
f.pack();
f.setVisible(true);
}
}
Search WWH ::




Custom Search