J.E.D.I.
public class AppletDemo extends Applet { public void paintGraphics g {
g.drawStringHello world, 80, 25; }
}
After compilation, try running this example using the java command. What happens? Remember that applets are special Java applications. They are not executed using the
java command. Instead, the applet is run on a web browser or using the applet viewer. To open the applet through a web browser, simply open the HTML document in which the
applet is embedded using the applet HTML tag the commented out code in the Hello World example.
Another way of running an applet is through the appletviewer command. Simply follow this syntax:
appletviewer java filename For example, to run the given applet example, use:
appletviewer AppletDemo.java The HTML tag in the given example indicates that an applet be created with a width of
300 pixels and a height of 100 pixels. Then, the drawString method draws the Hello world string at the pixel position 80,25 counting down then right.
Figure 11.1: Applet example
When creating an applet, it is necessary to extend the Applet class. As mentioned previously, the class is found in the java.applet package. Hence, importing the
java.applet package is a must. Also, it was mentioned before that the Applet class is a subclass of the Panel class. This implies that some methods of the Applet class are found
in the Panel class. To access methods or fields in the Panel class or other ancestor classes, it is necessary to import the java.awt package.
11.3 Applet Methods
This section discusses applet methods youll find useful.
11.3.1 The Applet Life Cycle
Instead of starting execution at the main method like in typical Java applications, the
Introduction to Programming II Page 150
J.E.D.I.
browser or the applet viewer interacts with the applet through the following methods: 1. init
init is the first method called. It is invoked exactly once when the applet is loaded. 2. start
After the invoking the init method, start is the next method called. It is invoked everytime the applets HTML document is displayed. Execution resumes with this
method when the applet is redisplayed. 3. stop
When the web browser leaves the applets HTML document, this method is called to inform the applet that it should stop its execution.
4. destroy This method is called when the applet needs to be removed from the memory
completely. The stop method is always called before this method is invoked.
Introduction to Programming II Page 151
J.E.D.I.
When creating applets, at least some of these methods are overriden. The following applet example overrides these methods.
import java.applet.; import java.awt.;
applet code=LifeCycleDemo width=300 height=100 applet
class LifeCycleDemo extends Applet { String msg =;
public void init { msg += initializing... ;
repaint; }
public void start { msg += starting... ;
repaint; }
public void stop { msg += stopping... ;
repaint; }
public void destroy { msg += preparing for unloading...;
repaint; }
public void paintGraphics g { g.drawStringmsg, 15, 15;
} }
The following is a sample html document embedded with a LifeCycleDemo applet. HTML
TITLELife Cycle DemoTITLE applet code=LifeCycleDemo width=300 height=100
applet HTML
Introduction to Programming II Page 152
J.E.D.I.
11.3.2 The paint Method
Another important method is the paint method, which the Applet class inherits from its ancestor class Component. It is invoked everytime the applets output needs to be
redrawn. An example of such an instance is when an applet hidden by another window is made visible again. The method is usually overriden when you want to customize how
your applet should look like. In the Hello World example, the applet has the Hello world string on the background after having overriden the paint method.
11.3.3 The showStatus Method