Basic Methods of Applet class

void init()

This method is called once by the browser or applet viewer when the applet that it has been loaded into the system. It performs the initialization of an applet. Typical examples are initialization of instance variables and GUI components of the applet.

void start()

This method is called after the init method completes execution and every time the user of the browser returns to the HTML on which the applet resides (after browsing to another HTML page). Typical actions include starting an animation or other execution threads.

void paint(Graphics g)

This method is called automatically every time the applet needs to be repainted. For example, if the user of the browser covers the applet with another open window on the screen, then uncovers the applet, the paint method is called. (inherited from Container)

void stop()

This method is called by the browser or applet viewer when the applet should stop its execution, normally when the user of the browser leaves the HTML page on which the applet resides. Typical actions performed here are to stop execution of animation and threads.

void destroy()

This method is called by the browser or applet viewer when the applet is being removed from memory, normally when the user of the browser exits the browsing session. This method performs any tasks that are required to destroy any resources that it has allocated.

Example Java Applet

// Triangle.java
import java.awt.Graphics;
import javax.swing.JApplet;


public class Triangle extends JApplet {
	int x[] = new int[3];
	int y[] = new int[3];
	int painted;

	public void init()
	{
		infomsg("init");
		double r = 81;
		double xc = 100;
		double yc = 100;
		double phi = -Math.PI / 2.0;
		double delta = 2.0*Math.PI / 3.0;
		for (int i=0; i<3; i++) {
			x[i] = (int)(xc + r*Math.cos(phi+i*delta));
			y[i] = (int)(yc + r*Math.sin(phi+i*delta));
		}
		painted = 0;
	}

	public void start()
	{
		infomsg("start");
	}

	public void stop()
	{
		infomsg("stop");
	}

	public void destroy()
	{
		infomsg("destroy");
	}

	public void paint(Graphics g)
	{
		infomsg("paint " + ++painted);
		g.drawLine(x[0],y[0],x[1],y[1]);
		g.drawLine(x[1],y[1],x[2],y[2]);
		g.drawLine(x[2],y[2],x[0],y[0]);
	}
	

	public void infomsg(String str)
	{
		System.out.println(str);
	}
}
No Java 2 SDK, Standard Edition v 1.3 support for APPLET!!


Maintained by John Loomis, last updated 5 June 2000