Java Applets
Java Applets
An applet is a small program that is embedded inside another application. Applets are not intended to run on their own. A Java applet is an applet written in the Java programming language. They are most commonly embedded in web pages to run in the environment of a web browser. Web pages are written in Hypertext Markup Language (HTML) . The web browser interprets the HTML source in order to render the pages on a display screen. Without using Java applets, HTML is quite limited in functionality, particularly in graphical user interfaces (GUIs) and multimedia. A Java applet is like a window application running within the embedding web page. It can provide much more sophisticated features in graphical user interfaces for interaction, as well as other functionalities such as animation and special effects.
The web browser must be equipped with the Java Virtual Machine (JVM) to handle web pages embedded with Java applets. The JVM is a Java interpreter that makes it possible to run compiled Java code; it enables the browser to run Java applets when rendering the embedding web page. The HTML source uses the APPLET tag to embed a Java applet in the web page. The parameters of the APPLET tag can specify the width and height of the window in the web page for the applet, and also refer to the file for the compiled code of the Java applet. The file usually would have the ".class" extension in the file name. This extension in the naming convention indicates that it is a file for compiled Java code. The following illustrates a simple HTML source sequence to embed a Java applet in the file myapplet.class.
<APPLET height 100 width 200 code "myApplet.class">
</APPLET>
In the preceding <APPLET> tag, the parameters height and width instruct the browser to reserve a window in the web page, 100 pixels high and 200 pixels wide, as screen real estate for the applet. The compiled code of the Java applet is in the file myApplet.class, specified for the parameter code. To render the web page, the browser accesses the file for the applet code, and runs the JVM to execute the applet. The </APPLET> tag closes the scope of the <APPLET> tag, as required in the syntax of HTML.
A Java applet is different from a regular Java application program in that it is not started with its main method. Instead, the first time the browser renders the web page, the JVM loads the applet code from the file specified. To run the applet, it invokes the init method first and then the start method. Thereafter, whenever the browser leaves the web page, the JVM invokes the stop method; and subsequently whenever the browser returns to the page, it invokes the start method again. The start and stop methods are then repeated as many times as the browser would enter and leave the web page. Before the browser unloads the applet from its cache memory, the JVM then invokes the destroy method. For every instance of a Java applet embedded in a web page, the init method is always the first to be invoked, and is invoked only once. Thereafter, the start and stop methods may be invoked as many times as needed. The destroy method is also invoked only once and is always the last method invoked before the applet is unloaded from the cache (such as when the browser terminates).
The init method is designed for the applet to acquire and set up resources for use. For example, if a person would like a web page to play music whenever the browser opens the page, he or she will use the init method to acquire the audio channels for use, and set up the audio file for playback. The person will then use the start method to begin playback of the audio file. Whenever the browser leaves to go to another page, the stop method will stop playback and mark the position of the music playback. When the browser returns to the web page, the start method can then resume music playback at the place where it left off at the previous stop method. The destroy method releases the resources acquired, since it is the last method invoked. These four methods are often called the life cycle methods of a Java applet; the method declarations are listed:
public void init(void);
public void start(void);
public void stop(void);
public void destroy(void);
The parameters height and width in the <APPLET> tag assign an applet screen real estate in the web page for graphical display. Whenever it becomes necessary to refresh or update the display, the JVM of the browser invokes the paint method of the applet:
public void paint(Graphics g);
The parameter "g" is the Graphics object for the window in the embedding web page of dimensions specified by the height and width in the <APPLET> tag. The paint method can use it to draw the content for display in the web page.
A Java applet must support all these methods: init, start, stop, destroy, and paint in order to function properly. The Java class library provides the Applet class with default implementation for all five methods. One can make use of inheritance in object-oriented programming when he or she derives an applet class from Applet; the applet will then inherit these methods from the base class Applet. One has to implement the methods in the applet class only when he or she needs to override the default implementation.
A simple Java applet, myApplet, will illustrate the functionality. The complete HTML source of the web page, embedding the applet using the <APPLET> tag, follows:
<HTML>
<HEAD><TITLE>emyApplet</TITLE></HEAD>
<BODY> <H1> My Applet</H1>
<HR><APPLET CODE="myApplet.class" WIDTH=200 HEIGHT=50></APPLET> <HR>
</BODY>
</HTML>
The web page shows the heading "My Applet" and the window for the applet. The complete source program for the Java applet follows:
// myApplet.java -a simple Java applet example. //
import java.applet.*; // to use class Applet
import java.awt.*; // to use Color
public class myApplet extends Applet
{int count = 0;
Color spectrum[] = new Color[3];
// init: to set up resources of 3 colors.
public void init()
{spectrum[0] = Color.blue;
spectrum[1] = Color.red;
spectrum[2] = Color.green;
}// start: to increment count for next color.
public void start()
{count = count + 1;
}// paint: to update content of display.
public void paint(Graphics g)
{g.setColor(spectrum[count%3]); // Change color.
g.drawString("Applet DEMO: "+count,10,20);
} }The applet keeps a count of how many times the start method has been invoked. The applet paints the text string "Applet DEMO" followed by the count. The color of the text rotates in the sequence of blue, red, and green: every time the browser leaves and then returns to the page, the color changes, because the start method increments the count. Figure 1 shows the web page with the browser returning the third time.
see also Internet; Object-Oriented Languages; Procedural Languages; Programming; World Wide Web.
Peter Y. Wu
Bibliography
Coad, Peter, Mark Mayfield, and Jonathan Kern. Java Design: Building Better Apps and Applets, 2/e, 2nd ed. Upper Saddle River, NJ: Yourdon Press, 1999.
Gottleber, Timothy T. Excellent HTML with an Introduction to Java Applets. Boston: Irwin/McGraw-Hill, 1998.