FileDocCategorySizeDatePackage
AppletViewer.javaAPI DocExample3098Sun Mar 14 08:01:52 GMT 2004None

AppletViewer

public class AppletViewer extends Object

Fields Summary
JFrame
f
The main Frame of this program
static AppletAdapter
aa
The AppletAdapter (gives AppletStub, AppletContext, showStatus)
String
appName
The name of the Applet subclass
Class
ac
The Class for the actual applet type
Applet
ai
The Applet instance we are running, or null. Can not be a JApplet until all the entire world is converted to JApplet.
final int
WIDTH
The width of the Applet
final int
HEIGHT
The height of the Applet
Constructors Summary
AppletViewer(String appName)
Construct the GUI for an Applet Viewer

		super();

		this.appName = appName;

		f = new JFrame("AppletViewer");
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				f.setVisible(false);
				f.dispose();
				System.exit(0);
			}
		});
		Container cp = f.getContentPane();
		cp.setLayout(new BorderLayout());

		// Instantiate the AppletAdapter which gives us
		// AppletStub and AppletContext.
		if (aa == null)
			aa = new AppletAdapter();

		// The AppletAdapter also gives us showStatus.
		// Therefore, must add() it very early on, since the Applet's
		// Constructor or its init() may use showStatus()
		cp.add(BorderLayout.SOUTH, aa);

		showStatus("Loading Applet " + appName);

		loadApplet(appName , WIDTH, HEIGHT);	// sets ac and ai
		if (ai == null)
			return;

		// Now right away, tell the Applet how to find showStatus et al.
		ai.setStub(aa);

		// Connect the Applet to the Frame.
		cp.add(BorderLayout.CENTER, ai);

		Dimension d = ai.getSize();
		d.height += aa.getSize().height;
		f.setSize(d);
		f.setVisible(true);		// make the Frame and all in it appear

		showStatus("Applet " + appName + " loaded");

		// Here we pretend to be a browser!
		ai.init();
		ai.start();
	
Methods Summary
voidloadApplet(java.lang.String appletName, int w, int h)

		// appletName = ... extract from the HTML CODE= somehow ...;
		// width = 		ditto
		// height = 		ditto
		try {
			// get a Class object for the Applet subclass
			ac = Class.forName(appletName);
			// Construct an instance (as if using no-argument constructor)
			ai = (Applet) ac.newInstance();
		} catch(ClassNotFoundException e) {
			showStatus("Applet subclass " + appletName + " did not load");
			return;
		} catch (Exception e ){
			showStatus("Applet " + appletName + " did not instantiate");
			return;
		}
		ai.setSize(w, h);
	
public static voidmain(java.lang.String[] av)
Main is where it all starts. Construct the GUI. Load the Applet. Start it running.


	                	 
	     
		new AppletViewer(av.length==0?"HelloApplet":av[0]);
	
public voidshowStatus(java.lang.String s)

		aa.getAppletContext().showStatus(s);