1. Overview

1.1. Definition

An applet is a graphic Java program made to run as part of other programs, usually a Browser. It should extend the class Applet.

1.2. Graphic Classes Hierarchy

Figure 12.1. Graphic Classes

Graphic Classes

Applets extend the Panel Class.

public class Simple extends Applet {
    . . .
    public void init() { . . . }
    public void start() { . . . }
    public void stop() { . . . }
    public void destroy() { . . . }
    . . .
}
  • init()

    To initialize the applet each time it's loaded (or reloaded).

  • start()

    To start the applet's execution.

  • stop()

    To stop the applet's execution.

  • destroy()

    To perform a final cleanup in preparation for unloading.

Audio in Portuguese

1.4.1. For Drawing

  • paint()

    The basic display method. Many applets implement the paint() method to draw the applet's representation within a browser page.

  • update()

    A method you can use along with paint() to improve drawing performance.

1.4.2. Event Handling

The Component class defines several methods (such as action() and mouseDown()) for handling particular types of events, and then one catch-all method, handleEvent().

1.4.3. GUI components

  • Buttons (java.awt.Button)

  • Checkboxes (java.awt.Checkbox)

  • Single-line text fields (java.awt.TextField)

  • Larger text display and editing areas (java.awt.TextArea)

  • Labels (java.awt.Label)

  • Lists (java.awt.List)

  • Pop-up lists of choices (java.awt.Choice)

  • Sliders and scrollbars (java.awt.Scrollbar)

  • Drawing areas (java.awt.Canvas)

  • Menus (java.awt.Menu, java.awt.MenuItem, java.awt.CheckboxMenuItem)

  • Containers (java.awt.Panel, java.awt.Window and its subclasses)

  • An applet can't load libraries or define native methods.

  • It can't ordinarily read or write files on the host that's executing it.

  • It can't make network connections except to the host that it came from.

  • It can't start any program on the host that's executing it.

  • It can't read certain system properties. Windows that an applet brings up look different than windows that an application brings up.

Audio in Portuguese

  • Applets can make network connections to the host they came from.

  • Applets running within a Web browser can easily cause HTML documents to be displayed.

  • Applets can invoke public methods of other applets on the same page.

  • Applets that are loaded from the local file system have none of the restrictions that applets loaded over the network do.

  • Although most applets stop running once you leave their page, they don't have to.

1.7.1. The Simplest Possible <APPLET> Tag

<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
</APPLET>

1.7.2. Specifying the Applet Directory with CODEBASE

<APPLET CODE=AppletSubclass.class CODEBASE=aURL
        WIDTH=anInt HEIGHT=anInt>
</APPLET>
<applet code=Simple.class codebase=example/ width=500 height=20>
</applet>

1.7.3. Specifying Parameters with the <PARAM> Tag

<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
<PARAM NAME=parameter1Name VALUE=aValue>
<PARAM NAME=parameter2Name VALUE=anotherValue>
</APPLET>

<applet code=AppletButton.class codebase=example width=350 height=60>
<param name=windowType value=BorderWindow>
<param name=windowText value="BorderLayout">
<param name=buttonText value="Click here to see a BorderLayout in action">
. . .
</applet>

1.7.4. Specifying Text to be Displayed by Java-Deficient Browsers

<applet code=AppletButton.class codebase=example width=350 height=60>
<param name=windowType value=BorderWindow>
<param name=windowText value="BorderLayout">
Your browser can't run 1.0 Java applets,
so here's a picture of the window the program brings up:</em>
</applet>

Audio in Portuguese