INTRODUCTION TO APPLET :
There are two kinds of Java programs, applications (also called stand-alone programs) and Applets. An Applet is a small Internet-based program that has the Graphical User Interface (GUI), written in the Java programming language.
Applets are designed to run inside a web browser or in applet viewer to facilitate the user to animate the graphics, play sound, and design the GUI components such as text box, button, and radio button. When applet arrives on the client, it has limited access to resources, so that it can produce arbitary multimedia user interface and run complex computation without introducing the risk of viruses or breaching data integrity.
To create an applet, we extend the “java.applet.Applet” class And by overriding the methods of java.awt.Applet, new functionality can be placed into web pages.
Applets are compiled using javac compiler and it can be executed by using an appletviewer or by embedding the class file in the HTML (Hyper Text Markup Languege) file.
The java.applet package is the smallest package in Java API(Application Programming Interface). The Applet class is the only class in the package. The Applet class has many methods that are used to display images, play audio files etc but it has no main() method. Some of them were explained below that give you the knowledge about Applets and their behavior.
init() : This method is used for whatever initializations are needed for your applet. Applets can have a default constructor, but it is better to perform all initializations in the init method instead of the default constructor.
start() : This method is automatically called after Java calls the init method. If this method is overwritten, code that needs to be executed every time that the user visits the browser page that contains this applet.
stop() : This method is automatically called when the user moves off the page where the applet sits. If your applet doesn't perform animation, play audio files, or perform calculations in a thread, you don't usually need to use this method.
destroy() : Java calls this method when the browser shuts down.
Following are the advantages of a Java Applet :
Every java Applet inherits a set of default behaviours from the Applet class. As a result, when an applet is loaded it undergoes a series of changes in its state. Following are the states in applets lifecycle.
1) Born or Initialisation state :
An applet begins its life when the web browser loads its classes and calls its init() method. This method is called exactly once in Applets lifecycle and is used to read applet parameters. Thus, in the init() method one should provide initialization code such as the initialization of variables.
//Syntex Eg. public void init() { //initialisation }
2) Running State :
Once the initialization is complete, the web browser will call the start() method in the applet. This method must called atleat once in the Applets lifecycle as the start() method can also be called if the Applet is in “Stoped” state. At this point the user can begin interacting with the applet.
//Syntex Eg. public void start() { //Code }
3) Stopped State :
The web browser will call the Applets stop() method, if the user moved to another web page while the applet was executing. So that the applet can take a breather while the user goes off and explores the web some more. The stop() method is called atleast once in Applets Lifecycle.
//Syntex Eg. publc void stop() { //Code }
4) Dead State :
Finally, if the user decides to quit the web browser, the web browser will free up system resources by killing the applet before it closes. To do so, it will call the applets destroy() method. One can override destroy() to perform one-time tasks upon program completion. for example, cleaning up threads which were started in the init() method.
//Syntex Eg. public void destroy() { // Code }
Note : If the user returns to the applet, the web browser will simply call the applet's start() method again and the user will be back into the program.
5) Display State :
Applet moves to the display state whenever it has to perform the output operations on the screen. This happens immediately after the applet enters into the running state. The paint() method is called to accomplish this task.
//Syntex Eg. public void paint(Graphics g) { //Display Statements }
One can show Lifecycle of an Applet Graphically as follows :
The following example is made simple enough to illustrate the essential use of Java applets through its java.applet package.
//Example. import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("My First Applet",40,40); } }
Here is the illustration of the above example,
There are two ways in which one can run an applet, as follows
To execute an applet in a web browser, you need to write a short HTML text file that contains the appropriate APPLET tag.
For above example it is
//HTML CODE <html> <body> <applet code="SimpleApplet.class" width=200 height=100> </applet> </body> </html>
The output of above example appears as shown in the following figure :
Insted of creating different text file for html code one can write above program as follows
import java.awt.*; import java.applet.*; /* <applet code="SimpleApplet" width=200 height=100> </applet> */ public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("My First Applet",40,40); } }
The output remains same.
The Applet tag is used to start an applet from both HTML document and form applet viewer.
An applet viewer will execute each Applet tag that it finds in a separate window, while web browsers like Netscape Navigator, Internet Explorer and HotJava will allow many applets in a single page.
The <applet....> tag included in the body section of HTML file supplies the name of the applet to be loaded and tells the browser how much space the applet ruquires
The synatax for the standard Applet tag is as follows
<applet[codebase=codebaseURL] code=”Applet file” [ALT=”alternative text] [name=AppletInstanceName] Width=pixels height= pixels [align= alignment] > [<param name=”Attributename” value =”Attribute value”] [<param name=”Attributename” value =”Attribute value”] ........ [HTML displayed in the absence of java] </applet>
Here is meaning of each peice of above code
One can supply user-defined parameters to an applet using <param.....> tag. Each <param....> tag has a name attribute such as color,and a value attribute such as red. Inside the applet code, the applet can refer to that parameter by name to find its value. For e.g. the color of the text can be changed to red by an applet using a <param...> tag as follows
//HTML CODE <applet....> <param=color value = “red”> </applet>
Similarly we can change the text to be displayed by an applet by supplying new text to the applet through a <param.....>tag as shown below.
<param name=text value = “xyz” >
Passing a parameters to an applet is similar to passing parameters to main() method using command line arguments. To set up and handle parameters, we need to do two things.
Parameters are passed to an applet when it is loaded. We can define the init() method in the applet to get hold of the parameters defined in the <param> tags. This is done using the getparameter() method, which takes one string argument representing the name of the parameter and returns a string containing the value of that parameter.
All Rights Reserved. © 2024 BookOfNetwork