Java Reference
In-Depth Information
Here, message is the string to be output beginning at x , y . In a Java window, the upper-left
corner is location 0,0. The call to drawString( ) in the applet causes the message to be dis-
played beginning at location 20,20.
Notice that the applet does not have a main( ) method. Unlike the programs shown earli-
er in this topic, applets do not begin execution at main( ) . In fact, most applets don't even
have a main( ) method. Instead, an applet begins execution when the name of its class is
passed to a browser or other applet-enabled program.
After you have entered the source code for SimpleApplet , you compile in the same way
that you have been compiling programs. However, running SimpleApplet involves a dif-
ferent process. There are two ways in which you can run an applet: inside a browser or with
a special development tool that displays applets. The tool provided with the standard Java
JDK is called appletviewer , and we will use it to run the applets developed in this chapter.
Of course, you can also run them in your browser, but the appletviewer is much easier to
use during development.
NOTE
Beginning with the release of Java 7, update 21, Java applets must be signed to prevent
security warnings when run in a browser. In fact, in some cases, the applet may be preven-
ted from running. Applets stored in the local file system, such as you would create when
compiling the examples in this topic, are especially sensitive to this change. You may
need to adjust the security settings in the Java Control Panel to run a local applet in a
browser. At the time of this writing, Oracle recommends against the use of local applets,
recommending instead that applets be executed through a web server. Furthermore, un-
signed local applets may be (probably will be) blocked from execution in the future. In
general, for applets that will be distributed via the Internet, such as commercial applica-
tions, signing is a virtual necessity. The concepts and techniques required to sign applets
(and other types of Java programs) are beyond the scope of this topic. However, extensive
information is found on Oracle's website. Finally, as mentioned, the easiest way to try the
applet examples is to use appletviewer .
One way to execute an applet (in either a Web browser or the appletviewer ) is to write
a short HTML text file that contains a tag that loads the applet. At the time of this writing,
Oracle recommends using the APPLET tag for this purpose. (The OBJECT tag can also be
used, and other deployment strategies are available. Consult the Java documentation for the
latest information.) Using the APPLET tag, here is the HTML file that will execute Sim-
pleApplet :
Search WWH ::




Custom Search