Java Reference
In-Depth Information
In the preceding applet, the statement in Line 1 invokes the paint method of the
class JApplet . Notice that the method paint uses a Graphics object g as the argument.
Recall that the class Graphics is an abstract class, and for this reason, you cannot
create an instance of the class Graphics . The system will create a Graphics object for
you; you need not be concerned about it. The statement in Line 2 draws the string
"Welcome to Java Programming" at the coordinate position (30, 30) .
Until now, when we created a GUI application program, we used methods such as
setTitle and setSize . As you can see in the preceding applet, such methods are not
used in an applet. Note the following about applets:
￿ The method setTitle is not used in applets because applets do not have
titles. An applet is embedded in an HTML document, and the applet
itself does not have a title. The HTML document may have a title, which
is set by the document.
￿ The method setSize is not used in applets because the applet's size is
determined in the HTML document, not by the applet. You do not need
to set the size of the applet.
￿ You do not need to invoke the method setVisible .
￿ You do not need to close the applet. When the HTML document
containing the applet is closed, the applet is destroyed.
￿ There is no method main .
As with an application, you compile an applet and produce a .class file. Once the
.class file is created, you need to place it in a Web page to run the applet. For
example, you can create a file with the .html extension, say, WelcomeApplet.html ,
with the following lines in the same folder where the WelcomeApplet.class file
resides:
<HTML>
<HEAD>
<TITLE>WELCOME APPLET</TITLE>
</HEAD>
<BODY>
<OBJECT code = "WelcomeApplet.class" width = "250"
height = "60">
</OBJECT>
</BODY>
</HTML>
Once the HTML file is created, you can run your applet either by opening
WelcomeApplet.html with a Web browser, or you can enter:
appletviewer WelcomeApplet.html
at a command-line prompt, if you are using the JDK (Java Development Kit).
Search WWH ::




Custom Search