Applet Programming
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.
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 ; }
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 = ; }
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;
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>
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);
}
}