About Applets Module 5 Applets An applet is a little application. Prior to the World Wide Web, the built-in writing and drawing programs that came with Windows were sometimes called "applets." On the Web, using Java, the object-oriented programming language, an applet is a small program that can be sent along with a Web page to a user. Java applets can perform interactive animations, immediate calculations, or other simple tasks without having to send a user request back to the server. Hierarchy of Applet Life Cycle of an Applet The life cycle of an Applet is as given below 1. init( ): This is the first method to be called. The variables required to run Applet are initialized inside this method. This method will be called only once during the runtime of the Applet. 2. start( ): This method is called after init( ). This method will be called every time Applet is restarted. Example, if user leaves web browser and comes back later, start( ) method will be loaded again.
3. paint( ): This method is called each time applet s output must be redrawn. Example, if user minimizes web browser and maximize it later. 4. stop( ): This method is called when web browser leaves the web page that contains the applet. When stop( ) is called, the applet is not terminated, its still running. It s like suspending the applet for a while. 5. destroy( ): This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet. An Applet Skeleton import java.awt.*; import java.applet.*; public class AppExample extends Applet{ public void init(){ // Do Initialization public void start(){ //Start or Resume Execution public void stop(){ //Suspends Execution public void destroy(){ //perform shutdown activities public void paint(graphics g){ //redisplay contents of window
Example 1: Sample Applet Application import javax.swing.japplet; import java.awt.graphics; public class MyApplet extends JApplet { public void paint(graphics g) { g.drawstring("my First Applet Program", 50, 25); Example 2: import java.applet.applet; import java.awt.*; public class MyApplet1 extends Applet { String msg; public void init(){ setbackground(color.blue); setforeground(color.white); msg = "INSIDE INIT() ---"; public void start(){ msg += "Inside Start() ---"; public void paint(graphics g){ msg += "INSIDE PAINT() ---"; g.drawstring(msg, 30, 80);
showstatus("displaying Status Window"); repaint( ) This method is used to redraw the output window. When this method is called, it in turn calls update( ) method of Component class that in turn calls the paint( ) method of the program to redraw the output window. There are four versions of repaint( ) method, they are 1. void repaint( ) // Repaints the entire window 2. void repaint(int left, int top, int width, int height) where, left & top coordinates from the upper left top corner width & height region to be repainted 3. void repaint(long maxdelay) 4. void repaint(long maxdelay, int left, int top, int width, int height) where, maxdelay time in milliseconds should be elapsed before calling update( ) method. Below program demonstrates the working of repaint method /* AppletRepaint.java */ import java.awt.*; import java.applet.*; public class AppletRepaint extends Applet implements Runnable { String msg = " A Running Banner "; Thread t = null;
int state; boolean stopflag; public void init() { setbackground(color.blue); setforeground(color.white); public void start(){ t = new Thread(this); stopflag = false; t.start(); public void run(){ char ch; for(;;){ try{ repaint(); Thread.sleep(5000); ch = msg.charat(0); msg = msg.substring(1, msg.length()); msg += ch; if(stopflag) break; catch(interruptedexception e){ public void stop(){ stopflag = true; t = null; public void paint(graphics g){ g.drawstring(msg, 50, 30);
Status Window It is used to display information at status bar. Prototype: showstatus( Message to be displayed ); The HTML Applet Tag <APPLET The below applet tag is used by Appletviewer and HTML to execute applets. Appletviewer executes the applets in new window, whereas HTML allows multiple applets to run in same window. Syntax: [CODEBASE = codebaseurl] CODE = appletfile [ALT = alternatetext] [NAME = appletinstancename] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels][hspace = pixels] >
[PARAM NAME = AttributeName VALUE = AttributeValue] [PARAM NAME = AttributeName VALUE = AttributeValue]. </APPLET> The attributes in square brackets are optional. CODEBASE: It is the path of the directory that will be searched for the applets executable class file i.e. specified by the CODE tag. CODE: It is the name of the applet compiled file to be run in browser. ALT: Alternate text is displayed when browser can t run applets. NAME: Name given to applet. Naming applet helps in identifying and communicating with other applets in same page. WIDTH and HEIGHT: Specifies the size of applets display area. ALIGN: Specifies the alignment of the applet. VSPACE and HSPACE: VSPACE specifies the space above and below the applet. HSPACE specifies the space left and right of the applet. Both, space are in pixels. PARAM NAME and VALUE: This tag allows passing parameter to applets in HTML page. The applets access this parameters using getparameter( ) method. getdocumentbase( ) and getcodebase( ) Displays the path of directory that holds the path of the HTML file that has started the applet (getdocumentbase). Displays the path of the applets class file from where the applet is loaded (getcodebase). Below Program demonstrates DocumentBase and CodeBase. import java.awt.*; import java.applet.applet; import java.net.*; public class getcodedoc extends Applet { public void paint(graphics g){ String msg; URL url = getcodebase(); msg = "Code Base:" + url.tostring(); g.drawstring(msg, 10, 40);
Output: url = getdocumentbase(); msg = "Document Base:" + url.tostring(); g.drawstring(msg, 10, 60);