Overview of Applets |
This lesson gave you lots of information -- almost everything you need to know to write a Java applet. This page summarizes what you've learned, adding bits of information to help you understand the whole picture.First you learned that to write an applet, you must create a subclass of the
java.applet Applet
class. In yourApplet
subclass, you must implement at least one of the following methods:init
,start
, andpaint
. Theinit
andstart
methods, along withstop
anddestroy
, are called when major events (milestones) occur in the applet's life cycle. Thepaint
method is called when the applet needs to draw itself to the screen.The
Applet
class extends the AWTPanel
class, which extends the AWTContainer
class, which extends the AWTComponent
class. FromComponent
, an applet inherits the ability to draw and handle events. FromContainer
, an applet inherits the ability to include other components and to have a layout manager control the size and position of those components. FromApplet
, an applet inherits several capabilities, including the ability to respond to major milestones, such as loading and unloading. You'll learn more about what theApplet
class provides as you continue along this trail.You include applets in HTML pages using the
<APPLET>
tag. When a browser user visits a page that contains an applet, here's what happens:
- The browser finds the class file for the applet's
Applet
subclass. The location of the class file (which contains Java bytecodes) is specified with theCODE
andCODEBASE
attributes of the<APPLET>
tag.- The browser brings the bytecodes over the network to the user's computer.
- The browser creates an instance of the
Applet
subclass. When we refer to an applet, we're generally referring to this instance.- The browser calls the applet's
init
method. This method performs any one-time initialization that is required.- The browser calls the applet's
start
method. This method often starts a thread to perform the applet's duties.An applet's
Applet
subclass is its main, controlling class, but applets can use other classes, as well. These other classes can be either local to the browser, provided as part of the Java environment, or custom classes that you supply. When the applet tries to use a class for the first time, the browser tries to find the class on the host that's running the browser. If the browser can't find the class there, it looks for the class in the same place that the applet'sApplet
subclass came from. When the browser finds the class, it loads its bytecodes (over the network, if necessary) and continues executing the applet.Loading executable code over the network is a classic security risk. For Java applets, some of this risk is reduced because the Java language is designed to be safe -- for example, it doesn't allow pointers to random memory. In addition, Java-compatible browsers improve security by imposing restrictions. These restrictions include disallowing applets from loading code written in any non-Java language, and disallowing applets from reading or writing files on the browser's host.
Overview of Applets |