Using Applet Life Cycle methods
With this knowledge of applets, let us write a small applet to understand the syntax of developing an applet. Let us start from life cycle methods.
As you can guess, two programs exist, one applet (LifeTest.java) and one HTML (Life.html) file.
1st Program: Applet program – LifeTest.java
public class LifeTest extends Applet
public void init()
System.out.println("init(): applet started");
public void start()
System.out.println("start(): applet activated");
public void paint(Graphics g)
System.out.println("paint(): applet running");
public void stop()
System.out.println("stop(): applet inactivated ");
public void destroy()
System.out.println("destroy(): applet destroyed");
2nd Program: HTML Program – Life.html
<applet code="LifeTest.class" width="250" height="125">
Screenshot of Life.html of Applet Life Cycle
Compilation and Execution
In compilation, no difference with applets; only execution differs.
|Compilation||C:\snr\way2java\applets> javac LifeTest.java|
|Execution||C:\snr\way2java\applets> appletviewer Life.html|
As discussed earlier, the above HTML file can be run in two ways. Anyhow, let us run this with appletviewer as println() methods do not work in a browser. We use browser in later applets.
All the five methods of Applet Life Cycle are included in the above applet program and stuffed with just println() statements; the best way to understand which method is called when. When applet is destroyed, the browser calls stop() and destroy() methods. The output will be obtained at DOS prompt. The above screenshot shows clearly.