Java Reference
In-Depth Information
window.setVisible(true);
}
private static SketchFrame window; // The application window
private static Sketcher theApp; // The application object
}
To run Sketcher as an applet, you should add an .html file to the Sketcher directory with the contents:
<APPLET CODE="Sketcher.class" WIDTH=300 HEIGHT=200>
</APPLET>
If you recompile the revised version of the Sketcher class, you can run it as before, or using
AppletViewer .
How It Works
The class now extends the class JApplet , and an import statement has been added for the
javax.swing package.
The init() method now does most of what the method main() did before. The method main() now
creates an instance of the Sketcher class and stores it in the static data member theApp . The
method main() then calls the init() method for the new Sketcher object. The window variable no
longer needs to be declared as static since it is always created in the init() method.
The class member, theApp , must be declared as static for the case when the program is run as an
application. When an application starts execution, no Sketcher object exists, so the data member,
theApp , does not exist either. If theApp is not declared as static , you can only create the
Sketcher object as a local variable in main() .
Even if Sketcher is running as an applet, the application window appears as a detached window from
the AppletViewer window, and it is still positioned relative to the screen.
Of course, when we come to implement the File menu, it will no longer be legal to derive the
Sketcher class from the JApplet class since it will contravene the rule that an applet must not access
the files on the local machine. It is also not recommended to create frame windows from within an
untrusted applet, so you will get a warning from the appletviewer about this.
All you need to do to revert back to just being an application is to remove the import statement for
javax.swing and remove extends JApplet from the Sketcher class header line. Everything else
can stay as it is.
Search WWH ::




Custom Search