Applet & Event Handling PDF
Applet & Event Handling PDF
Application
??
Applets are restricted from using libraries Applications can import C or C++
from other languages such as C or C++.
programs using JNI
Applet have their own life cycle.
Dynamic display
Flash outputs
3.
4.
5.
6.
7.
States:
Born
Begin
start()
stop()
Running
idle
start()
Destroyed
stopped
destroy()
dead
End
Initialization state
Running state
Applet enters the running state when the system calls the
start() method of Applet class. This occurs automatically
after the applet is initialized. Starting can also occurs if
applet is already in stopped state. Start method may be
called more than once.
public void start()
{
------------------}
Dead state
Display state
Example 1
import java.awt.*;
import java.applet.*;
public class AppletEx extends Applet
{
public void paint(Graphics g)
{
g.drawString("Applet Example", 20,20);
}
}
<HTML>
<HEAD><title>applet example</title>
</HEAD>
<body>
<APPLET
CODE=AppletEx.class
WIDTH=200
HEIGHT=200></APPLET>
</body>
</html>
Appletviewer AppletEx.html
Color.green
Color.blue
Color.lightGray
Color.darkGray
Color.magenta
Color.gray
Color.pink
Color.red
Color.yellow
Color.white
import java.awt.*;
import java.applet.*;
public class Sample extends Applet{
String msg;
public void init()
{setBackground(Color.gray);
setForegorund(Color.red);
msg="Inside init()--";
}
public void start()
{
msg+="Inside start()--";
}
public void paint(Graphics g){
msg+="Inisde paint()--";
g.drawString(msg,10,10);
}
}
Example 2
Example 3
Status window:
By default: Applet started
import java.awt.*;
import java.applet.*;
public class Status extends Applet{
public void init()
{setBackground(Color.gray);
}
public void paint(Graphics g){
g.drawString("This is in the applet window.",10,20);
showStatus("Applet status window");
}
}
getParameter() method
import java.awt.*;
import java.applet.*;
public class Example extends Applet{
String str;
public void init()
{
str=getParameter("string");
if(str==null)
str="Java";
str="Hello" +str;
}
public void paint(Graphics g){
g.drawString(str,10,100);}
Example 4
<HTML><HEAD>
<TITLE>JAVA APPLETS</TITLE>
</HEAD>
<BODY>
<APPLET CODE=Example.class WIDTH=400
HEIGHT=200>
<PARAM NAME="string" VALUE="Applet!">
</APPLET>
</BODY>
</HTML>
import java.awt.*;
import java.applet.*;
public class Example extends Applet{
public void paint(Graphics g)
{
int value1=10;
int value2=20;
int sum=value1+value2;
String s="SUM=" + String.valueOf(sum);
g.drawString(s,100,100);
}
}
Example 5
Example 6
import java.awt.*;
import java.applet.*;
public class paramex extends Applet{
String author;
int ver;
public void start()
{
String temp;
author=getParameter("author");
if(author==null)author="not found";
temp=getParameter("version");
try{
if(temp!=null)
ver=Integer.parseInt(temp);
else
ver=0;
}catch(NumberFormatException exc)
{
ver=-1;
}
}
public void pain(Graphics g)
{
g.drawString("By + author,10,40);
g.drawString("version" + ver, 10,60);
}
}
Example 7
import java.awt.*;
import java.applet.*;
public class Example extends Applet{
TextField text1,text2;
public void init(){
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);
text1.setText(10");
text2.setText(10");
}
Program
Event Handling
Event Handling
Event Handling
Events
Event source
Event listeners
Events
Event source
Event source
Event Listener
Event source