Applet Programming
What is an Applet?
It is a small java program that is
accessed on an internet server,
transported over the internet ,
automatically installed and run as
part of a web document.
Applet
class
is
contained
in
java.applet package.
All applets are subclasses of Applet
class.
Some important points
Execution of an applet does not start
with a main method on rather applet
has its own life cycle..
Output in an applet is not displayed
using println method but using
graphic methods.
Applet is not executed using java
interpreter like a normal java
program. It is embedded in a web
page.
Applet output window
Applet class methods
void init() called first in beginning
void start()called to start execution
void paint(Graphics g) paints output
window
void stop() called to suspend execution
void destroy() called to terminate
applet
boolean isActive()true if applet started
Applet tag
<APPLET
[CODEBASE = codebaseURL]
CODE = file name
[ALT = aletrnate text]
[NAME = applet name]
WIDTH = w
HEIGHT = h
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
[<PARAM NAME = attribute name VALUE = value>]
___
</APPLET>
Example
import java.awt.*;
import java.applet.*;
/*
<applet code = Sample width = 200 height =
50>
</applet>
*/
public class Sample extends Applet {
String msg;
public void init()
{ setBackground(Color.green);
setForeground(Color.red);
msg = Inside init ; }
public void start()
{ msg += Inside start ;}
public void paint(Graphics g)
{
msg += Inside paint ;
g.drawString(msg, 10,30);
}
}
drawString is a method of Graphics class
setBackground and setForeground are in
Component class.
showStatus(String s) can be used to change
status shown in status window.
Color class constants
Color.gray
Color.blue
Color.black
Color.green
Color.red
Color.orange
Color.pink
Color.white
Color.yellow
Color.cyan
Color.magenta
Color.darkgray
Color.lightgray
Example
import java.awt.*;
import java.applet.*;
/*
<applet code = Shapes width = 200 height =
50>
</applet>
*/
public class Shapes extends Applet {
String msg;
public void init()
{ setBackground(Color.green);
setForeground(Color.red);
msg = ; }
public void paint(Graphics g)
{
g.drawLine(0,0, 100,150); //
g.drawRect(10,10,60,50); //
g.fillRect(100,10,60,50); //
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
//
}
}
Syntax of these methods
void drawLine(int startx,int starty,int endx,int
endy);
void drawRect(int top, int left, int width, int
height)
void fillRect(int top, int left, int width, int height)
void drawRoundRect(int top, int left, int width,
int height, int xdiameter, int ydiameter);
void fillRoundRect(int top, int left, int width, int
height, int xdiameter, int ydiameter);
if height and width are equal, it is a square.
Drawing ellipses and circles
void drawOval(int top, int left, int
width, int height)
void fillOval(int top, int left, int width,
int height)
if height and width are equal it is
circle, else an ellipse.
Drawing arcs and polygons
void drawArc(int top, int left, int width,
int height, int startangle, int endangle)
void fillArc(int top, int left, int width,
int height, int startangle, int endangle)
Drawing Polygons
void drawPolygon(int x[], int y[], int
num)
void fillPolygon(int x[], int y[], int
num)
where x and y are arrays of integers
and num is the number of vertices in
the polygon.
Passing Parameters
import java.awt.*;
import java.applet.*;
public class Parameters extends Applet
{
private String msg;
private int x;
private int y;
public void init()
{ setBackground(Color.green);
setForeground(Color.red);
msg = getParameter(message);
x = Integer.parseInt(getParameter(xposition);
y = Integer.parseInt(getParameter(yposition);
}
public void paint(Graphics g)
{
g.drawString(msg, x,y);
showstatus(Applet Parameters is running);
}
}
HTML file
<html>
<body>
<applet code = Parameters.class width = 200
height = 100 alt = Use Java enabled browser >
<param name = message value = Welcome to
Java />
<param name = xposition value = 50 />
<param name = yposition value = 60 />
</applet>
</body>
</html>
To display a moving banner
import java.awt.*;
import java.applet.*;
public class Banner extends Applet implements runnable
{
private String msg ;
Thread t = null;
int state;
boolean flag;
public void init()
{ setBackground(Color.green);
setForeground(Color.red);
msg = Welcome to Java Programming;
}
public void start()
{
t = new Thread(this);
flag = true;
t.start();
}
public void run()
{
char ch;
for( ; ; )
{ try
{ repaint();
Thread.sleep(300);
ch = msg.charAt(0);
msg = msg.substring(1,msg.length());
msg += ch;
if(!flag)
break;
}catch(InterruptedException e) { }
} // for loop ends
} // run method ends
public void stop()
{
flag = false;
t = null;
}
public void paint(Graphics g)
{
g.drawString(msg,50,50);
}
}
// repaint() method is defined in Graphics class
and gives a call to update() method of Applet
class which in turn calls paint()
getDocumentBase()
returns the directory path containing the
HTML file of the applet
getCodeBase()
returns the directory path containing the
applet class file of the applet
Example
import java.awt.*;
import java.applet.*;
import java.net.*;
public class Paths extends Applet
{
public void paint(Graphics g)
{
String msg;
URL url = getCodeBase();
msg = HTML file path + url.toString();
g.drawString(msg, 50,50);
URL url = getDocumentBase();
msg = Applet file path + url.toString();
g.drawString(msg, 50,50);
}
}