What is Applet with Simple Example ?



An Applet is a java program that is transmitted over the network from server to the client and execute within browser of client. Applets are used to provide interactivity in web application.
It is a managed object. Managed object is an object that is created and managed by a run time environment.  Lifecycle methods are used to manage the lifecycle of a managed object.
Applets are managed object because their objects are created and managed by the Browser or another Java applet viewing program, such as the Applet Viewer.

Lifecycle Methods of Applet:
init(): executed only once when an object of the applet is created. This method is used for initialization purpose.
public void init();
start():represents the entry point of an apple, it is executed initially after init() and subsequently each time execution of the applet resume.
public void start();
stop(): executed each time execution of the applet is suspended.
public void stop();
paint():used to draw the contents of an applet. It is executed initially after start and subsequently each time repaint() is invoke or applet is redraw by the container.
public void paint(Graphics g);
destroy():executed when an applet object is unloaded form memory.
public void destroy();

Graphics: Object of graphics class represents the graphics environment of a GUI component. Each GUI component has a graphics object associated with it that determines contents of the GUI component.  Graphics Class provides various methods to draw components.
drawstring() : draw a string on a GUI component.
public void drawstring(String text, int left, int top)
Another methods are drawLine(), drawRect(), drawOval()

Limitation

An Applet has the following limitations:
1.            An applet can only initiate socket communication with the host form which it was            downloaded.
2.            An applet cannot access local file system on the client machine.
3.            An applet can only request basic information, such as name and version of OS, version of JRE     from the client machine.
4.            And applet does not have main method because its execution is predetermined by the life         cycle method.

<applet> tag is used to embed the applet within an html page.

<applet code="ClassName.class" width="350" height="350"></applet>

Simple Example:

1.            Create a Java Source File
package helloapplet;
import java.applet.Applet;
import java.awt.Graphics;

/**
 *
 * @author Navindra Jha
 */
public class MyApplet extends Applet {
     @Override
         public void paint(Graphics g) {
             g.drawString("Hello applet!", 50, 25);
         }
}

2.            Compile the Source File
                If the compilation succeeds, the compiler creates a file named MyApplet.class
3.            Create an HTML File that Includes the Applet
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
<APPLET code="MyApplet.class" width=150 height=25>
</APPLET>
</BODY>
</HTML>

4.            Run the Applet
To run the applet, you need to load the HTML file into an application that can run Java applets. This application might be a Java-compatible browser or another Java applet viewing program, such as the Applet Viewer.

How To Run Applet On Netbeans

1.            Right-click the HelloApplet project and choose Properties to open the Properties window.
2.            Right-click the HelloApplet project node in the Projects window and select New > Other               (Ctrl-N).
3.            Under Categories, select Java. Under File Types, select Applet.
4.            Under Class Name, type MyApplet. Under Package, type helloapplet
5.            Click Finish.
The IDE creates the applet source file in the specified package. The applet source file opens in the Source editor.
6.            Define your applet class like below
package helloapplet;
import java.applet.Applet;
import java.awt.Graphics;

/**
 *
 * @author Navindra Jha
 */
public class MyApplet extends Applet {
     @Override
         public void paint(Graphics g) {
             g.drawString("Hello applet!", 50, 25);
         }
}
7.            Right-click the MyApplet.java file node in the Projects window and choose Run File from the contextual menu. The applet is launched in the Applet Viewer.



No comments:

Post a Comment