11 Applet
11 Applet
1
Java Programming | CCIT
Table of Contents
Applets .................................................................. 3
Applet Tag ........................................................... 3
Life cycle methods for Applet .............................. 4
Class Applet ......................................................... 5
Class Applet Hierarchy ......................................... 6
AppletViewer ....................................................... 8
Class Graphics ...................................................... 9
class Color .......................................................... 14
RGB Color Modal ............................................... 20
HSB Color Modal ............................................... 20
2
Java Programming | CCIT
Applets
A Java applet is a specially designed Java class which can be used within web
pages to enhance them.
A browser enabled with Java technology can download the applet from the
internet and run it.
Applets have strict security rules that are enforced by the Web browser.
Applet Tag
Applets can be added into web pages by using applet tag.
Example:-
<APPLET code=className width=w height=h alt="alternate Text" >
<PARAM name=pname value=val >
-----
</APPLET>
where
Code - A URL of applet class.
Height - Height to display the applet.
width - Width to display the applet.
al - Alternate text to be displayed in case browser does not support applet.
Param tag is used to pass additional parameters to applet
3
Java Programming | CCIT
5
Java Programming | CCIT
6
Java Programming | CCIT
Example:-
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
String s="";
public void destroy()
{
s=s+"destroy ";
showStatus(s);
}
public void paint(Graphics g)
{
s=s+"paint ";
showStatus(s);
}
public void stop()
{
s=s+"stop ";
showStatus(s);
}
public void start()
{
s=s+"start ";
showStatus(s);
}
public void init()
{
s=s+"init ";
showStatus(s);
}
}
//<applet code=MyApplet width=300 height=300 ></applet> 7
Java Programming | CCIT
NOTE: -
Applet is an old technology.
In old days they were supported by browsers.
Now browsers have stopped supporting Applets.
But still we can test our Applet by using a tool AppletViewer.
This tool was available till jdk1.8.
AppletViewer
It is a tool generally used by developers for testing their applets before deploying
them to a website.
Syntax :
appletviewer filename
It loads the applet class specified in applet tag of the file and calls applet different
life cycle methods of applet on different events.
8
Java Programming | CCIT
9
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawOval(0,0,200,200);
g.drawOval(50,50,100,100);
}
}
10
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<10;i++)
g.drawOval(i*10,i*10,200-i*20,200-i*20);
}
}
11
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.fillRect(5,5,90,90);
g.fillRect(105,5,90,90);
g.fillRect(5,105,90,90);
g.fillRect(105,105,90,90);
}
}
12
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawOval(0,0,200,200);
g.drawArc(30,30,140,140,180,180);
g.fillOval(50,50,30,30);
g.fillOval(120,50,30,30);
}
}
13
Java Programming | CCIT
14
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{ public void paint(Graphics g)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.fillOval(i*10,i*10,200-i*20,200-i*20);
}
}
}
15
//<applet code=MyApplet width=200 height=200 ></applet>
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(5,5,90,90);
g.setColor(Color.green);
g.fillRect(105,5,90,90);
g.setColor(Color.blue);
g.fillRect(5,105,90,90);
g.setColor(Color.yellow);
g.fillRect(105,105,90,90);
}
}
//<applet code=MyApplet width=200 height=200 ></applet>
16
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
int xp[]={100,200,0};
int yp[]={0,170,170};
g.setColor(Color.blue);
g.fillPolygon(xp,yp,3);
g.setColor(Color.orange);
g.fillOval(75,75,50,50);
g.setColor(Color.red);
g.drawString("CCIT Amravati",65,190);
}
}
17
Java Programming | CCIT
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
Color co[]={Color.red,Color.blue,Color.green,
Color.magenta,Color.cyan,Color.yellow };
for(int i=0;i<12;i++)
{
g.setColor(co[i%6]);
g.fillArc(0,0,200,200,i*30,30);
}
}
}
18
Java Programming | CCIT
Design an Applet of size 256 X 200 containing 256 lines of different colors from
red to blue.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
for(int x=0;x<256;x++)
{
Color clr=new Color(255-x,0,x);
g.setColor(clr);
g.drawLine(x,0,x,200);
}
}
}
19
Java Programming | CCIT
Hue
o It is number between 0.0 and 1.0 which indicates the hue of the color.
Saturation
o It is number between 0.0 and 1.0 which indicates color depth.
o 1 will make the color as deep as possible.
o 0 will take all the color out of the mixture and make it a shade of gray.
Brightness
o Is is decimal number between 0.0 and 1.0 which indicates brightness.
o 1 will make the color as light as possible.
o 0 will make it very dark.
20
Java Programming | CCIT
21