Sams Teach Yourself C in 21 Days

(singke) #1
Line 3: Applets must be declared as subclasses of the Java class Applet. This class con-
tains most of the “behind-the-scenes” machinery that makes applets work.
Line 5: The initmethod is the first thing called when an applet is run. It is an applet’s
equivalent to a constructor method.
Line 7: The startmethod is called immediately after the initmethod. In many ways, it
is equivalent to the mainmethod in stand-alone applications.
Line 9: The stopmethod is called when the applet is being terminated. It is always
called before the destroymethod.
Line 11: The destroymethod is the last method called when the applet is shutting down.
Line 13: The paintmethod is called when the applet’s screen display requires updating.
You put the code that draws the applet’s visual interface here.
One important thing to note about these methods is that you never call them directly
yourself. They are automatically called, when and if needed, by the browser in which the
applet is running. You put code in these methods, as required by the applet, to perform
initialization steps and to clean up upon termination. Some simpler applets will use only
one or two of these methods; more complex applets might use all of them.

Putting an Applet on a Web Page ..................................................................


Most modern Web browsers support Java, but there are still some older browsers in use
that do not. Also, even with a browser that does support Java applets, this support might
be turned off for various reasons. Fortunately, a browser that does not have Java support
will simply ignore an applet included in a Web page.
To include an applet on a Web page you use the <APPLET>tag in the page’s HTML. The
syntax is
<APPLET Name = AppletClassName
Code = FullPathToApplet
WIDTH = w
HEIGHT = h
ALT=text>
</APPLET>
AppletClassNameis the name of the class file (appletname.class) created when you com-
pile the applet. FullPathToAppletis the full path to the .class file. The wandhvalues
specify the size of the applet window, in pixels. Textis text that will be displayed only if
the browser does not support Java. This is optional, but a good idea. A Web page can
have multiple applet on it, each defined by its own <APPLET>tag. Here is an example of a
real<APPLET>tag:

760 Bonus Day 6

41 448201x-Bonus6 8/13/02 11:23 AM Page 760

Free download pdf