Applets can be executed in two ways: from Browser or from Appletviewer. The JDK provides the Appletviewer utility.
Browsers allow many applets on a single page, whereas applet viewers show the applets in separate windows.
Program, named SampleApplet, demonstrates the life-cycle methods of applets:
Program: A demonstration of life-cycle methods of applets.
import java.applet*;
import java.awt.*;
import java.awt.event.*;
/*<APPLETcode = “SampleApplet” width=300 height=150> </APPLET>*/
public class SampleApplet extends Applet implements ActionListener
{
Button b = new Button (“Click Me”);
boolean flag = false;
public void init ()
{
System.out.println (“init () is called”);
add(b);
b.addActionListener (this );
}
public void start ()
{
System.out.println (“start () is called”);
}
public void destroy ()
{
System.out.println(“destroy () is called”);
}
public void stop ()
{
System.out.println(“stop () is called”);
}
public void actionPerformed (ActionEvent ae)
{
flag = true;
repaint ();
}
public void paint(Graphics g)
{
System.out.println (“paint () is called”);
if (flag)
g.drawString (”This is a simple Applet”,50,50);
}
}
For executing the program using the AppletViewer or the web browser, first the program is to be compiled in the same way as the Java application program as shown below:
C:\>Javac SampleApplet.java
We’ll be covering the following topics in this tutorial:
Running the applet using AppletViewer
AppletViewer is a program which provides a Java run-time environment for applets. It accepts a HTMLfile as the input and executes the <applet> reference, ignoring the HTML statements. Usually, AppletViewer is used to test Java applets.
To execute any applet program using AppletViewer the applet tag should be added in the
comment lines of the program. These comment lines will not be considered when the programs are compiled. The command AppletViewer <appletfile.java> is used to view the applet .To execute Program, we can invoke the same as follows:
c:\>appletviewer SampleApplet.java
The reader can thus execute Program. It is suggested as an exercise.
The drawstring () method draws the String ‘This is a sample applet’ on the applet panel, starting at the coordinate (50, 50). Note that the applet panel provides a graphical environment in which the drawing will paint rather than print. Usually, the panel is specified in terms of pixels in which the upper left comer is the origin (0, 0).
As mentioned earlier, applets interact with the user through AWT. In Program we have used one AWT component-button. Observe the SampleApplet class header:
public class SampleApplet extends Applet implements ActionListener
The ActionListener interface is implemented by a class to handle the events generated
by components such as buttons. The method actionPerformed () available in this interface is overridden so that some action is performed when the button is pressed. In Program, it can be observed that when the applet is first executed, the paint () method does not display anything on the applet window. This is because flag is set to false. When the button is pressed (observe the actionPerformed () method) the flag is set to true and repaint () method is called. This time since the flag is set to true, the repaint () method will display the string ‘This is a simple applet’.
In Program we use the System.out.println () method to print the sequence of the methods executed when the applet is running.
The output shown in the console is in the following:
init () is called;
start () is called;
paint () is called;
stop () is called;
start () is called;
paint () is called;
stop () is called;
destroy () is called;
When the applet program is first executed, three methods-init (), start (), paint () are executed. Later, if the applet window is minimized, the stop () method is executed. If the applet window is maximized again, the start () and the paint () methods will be executed simultaneously, When the applet window is closed, the stop () and the destroy () methods will be called simultaneously.
Running the applet using the web browser
The browsers that support applets include Netscape Navigator, Internet Explorer and Hot Java. The applet can be viewed from a web browser by writing the APPLET tag within the HTML code. To run the Program in the web browser, the following html code has to be written:
<html>
<head>
<title> A sample Applet</title >
</head>
<body>
<applet code=”SampleApplet” width=200 height=200>
</applet>
</body>
</html>
To run the applet, the browser, which could be the Internet Explorer for instance, has to be opened, the options File Open have to be selected. Then, the applet code HTML file is opened by using the Browse button.
To show the difference between running the applet in the applet viewer and the web browser Program is modified a little to the form shown in Program:
Program An alternate way of running an applet.
import java.applet.*;
import java.awt. *;
public class SampleApplet extends Applet
{
String msg = “”;
public void init ()
{
msg = msg + “init ();”
}
public void start ()
{
msg = msg+ “start ();”
}
public void destroy ()
{
msg = msg+ “destroy ();”
}
public void stop ()
{
msg = msg+ “stop ();”
}
public void paint(Graphics g)
{
msg = msg+ “paint ();”
g.drawString(msg,10,20);
}
}
The button is removed and the life-cycle methods are displayed on the applet window instead of on the console. The difference between the output of the above applet when called using the AppletViewer and the web browser is in Figure.
The output of Program shows the applet with the sequence of the methods executed. When the applet program is first executed, the init (), start () and paint () methods are called.
When the web browser is minimized, the paint () method is called, and when it is maximized, paint () is called again. In the case of the applet viewer the stop (), start () and paint () methods are called when the applet is maximized. The same things happen when the applet viewer and the web browser are’ closed. That is, the stop () method and the destroy () method are called. When moving from the web page containing the applet to another web page and back, the start () method in the applet is called.