Java Reference
In-Depth Information
Method
Description
void stop()
This method is called by the browser when the user moves off the page
containing the applet. You implement this to stop any operations that you
started in the start() method.
void destroy()
This method is called after the stop() method when the browser is shut
down. In this method you can release any resources your applet uses that
are managed by the local operating system. This includes such things as
resources used to display a window.
These are the basic methods you need to implement in the typical applet. We really need some graphics
knowledge to go further with implementing an applet, so we will return to the practical application of
these methods in Chapter 13.
Converting an Application to an Applet
Subject to the restrictions described in the previous section, you can convert an application to an applet
relatively easily. You just need to be clear about how each part of program executes. You know that an
application is normally started in the method main() . The method main() is not called for an applet
but the method init() is, so one thing you should do is add an init() method to the application
class. The other obvious difference is that an applet always extends the JApplet class.
We can demonstrate how to convert an application so that it also works as an applet, by changing the
definition of the Sketcher class. This doesn't make a very sensible applet, but you can see the
principles at work.
Try It Out - Running Sketcher as an Applet
You need to modify the contents of Sketcher.java so that it contains the following:
// Sketching application
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JApplet;
public class Sketcher extends JApplet {
public static void main(String[] args) {
theApp = new Sketcher(); // Create the application object
theApp.init(); // ...and initialize it
}
public void init() {
window = new SketchFrame("Sketcher"); // Create the app window
Toolkit theKit = window.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
// Set the position to screen center & size to half screen size
window.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
Search WWH ::




Custom Search