This web page requires a Java enabled web browser.

Beginning Java

How a Java Applet Works

Java applets are made to be run from a browser. The browser, then, is what loads and runs the Java applet. There is a specific interface between a Java applet and the browser that calls it.

Life Cycle of an Applet

There are five major events in the life of any Java applet:

  1. Init - The browser loads the applet into memory and creates an instance of its class. At this point the applet may need to be initialized. Done only once during a browser session.
  2. Start - The browser starts the applet running. This occurs every time the html page containing the applet is visited.
  3. Paint - The display part of the applet is painted on the screen. This can occur many times due to events that require the applet to repaint its display area.
  4. Stop - The browser stops the applet running. This occurs every time the html page containing the applet is exited.
  5. Destroy - As the browser session is closed (you exit the browser) it delets the instance of the applet class.

For each of these five major events there is a method in the applet class that is called by the browser:

  1. public void Init() - This method sets up display configuration and values that are set for the entire session.
  2. public void Start() - Every time the html page containing the applet is visited the browser calls the applets Start method. If the applet has, for example, animation or sound this would be started in this method.
  3. public void Paint(java.awt.Graphics g) - The display part of the applet is painted on the screen. This can occur many times due to resizing or other events that require the applet to repaint its display area. The display part of the applet is a Graphics object called g.
  4. public void Stop() - Every time the html page containing the applet is exited (going to the next page) the browser calls the applets Stop method. This method is where the applet would stop animation or sound (see Start).
  5. public void Destroy() - When you exit the browser it destroys all Java applets that were loaded during that session. The browser calls the applets Destroy method.

The following Java applet illustrates these five events in the life of an applet:

/*
	TestApplet.java - testing how an applet works
*/

public class TestApplet extends java.applet.Applet
{
	java.applet.AudioClip audio;

	public void init()
	{
		System.out.println("Initializing TestApplet...");
		audio = getAudioClip(getDocumentBase(), "xxxx.au");
		resize(150,100);
	}
	public void start()
	{
		System.out.println("Starting TestApplet...");
		audio.loop();
	}
	public void paint(java.awt.Graphics g)
	{
		g.drawString("Java TestApplet",10,10);
		System.out.println("Paint TestApplet...");
	}
	public void stop()
	{
		System.out.println("Stopping TestApplet...");
		audio.stop();
	}
	public void destroy()
	{
		System.out.println("Destroying TestApplet.... Bye bye.\n");
		audio.stop();
	}
}

The above Java code plays an AU file over and over while you are on the page containing the applet. In fact, this page is playing an audio file, assuming everything is set up properly (You should be quite sick of listening to it by now).

AppletViewer is just a simple browser that launches Java applets from a command line. You can use it to launch this applet and the applet will print on the command line each time one of the methods is called. Try resizing the applet in applet viewer. You will see that the paint method is called each time.