Life Cycle of an Applet

Life Cycle of an Applet


Four strategies in the Applet class gives you the system on which you assemble any genuine applet − 

init − This strategy is proposed for whatever instatement is required for your applet. It is called after the param labels inside the applet tag have been prepared. 

begin − This strategy is consequently called after the program calls the init technique. It is likewise called at whatever point the client comes back to the page containing the applet subsequent to having headed out to different pages. 

stop − This strategy is consequently called when the client gets off the page on which the applet sits. It can, along these lines, be called over and again in a similar applet. 

demolish − This technique is just called when the program close down regularly. Since applets are intended to live on a HTML page, you ought not ordinarily desert assets after a client leaves the page that contains the applet. 

paint − Invoked quickly after the begin() strategy, and furthermore whenever the applet needs to repaint itself in the program. The paint() strategy is really acquired from the java.awt. 

A "Welcome, World" Applet 

Following is a basic applet named HelloWorldApplet.java − 

import java.applet.*; 

import java.awt.*; 

open class HelloWorldApplet broadens Applet { 

open void paint (Graphics g) { 

g.drawString ("Hello World", 25, 50); 



These import explanations carry the classes into the extent of our applet class − 

java.applet.Applet 

java.awt.Graphics 

Without those import proclamations, the Java compiler would not perceive the classes Applet and Graphics, which the applet class alludes to. 

The Applet Class 

Each applet is an augmentation of the java.applet.Applet class. The base Applet class gives techniques that an inferred Applet class may call to acquire data and administrations from the program setting. 

These incorporate techniques that do the accompanying − 

Get applet parameters 

Get the system area of the HTML document that contains the applet 

Get the system area of the applet class registry 

Print a status message in the program 

Get a picture 

Get a sound clasp 

Play a sound clasp 

Resize the applet 

Also, the Applet class gives an interface by which the watcher or program gets data about the applet and controls the applet's execution. The watcher may − 

Demand data about the creator, form, and copyright of the applet 

Demand a portrayal of the parameters the applet perceives 

Introduce the applet 

Wreck the applet 

Begin the applet's execution 

Stop the applet's execution 

The Applet class gives default executions of each of these strategies. Those usage might be abrogated as fundamental. 

The "Welcome, World" applet is finished the way things are. The main strategy abrogated is the paint technique. 

Conjuring an Applet 

An applet might be conjured by implanting mandates in a HTML document and review the record through an applet watcher or Java-empowered program. 

The <applet> tag is the reason for inserting an applet in a HTML record. Following is an illustration that summons the "Welcome, World" applet − 

<html> 

<title>The Hello, World Applet</title> 

<hr> 

<applet code = "HelloWorldApplet.class" width = "320" stature = "120"> 

On the off chance that your program was Java-empowered, a "Welcome, World" 

message would show up here. 

</applet> 

<hr> 

</html> 

Note − You can allude to HTML Applet Tag to see more about calling applet from HTML. 

The code quality of the <applet> tag is required. It determines the Applet class to run. Width and stature are additionally required to determine the underlying size of the board in which an applet runs. The applet order must be shut with a </applet> tag. 

In the event that an applet takes parameters, qualities might be passed for the parameters by including <param> labels amongst <applet> and </applet>. The program overlooks content and different labels between the applet labels. 

Non-Java-empowered programs don't process <applet> and </applet>. In this manner, anything that shows up between the labels, not identified with the applet, is unmistakable in non-Java-empowered programs. 

The watcher or program searches for the accumulated Java code at the area of the archive. To indicate something else, utilize the codebase trait of the <applet> tag as demonstrated − 

<applet codebase = "https://amrood.com/applets" code = "HelloWorldApplet.class" 

width = "320" stature = "120"> 

On the off chance that an applet lives in a bundle other than the default, the holding bundle must be determined in the code trait utilizing the period character (.) to isolate bundle/class parts. For instance − 

<applet = "mypackage.subpackage.TestApplet.class" 

width = "320" stature = "120"> 

Getting Applet Parameters 

The accompanying case shows how to influence an applet to react to setup parameters indicated in the report. This applet shows a checkerboard example of dark and a moment shading. 

The second shading and the measure of each square might be determined as parameters to the applet inside the archive. 

CheckerApplet gets its parameters in the init() technique. It might likewise get its parameters in the paint() strategy. In any case, getting the qualities and sparing the settings once toward the begin of the applet, rather than at each revive, is helpful and productive. 

The applet watcher or program calls the init() strategy for every applet it runs. The watcher calls init() once, quickly subsequent to stacking the applet. (Applet.init() is executed to do nothing.) Override the default usage to embed custom introduction code. 

The Applet.getParameter() technique brings a parameter given the parameter's name (the estimation of a parameter is dependably a string). On the off chance that the esteem is numeric or other non-character information, the string must be parsed. 

The accompanying is a skeleton of CheckerApplet.java − 

import java.applet.*; 

import java.awt.*; 

open class CheckerApplet broadens Applet { 

int squareSize = 50;/instated to default measure 

open void init() {} 

private void parseSquareSize (String param) {} 

private Color parseColor (String param) {} 

open void paint (Graphics g) {} 


Here are CheckerApplet's init() and private parseSquareSize() strategies − 

open void init () { 

String squareSizeParam = getParameter ("squareSize"); 

parseSquareSize (squareSizeParam); 

String colorParam = getParameter ("shading"); 

Shading fg = parseColor (colorParam); 

setBackground (Color.black); 

setForeground (fg); 


private void parseSquareSize (String param) { 

in the event that (param == invalid) return; 

attempt { 

squareSize = Integer.parseInt (param); 

} get (Exception e) { 

/Let default esteem remain 



The applet calls parseSquareSize() to parse the squareSize parameter. parseSquareSize() calls the library strategy Integer.parseInt(), which parses a string and returns a whole number. Integer.parseInt() tosses an exemption at whatever point its contention is invalid. 

In this manner, parseSquareSize() finds special cases, as opposed to enabling the applet to bomb on terrible info. 

The applet calls parseColor() to parse the shading parameter into a Color esteem. parseColor() completes a progression of string correlations with coordinate the parameter incentive to the name of a predefined shading. You have to actualize these techniques to influence this applet to work. 

Indicating Applet Parameters 

The accompanying is a case of a HTML record with a CheckerApplet implanted in it. The HTML document determines the two parameters to the applet by methods for the <param> tag. 

<html> 

<title>Checkerboard Applet</title> 

<hr> 

<applet code = "CheckerApplet.class" width = "480" tallness = "320"> 

<param name = "shading" esteem = "blue"> 

<param name = "squaresize" esteem = "30"> 

</applet> 

<hr> 

</html> 

Note − Parameter names are not case touchy. 

Application Conversion to Applets 

It is anything but difficult to change over a graphical Java application (that is, an application that uses the AWT and that you can begin with the Java program launcher) into an applet that you can insert in a site page. 

Following are the particular strides for changing over an application to an applet. 

Influence a HTML to page with the proper tag to stack the applet code. 

Supply a subclass of the JApplet class. Make this class open. Something else, the applet can't be stacked. 

Dispose of the primary technique in the application. Try not to develop an edge window for the application. Your application will be shown inside the program. 

Move any instatement code from the casing window constructor to the init technique for the applet. You don't have to unequivocally develop the applet question. The program instantiates it for you and calls the init strategy. 

Expel the call to setSize; for applets, measuring is finished with the width and stature parameters in the HTML document. 

Expel the call to setDefaultCloseOperation. An applet can't be shut; it ends when the program exits. 

On the off chance that the application calls setTitle, dispose of the call to the strategy. Applets can't have title bars. (You can, obviously, title the page itself, utilizing the HTML title tag.) 

Try not to call setVisible(true). The applet is shown naturally. 

Occasion Handling 

Applets acquire a gathering of occasion taking care of techniques from the Container class. The Container class characterizes a few strategies, for example, processKeyEvent and processMouseEvent, for taking care of specific sorts of occasions, and after that one catch-all strategy called processEvent. 

Keeping in mind the end goal to respond to an occasion, an applet must supersede the suitable occasion particular technique. 

import java.awt.event.MouseListener; 

import java.awt.event.MouseEvent; 

import java.applet.Applet; 

import java.awt.Graphics; 

open class ExampleEventHandling expands Applet executes MouseListener { 

StringBuffer strBuffer; 

open void init() { 

addMouseListener(this); 

strBuffer = new StringBuffer(); 

addItem("initializing the apple "); 


open void begin() { 

addItem("starting the applet "); 


open void stop() { 

addItem("st

Post a Comment

Thanks for comment.

Previous Post Next Post