Pages

Applets JAVA


An applet is a program that comes from server into a client and gets executed at client side and displays the result. An applet represents byte code embedded in a html page. (applet = bytecode + html) and run with the help of Java enabled browsers such as Internet Explorer. An applet is a Java program that runs in a browser. Unlike Java applications applets do not have a main () method. To create applet we can use java.applet.Applet or javax.swing.JApplet class. All applets inherit the super class ‘Applet’. An Applet class contains several methods that helps to control the execution of an applet.

Advantages: 
· Applets provide dynamic nature for a webpage.
· Applets are used in developing games and animations.

Creating an applet:   
· Let the Applet class extends Applet or JApplet class.
· Overide the following methods of Applet class. o public void init (): This method is used for initializing variables, parameters to create components. This method is executed only once at the time of applet loaded into memory.
o   public void start (): After init() method is executed, the start method is executed automatically. Start method is executed as long as applet gains focus. In this method code related to opening files and connecting to database and retrieving the data and processing the data is written.
o   public void stop (): This mehtod is executed when the applet loses focus. Code related to closing the files and database, stopping threads and performing clean up operations are written in this stop method.
o   public void destroy (): This method is exexuted only once when the applet is terminated from the memory.
Executing above methods in that sequence is called applet life cycle. We can also use public void paint (Graphics g) in applets.
After writing an applet, an applet is compiled in the same way as Java application but running of an applet is different. There are two ways to run an applet.
· Executing an applet within a Java compatible web browser.
· Executing an applet using ‘appletviewer’. This executes the applet in a window.
To execute an applet using web browser, we must write a small HTML file which contains the appropriate ‘APPLET’ tag. <APPLET> tag is useful to embed an applet into an HTML page. It has the following form:
<APPLET CODE=”name of the applet class file” CODEBASE=”path of the applet class file” HEIGHT = maximum height of applet in pixels WIDTH = maximum width of applet in pixels ALIGN = alignment (LEFT, RIGHT, MIDDLE, TOP, BOTTOM) 
ALT = alternate text to be displayed>
<PARAM NAME = parameter name VALUE = its value>
</APPLET>

The <PARAM> tag useful to define a variable (parameter) and its value inside the HTML page which can be passed to the applet. The applet can access the parameter value using getParameter () method, as:
                        String value = getParameter (“pname”);
Where pname is the parameter name and its value is retrieved.
The HTML file must be saved with .html extension. After creating this file, open the Java compatible browser (Internet Explorer) and then load this file by specifying the complete path, then Applet program will get executed.
In order to execute applet program with an applet viewer, simply include a comment at the head of Java Source code file that contains the ‘APPLET’ tag.Thus, our code is documented with a prototype of the necessary HTML statements and we can test out compiled applet by starting the appletviewer with the Java file as:  appletviewer programname.java  

Program 1: Write an applet program with a message and display the message in paint () method.
/*         <applet code="MyApplet.class" width = 600 height= 450>
</applet>          */
import java.applet.Applet; import java.awt.*;
public class MyApplet extends Applet
{           String msg="";
            public void init()
            {
                        msg += "init";
            }
            public void start()
            {
                        msg +=" start";
            }
            public void paint(Graphics g)
            {
                        g.drawString(msg,10,100);
            }
            public void stop()
            {
                        msg += " stop";
            }
            public void destroy()
            {
                        msg+= " destroy";
            }
}
Output:



Program 2: Write a program to move an image from left to right in an Applet. To load an image use Image class of java.awt.
/*         <applet code="MyApplet1.class" width = 600 height= 450>
</applet>          */          
import java.applet.*; import java.awt.*; public class MyApplet1 extends Applet
{           public void paint (Graphics g)
            {          Image i = getImage (getDocumentBase (),"plane.gif");
                        for (int x= 0 ; x<=800 ; x++)                {             g.drawImage (i, x, 0, null);
                                    try
                                    {          Thread.sleep (20);
                                    }
                                    catch(InterruptedException ie) {          }
                        }
            }           
}                                              

Output:



Program 3: Write a program to pass employ name and id number to an applet.
/*         <applet code="MyApplet2.class" width = 600 height= 450>
<param name = "t1" value="Hari Prasad">
<param name = "t2" value ="101">
</applet>          */
import java.applet.*; import java.awt.*;
public class MyApplet2 extends Applet
{           String n;           String id;              public void init()
            {          n = getParameter("t1");
                        id = getParameter("t2");
            }
            public void paint(Graphics g)
            {          g.drawString("Name is : " +  n, 100,100);                    g.drawString("Id is : "+ id, 100,150);
            }           


Output: