Skip to main content

What is Applet ?

***What is an Applet?


An Applet is a program that can be
referenced by an HTML source code of a
web page.


It is dynamically downloaded from a web
server to a browser and it is executed in the
environment provided by the browser.



  • Applet Viewer can also be used to execute  the applet.


***FirstApplet.java

import java.applet.Applet;
import java.awt.Graphics;
/*<applet code=“FirstApplet” width=200 height=200>
</applet>*/
public class FirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello World”,20,100);
}
}


*** In the above example,

The first two line is importing the java.applet and
java.awt.Graphics package.

The third line is the comment entry which is used only for
applet viewer to execute the applet using the HTML source
code.

The next line declares the FirstApplet class which extends
Applet class. Note that each applet created by user must
extend the Applet class.

There is a paint() method defined which accepts the object of
Graphics class. This method is invoked automatically
whenever the applet is displayed.

The drawString() method is defined in the class Graphics and
it takes three arguments, message and x and y co-ordinates.
Appletviewer is used to execute the applet.



***LIFE CYCLE OF APPLET

Normally, any program starts execution
from the main() method of a class.

An apple executes in an environment
provided by the browser or an appletviewer.

There are total four methods that are called
during the life cycle of an applet. They are:
init(), start(), stop() and destroy().

All these methods are defined in the
java.applet.Applet class.

The init() method is called when the applet
begins execution. The code which is to be
executed only once is placed in this method.


The start() method is executed after the
init() method completes execution. This
method is also called when an appletviewer
or the browser resumes the execution of the
applet.

The stop() method is called by the applet
viewer or web browser to suspend
execution of an applet. Therefore, the
start() and stop() methods may be called
multiple time during the life cycle of the
applet.

The stop() method is also called when the
browser calls another web page.

Finally, the destroy() method is called by the
applet viewer or web browser before the
applet is terminated.

The stop() method is invoked before the
destroy() method.



EXAMPLE :

import java.applet.Applet;
import java.awt.Graphics;
/*<applet code="AppletLifeCycle" width=200
height=200></applet>*/

public class AppletLifeCycle extends Applet
{
String str="";
public void init()
{
str+="Init:";
}
public void start()
{
str+="Start:";
}
public void stop()
{
str+="Stop";
}
public void destroy()
{
System.out.println("Destroy");
}
public void paint(Graphics g)
{
g.drawString(str,20,20);
}
}


***GRAPHICS CLASS
A graphics object encapsulates a set of
methods that can perform graphics output.


It allows to draw lines, ovals, rectangles,
strings, images, characters and arcs.




Comments

Popular posts from this blog

What is Servet ?

A servlet is a java class that extends an application hosted on a web server. Handles the HTTP request-response process (for our purposes) Often thought of as an applet that runs on a server. Provides a component base architecture for web development, using the Java Platform The foundation for Java Server Pages (JSP). Alternative to CGI scripting and platform specific server side applications.

Panels in AWT

Panels: import java.applet.*; import java.awt.*; /*<applet code="PanelDemo" width=300 height=300></applet>*/ public class PanelDemo extends Applet { public void  init() { setLayout(new BorderLayout()); Panel pn=new Panel(); Button b1=new Button("Button 1"); pn.add(b1); Button b2=new Button("Button 2"); pn.add(b2); add(pn,"North"); Panel pe=new Panel(); Button b3=new Button("Button 3"); pe.add(b3); add(pe,"East"); Panel pw=new Panel(); Button b4=new Button("Button 4"); pw.add(b4); add(pw,"West"); Panel ps=new Panel(); Button b5=new Button("Button 5"); ps.add(b5); Button b6=new Button("Button 6"); ps.add(b6); add(ps,"South"); } }