Applets


An Applet is a Java program that specifically runs in a browser. As such, there are severe security restrictions to prevent access to the users computer.

Applet extends Panel, a component that we will look at in more detail later.

First, it's a good idea to import the package for Applet:

	import java.applet.*;
Then, your class needs to extend Applet:
	public class SomeApplet extends Applet {
	}
Finally, there are a few methods you may want to over-ride:
	public void init() {

		/*
		 * code that is run the first time the Applet is loaded
		 * note that once an Applet is loaded by the browser, you have to close the
		 * browser in order to re-load the class file and any changes to it
		 */

	}

	public void paint(Graphics g) {
		// code to control what is painted (drawn) on the Applet panel
	}

	public void start() {
		// code to execute whenever the browser (and Applet) receives focus
	}

	public void stop() {
		// code to execute whenever the browser (and Applet) loses focus
	}
If you include a constructor in the class, that code will be executed automatically before the init method.

To display an applet in a browser, you need an HTML file with the appropriate tag. The tag to use depends on whether or not you use the Swing components and/or Java 1.2 specific code. Browsers only support Java 1.1.

For a 1.1 compliant Applet, use the applet tag:

	<applet class='SomeApplet' codebase='cgi-bin' height='100' width='200'>
	</applet>
If you want to use 1.2 or Swing components in an applet, the tag to use will depend on the browser:
	<!-- Internet Explorer -->
	<object classid='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93'
		height='100'
		width='200'
		codebase='http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0'
	>
		<param name='code' value='demo.class'>
		<param name='codebase' value='.'>
		<param name='type' value='application/x-java-applet;version=1.2'>
	</object>

	<!-- Netscape Navigator -->
	<embed code='demo.class'
		codebase='.'
		height='100'
		width='200'
		pluginspage='http://java.sun.com/products/plugin/1.2/plugin-install.html'
		type='application/x-java-applet;version=1.2'
	>
	<noembed>
It is possible to pass parameters to an applet from the HTML page by using the param tag inside either the applet or object tags. The param tag is not supported inside the embed tag. Take a look again at the object tag above, and you will see an example of how to use the param tag. Inside the Applet itself, you can retrieve the parameter as follows:
	String value1= getParameter("name1");
Note that this method always returns a String. If you need it as a number, you will need to parse it accordingly.

It is possible to call public methods of an Applet from JavaScript. To see this in action, go to the "JavaScript calling Java" resource for this week. Click on the button for the greeting, view the HTML source to see how it's done.

It is also possible to call javaScript functions from a JavaApplet.

First, you need the following classes from the netscape.javascript package:

  1. JSException.class
  2. JSObject.class
  3. JSProxy.class

Here is an example of an Applet that calls a JavaScript function that takes one parameter:

	private JSObject htmlWin;

	public void init() {
		htmlWin = JSObject.getWindow(this);
	}

	private void navigate(String anchor) {
		String strArgs[] = new String[1];
		strArgs[0] = anchor;
		htmlWin.call("reNavigate", strArgs);
	}
Finally, in the <applet> tag on the HTML page you will need to include the "mayscript" attribute.

Starting as either an Application or an  Applet

	public class SomeClass {
	
		private Frame frame = null;
		private Panel panel = new Panel();

		private static int appType = 0;

		public SomeClass {
			switch (appType) [
				case 0:
					add(panel);
					break;
				case 1:
					frame = new Frame("SomeClass Demo");
					frame .add(gbdPanel);
					frame .addWindowListener(this);
					frame .setSize(500,500);
					frame .show();
					break;
			}
		}

		public void init() {}

		public static void main(String args[]) {
			appType = 1;
			SomeClass sc = new SomeClass ();
		}
	}