More Java Techniques 759
BD6
Differences Between Applets and Applications ............................................
For the most part, programming a Java applet is no different from programming a stand-
alone application. In fact, it is possible to write a Java program that is both an application
and an applet. The differences between applet and application program can be summed
up as follows:
- For security reasons, applets cannot access files. It is obvious that you would not
want a Web page snooping around on your hard disk, uploading your personal
financial information, or deleting essential system files. - An applet does not need a mainmethod. Because the execution of an applet is con-
trolled by the browser it is running in, there is no need for main. If a program is
designed to be both applet and application, it will have a main, but this method will
be ignored when the program is running as an applet. - Applet programming is a little simpler than application programming because the
browser that the applet runs in takes care of some details, such as sizing and posi-
tion the program window—things that a stand-alone application has to do for itself. - Console input/output is meaningless for an applet.
Understanding the Structure of an Applet ....................................................
To get started with applets, look at Listing B6.7. This is not a real applet, but rather the
bare skeleton of an applet. The analysis that follows explains the various parts.
LISTINGB6.7 applet.java. The skeleton of a Java applet
1: import java.applet.Applet;
2:
3: public class AppletTest extends Applet {
4:
5: public void init() {}
6:
7: public void start() {}
8:
9: public void stop() {}
10:
11: public void destroy() {}
12:
13: public void paint() {}
14: }
Line 1: All applets must import java.applet.Appletin addition to any other
classes they need.
ANALYSIS
41 448201x-Bonus6 8/13/02 11:23 AM Page 759