Java Applet
What
is a Java applet?
A
Java applet is a small application which is written in Java and delivered to
users in the form of bytecode. The user launches the Java applet from a web
page, and the applet is then executed within a Java Virtual Machine (JVM)
in a process separate from the web browser itself.
What
is the difference between application and applet?
An applet is
a program written in the Java programming language that can be
included in an HTML page, much in the same way an image is included. An application is
a standalone Java program that runs as a true application,
outside of a browser. Both require a JVM (Java Virtual Machine).
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 platforms, including Linux,
Windows, Mac OS etc.
Drawback of Applet
- Plugin
is required at client browser to execute applet.
Hierarchy
of Applet
As
displayed in the above diagram, Applet class extends Panel. Panel class extends
Container which is the subclass of Component.
Lifecycle of Java Applet
- Applet
is initialized.
- Applet
is started.
- Applet
is painted.
- Applet
is stopped.
- Applet
is destroyed.
How to run an Applet?
There
are two ways to run an applet
1.
By
html file.
2.
By
AppletViewer tool (for testing purpose).
Examples
of Applet Program
AppletExample.java
/*
Basic Java Applet Example
This Java example shows how to create a
basic applet using Java Applet class.
*/
import
java.applet.Applet;
import
java.awt.Graphics;
/*
<applet code =
"AppletExample" width = 200 height = 200>
</applet>
*/
public class
AppletExample extends Applet{
public void paint(Graphics g){
//write text using drawString
method of Graphics class
g.drawString("Shree
Matrumandir College, Rajkot",20,100);
}
}
AppletImage.java
import
java.awt.*;
import
java.applet.*;
/*
<applet
code="AppletImage" width=300 height=300>
</applet>
*/
public class AppletImage extends Applet
{
Image pic;
public void init()
{
pic =
getImage(getDocumentBase(),"Gujarat.jpg");
}
public void paint(Graphics g)
{
g.drawImage(pic, 30,30, this);
}
}
AppletColors.java
/*
Print INDIA in an Applet in different
Colors Example
*/
import
java.applet.Applet;
import
java.awt.Graphics;
import
java.awt.Color;
/*
<applet code =
"AppletColors" width = 300 height = 300>
</applet>
*/
public class
AppletColors extends Applet{
public void paint(Graphics g){
Color c[] = { Color.blue,
Color.cyan, Color.darkGray,
Color.gray, Color.green, Color.lightGray,
Color.magenta, Color.orange, Color.pink,
Color.red,
Color.white, Color.yellow };
//this
will create light blue color
Color customColor = new
Color(10,10,255);
//set background color of an
applet
this.setBackground(customColor);
for(int i = 0; i<c.length; i++){
g.setColor(c[i]);
g.drawString(" I N
D I A ", 20, 20 + (i*25));
}
}
}
AppletAnimation.java
import
java.awt.*;
import
java.applet.*;
public class
AppletAnimation extends Applet
{
Image pic;
public void init()
{
pic=getImage(getDocumentBase(),"smiley.jpg");
}
public void paint(Graphics grp)
{
for(int i=50;i<100;i++)
{
grp.drawImage(pic, i,50,this);
try
{
Thread.sleep(300);
}
catch(Exception e){}
}
}
}
/*
<applet
code="AppletAnimation.class" width="400"
height="400">
</applet
*/
AppletGraphics1.java
import
java.awt.*;
import
java.applet.*;
/*
<applet code="AppletGraphics1"
width=300 height=300>
</applet>
*/
public class
AppletGraphics1 extends Applet
{
public void paint(Graphics g)
{
for(int
i=0;i<=250;i++)
{
Color c1=new
Color(35-i,55-i,110-i);
g.setColor(c1);
g.drawRect(250+i,250+i,100+i,100+i);
g.drawOval(100+i,100+i,50+i,50+i);
g.drawLine(50+i,20+i,10+i,10+i);
}
}
}
AppletKey.java
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/*
<applet
code="AppletKey" width=300 height=400>
</applet>
*/
public class
AppletKey extends Applet
implements
KeyListener
{
int X=20,Y=30;
String msg="Key
Events--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.blue);
setForeground(Color.red);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case
KeyEvent.VK_UP:
showStatus("Key
Up");
break;
case
KeyEvent.VK_DOWN:
showStatus("Key
Down");
break;
case
KeyEvent.VK_LEFT:
showStatus("Key
Left");
break;
case
KeyEvent.VK_RIGHT:
showStatus("Key
Right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key
Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
Comments
Post a Comment