Applets

A Java applet must be run from within another program, called a host application. At this point, most host applications are Web browsers. The interaction between an applet and its host application is rather involved.

From the viewpoint of an applet, the interaction involves defining a subclass of the java.applet.Applet class. The Applet class defines a number of methods that control the applet. A subclass of Applet overrides one or more of the methods:

From the viewpoint of the host application, the interaction typically follows a standard sequence of events. The host application usually does the following:

  1. Installs a SecurityManager object to implement a security policy.
  2. Creates a ClassLoader object to load the applet.
  3. Loads the applet and calls its default constructor.
  4. Passes an AppletStub object to the applet's setStub() method.
  5. Calls the applet's init() method in a separate thread.
  6. Marks the applet as active.
  7. Starts a new thread to run the applet's start() method.
  8. Calls the applet's show() method, which makes the applet visible and causes the applet's paint() method to be called for the first time.
  9. Calls the applet's paint() method whenever the applet needs to be refreshed.
  10. Calls the applet's start() and stop() methods when the host wants the applet to start or stop. These methods are typically called when the applet is exposed or hidden.
  11. Calls the applet's hide() method followed by its destroy() method when the host wants to shut down the applet.

Embedding an Applet in a Web Page

Web pages are written in a language called HTML. This explanation of how to embed an applet in a Web page assumes that you have some knowledge of basic HTML. An applet is embedded in a Web page using an <applet> tag. A minimal <applet> tag looks as follows:

<applet code=Clock height=300 width=350>
</applet> 

The code attribute of this sample <applet> tag specifies that the applet to be run is a class named Clock. The width and height attributes specify that the applet should be given a screen area that is 300 pixels high and 350 pixels wide.

The following list shows all of the attributes that can be specified in an <applet> tag. The attributes should be specified in the order in which they are listed. The code, height, and width attributes are required in an <applet> tag; the other attributes are optional:

Applet-specific parameters can be provided to an applet using <param> tags inside the <applet> tag. A <param> tag must specify name and value attributes. For example:

<param name=speed value=65> 

If a Web browser does not support the <applet> tag, it ignores the tag and simply displays any HTML content provided inside the tag. However, if the browser understands the <applet> tag, this HTML content is ignored. This means that you can provide HTML content inside an <applet> tag to inform users of non-Java-enabled browsers about what they are missing.

Here is an example that combines all of these elements:

<applet code=Compass height=400 width=300>
<param name=direction value=north>
<param name=speed value=65>
<p>
<i>If you can see this message, your Web browser is not Java enabled. There is a Java applet on this Web page that you are not seeing.</i>
<p>
</applet> 

If a non-Java-enabled browser is used to view this HTML file, the following text is displayed:

If you can see this message, your Web browser is not Java-enabled. There is a Java applet on this Web page that you are not seeing.