| Applet should be seen as another web-based resource. | |||
| Java programs that run in a browser. Used on the Web. | |||
| Applet Class | An applet must extend the java.awt.Applet class. | ||
| //Packages usually used
import java.awt.Applet; import java.awt.Graphics; import java.awt.event.*;
public class xxx extends Applet {……} |
|||
| Applet methods | public void init() | Called once after applet is created and before anything as been shown on the screen. | |
| public void start() | Called when applet is downloaded and becomes visible after been hidden. | ||
| public void paint(Graphics g) | Overridden method from the java.awt.Component class. Used to render (draw) the applet. | ||
| public void stop() | Called when the applet is hidden. | ||
| public void destroy() | Called by JVM when applet is about to be permanently disposed. | ||
| An applet must extend the java.awt.Applet class. | |||
| HTML | An applet can be inserted in a html page to provide dynamic content. | ||
| A java enabled browser is needed. | |||
| Applet tag | <applet code=Metrics.class width=200 height=200>
</applet> |
||
| code | Identifies the compiled class file used as the applet. | ||
| The is found relative to the IP address and directory containing the HTML file embedding the <applet> tag. | |||
| Absolute path can be given. | |||
| base tag | The HTML base tag can change where the class file is sought. | ||
| width and height | The width and height of the applet size in pixels. These are required. | ||
| codebase | Provides the location to find resources for the applet such as images, sounds and the applet itself. | ||
| This can be the absolute or relative path. If a base HTML tag is not defined, then the value is relative to where the HTML document URL. | |||
| If the base tag is defined, then resources are found relative to the base tag value. | |||
| name | Provides a name for the applet. The AppletContext object with its method, getApplet(), can retrieve the applet environment information. | ||
| Exam Tip | The width and height of the applet is required | ||
| Passing Parameters | |||
| <param name=.. value=..> | The <param> tag allows the passing of data as a parameter to the applet. | ||
| The data is retrieved by the applet as a String. | |||
| <param name=state value=”Long Island”> | |||
| Parameter values can be passed directly from the <applet> tag. | |||
| <applet code=”mycode.class” extra=”myparam” width=200 height=200> | |||
| However the width and height values can not be passed as a <param tag> if left out in the <applet> tag. | |||
| Retrieving the Parameters | Using the static method Applet.getParameter(“xxx”) of the Applet class, an applet tag value can be retrieved. | ||
| import java.applet.Applet;
public class Tag extends Applet { public void init() { System.out.println(getParameter(“code”)); } } |
|||
| If the value passed as an argument in the getParameter() method is not found in the applet tag a null value is returned. | |||
| Other Applet Class Method | String() getParameterInfo() | To make an applet self documenting. The getParameterInfo() is overridden. An array of string is returned providing information about the applet. | |
| A user of the applet calls the getParameterInfo() method to get information about the applet. | |||
| URL getDocumentBase() | The static method getDocumentBase() retrieves the applet document URL, the HTML. | ||
| URL getCodeBase() | The static method getCodeBase() retrieves the applet URL, the applets URL, where the applet came from. | ||