Java Applets
• Applets in Java are small and dynamic internet-based programs.
• A Java Applet can be only executed within the applet framework of Java.
For an easy execution of applets, a restricted ‘sandbox’ is provided by the
applet framework. Generally, the applet code is embedded within
an HTML page. The applet codes are executed when the HTML pages get
loaded into the Java-compatible web browsers. Applets are mainly
downloaded on remote machines and used on the client side.
Java Applet vs. Java Application
Java Application Java Applet
Java Applications are the stand-alone Java Applets are small Java programs
programs which can be executed which are designed to exist within
independently HTML web document
Java Applications must have main() Java Applets do not need main() for
method for them to execute execution
Java Applets cannot run independently
Java Applications just needs the JRE
and require API’s
Java Applications do not need to Java Applets must extend
extend any class unless required java.applet.Applet class
Java Applications can execute codes
Java Applets Applications cannot do so
from the local system
Java Applications has access to all the Java Applets has access only to the
resources available in your system browser-specific services
Benefits of Applets
• As it operates on the client side, it requires much less response time.
• Any browser that has JVM operating in it can operate it.
• Secured
• It can be executed by browsers running under many plateforms,
including Linux, Windows, Mac Os etc.
Drawback of Applet
o Plugin is required at client browser to execute applet.
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
The life cycle of an applet is as shown in the figure below:
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4
life cycle methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only
once.
2. public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet
is stop or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only
once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
1. public void paint(Graphics g): is used to paint the Applet. It provides
Graphics class object that can be used for drawing oval, rectangle, arc
etc.
Simple example of Applet by applet viewer tool:
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g)
{
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java
import java.awt.*;
import java.applet.*;
/*
<applet code ="AppletSkeleton.class" width=300 height=100>
</applet>
*/
public class AppletSkeleton 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
}
}
Example of an Applet
import java.applet.*;
import java.awt.*;
/*
<applet code ="MyApplet.class" width=300 height=100>
</applet>
*/
public class MyApplet extends Applet
{
int height, width;
public void init()
{
height = getSize().height;
width = getSize().width;
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
}
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo1 extends Applet
{
public void paint(Graphics g)
g.setColor(Color.black);
g.drawString("Welcome to studytonight",50, 50);
g.setColor(Color.blue);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
g.drawLine(21,31,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
/*
<applet code =" GraphicsDemo1.class" width=300 height=100>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class Aimage extends Applet
{
Image img1;
public void init()
{
img1=getImage(getDocumentBase(),"icon.png");
}
public void paint(Graphics g)
{
g.drawImage(img1,100,100,this);
}
}
/*
<applet code ="Aimage.class" width=300 height=100>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class AnimationDemo extends Applet
{
Image p;
public void init()
{
p = getImage(getDocumentBase(),"ball.gif");
}
public void paint(Graphics g)
{
for(inti=0;i<500;i++)
{
g.drawImage(p, i,50, this);
try
{
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
/*
<applet code =" AnimationDemo.class" width=300 height=100>
</applet>
*/