Applet in Java
An applet is a special kind of Java program that runs in a Java
enabled browser. This is the first Java program that can run over the network using
the browser. Applet is typically embedded inside a web page and runs in the
browser.
In other words, we can say that Applets are small Java
applications that can be accessed on an Internet server, transported over Internet,
and can be automatically installed and run as apart of a web document.
After a user receives an applet, the applet can produce a
graphical user interface. It has limited access to resources so that it can run
complex computations without introducing the risk of viruses or breaching data
integrity.
To create an applet, a class must class extends
java.applet.Applet class. An Applet class does not have any main() method. It is
viewed using JVM. The JVM can use either a plug-in of the Web browser or a
separate runtime environment to run an applet application.
Advantage of Applet
There are many advantages of applet. They are as follows:
It works at client side so less response time.
Secured
It can be executed by browsers running under many plateforms, including
Linux, Windows, Mac Os etc.
Drawback of Applet
Plugin is required at client browser to execute applet.
How to run an Applet?
There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).
Simple example of Applet by html file:
To execute the applet by html file, create an applet and compile it. After that create
an html file and place the applet code in html file. Now click the html file .
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome",150,150);
}
}
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
Running Applet using Applet Viewer
To execute an Applet with an applet viewer, write short HTML file as discussed
above. If you name it as run.htm, then the following command will run your applet
program.
f:/>appletviewer myapplet.html
Graphics in Applet
In Applet, java.awt.Graphicsclass provides methods for using graphics.
Below are the Methods of the Graphics class.
Sr Methods Description
No.
public abstract void drawString(String str, int x,
1 Used to draw specified string.
int y)
public void drawRect(int x, int y, int width, int Used to draw a rectangle of
2
height) specified width and height.
Used to draw a rectangle with a
public abstract void fillRect(int x, int y, int width,
3 default colourof specified width and
int height)
height.
public abstract void drawOval(int x, int y, int Used to draw oval of specified width
4
width, int height) and height.
public abstract void fillOval(int x, int y, int width, Used to draw oval with a default
5
int height) colour of specified width and height.
public abstract void drawLine(int x1, int y1, int Used for drawing lines between the
6
x2, int y2) point (x1, x1) and (x2, y2).
public abstract booleandrawImage(Image img, int
7 Used for drawing a specified image.
x, int y, ImageObserver observer)
public abstract void drawArc(int x, int y, int width,
8 Used for drawing a circular arc.
int height, intstartAngle, intarcAngle)
10 public abstract void setColor(Color c) Used to set a colour to the object.
11 public abstract void setFont(Font font) Used to set font.
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Most applets override these four methods. These four methods forms Applet lifecycle.
init() : init() is the first method to be called. This is where variable are initialized. This
method is called only once during the runtime of applet.
start() : start() method is called after init(). This method is called to restart an applet after
it has been stopped.
stop() : stop() method is called to suspend thread that does not need to run when applet is
not visible.
destroy() : destroy() method is called when your applet needs to be removed completely
from memory.
Note: The stop() method is always called before destroy() method
Example of an Applet Life Cycle
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet
{
public void init()
{
//initialization
}
public void start ()
{
//start or resume execution
}
public void stop()
{
//suspend execution
{
public void destroy()
{
//perform shutdown activity
}
public void paint (Graphics g)
{
//display the content of window
}
}