Event Handling
Applet - Basics; Applet architecture; Applet UNIT 4: Applets & life cycle; Applet
display methods; Repaint; Status window; Passing parameters to applets;
Getdocumentbase(); Getcodebase(); Applet context and showdocument().
Event Handling – Event handling mechanisms; Delegation event model; Event
classes; Sources of events; Event listener interfaces; Handling mouse and
keyboard events; Adapter classes; Inner classes.
Unit 4
Java Applet
Applet is a special type of program that is
embedded in the webpage to generate the
dynamic content.
It runs inside the browser and works at client
side.
Advantages of Applet
It works at client side so less response time.
Secured
It can be executed by browsers running under
many plateforms, including Linux, Windows,
Mac Os etc.
• Drawback of Applet
Plugin is required at client browser to execute
applet.
Lifecycle of Java Applet
Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
init()
start()
stop()
destroy()
paint
do other work
Lifecycle methods for Applet:
 java.applet.Applet class
• For creating any applet java.applet.Applet class must
be inherited. It provides 4 life cycle methods of applet.
 public void init(): is used to initialized the Applet. It is
invoked only once.
 public void start(): is invoked after the init() method or
browser is maximized. It is used to start the Applet.
 public void stop(): is used to stop the Applet. It is
invoked when Applet is stop or browser is minimized.
 public void destroy(): is used to destroy the Applet. It
is invoked only once.
• Each applet has four major events in
its lifetime:
–Initialization --- init()
–Starting --- start()
–Painting --- paint(Graphics)
–Stopping --- stop()
–Destroying --- destroy()
java.awt.Component class
The Component class provides 1 life cycle
method of applet.
public void paint(Graphics g): is used to paint
the Applet. It provides Graphics class object
that can be used for drawing oval, rectangle,
arc etc.
How to run an Applet?
• There are two ways to run an applet
By html file.
By appletViewer tool
Example of Applet by html file:
To execute the applet by html file:
1. Create an applet and compile it.
2. Create an html file and place the applet code
in html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}
//myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="
300">
</applet>
</body>
</html>
Example of Applet by appletviewer
tool
To execute the applet by appletviewer tool
1.Create an applet that contains applet tag in
comment and compile it.
2. After that run it by: appletviewer First.java
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/ c:>javac First.java
c:>appletviewer First.java
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
<html>
<body>
<applet code="GraphicsDemo.class"
width="300" height="300">
</applet>
</body>
</html>
Passing Parameters to Applet
• Supply user-defined parameters to an applet
using <PARAM> tags.
<applet>
<param name=text value=“Applet>
</applet>
import java.awt.*;
import java.applet.*;
public class HelloParam 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,10);
}
<html>
<body>
<applet code=“HelloParam.class”
width=400 height=200>
<param name=“string”
value=“Applet!”>
</param>
</body>
</html>
• Write a applet program to pass name and age
of a person and print it .
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
*/
Output

Java Applets

  • 1.
    Event Handling Applet -Basics; Applet architecture; Applet UNIT 4: Applets & life cycle; Applet display methods; Repaint; Status window; Passing parameters to applets; Getdocumentbase(); Getcodebase(); Applet context and showdocument(). Event Handling – Event handling mechanisms; Delegation event model; Event classes; Sources of events; Event listener interfaces; Handling mouse and keyboard events; Adapter classes; Inner classes. Unit 4
  • 2.
    Java Applet Applet isa special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.
  • 3.
    Advantages of Applet Itworks at client side so less response time. Secured It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc. • Drawback of Applet Plugin is required at client browser to execute applet.
  • 4.
    Lifecycle of JavaApplet Applet is initialized. Applet is started. Applet is painted. Applet is stopped. Applet is destroyed.
  • 5.
  • 6.
    Lifecycle methods forApplet:  java.applet.Applet class • For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.  public void init(): is used to initialized the Applet. It is invoked only once.  public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.  public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.  public void destroy(): is used to destroy the Applet. It is invoked only once.
  • 7.
    • Each applethas four major events in its lifetime: –Initialization --- init() –Starting --- start() –Painting --- paint(Graphics) –Stopping --- stop() –Destroying --- destroy()
  • 8.
    java.awt.Component class The Componentclass provides 1 life cycle method of applet. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.
  • 9.
    How to runan Applet? • There are two ways to run an applet By html file. By appletViewer tool
  • 10.
    Example of Appletby html file: To execute the applet by html file: 1. Create an applet and compile it. 2. Create an html file and place the applet code in html file.
  • 11.
    //First.java import java.applet.Applet; import java.awt.Graphics; publicclass First extends Applet{ public void paint(Graphics g){ g.drawString("welcome",150,150); } }
  • 12.
  • 13.
    Example of Appletby appletviewer tool To execute the applet by appletviewer tool 1.Create an applet that contains applet tag in comment and compile it. 2. After that run it by: appletviewer First.java
  • 14.
    //First.java import java.applet.Applet; import java.awt.Graphics; publicclass First extends Applet{ public void paint(Graphics g){ g.drawString("welcome to applet",150,150); } } /* <applet code="First.class" width="300" height="300"> </applet> */ c:>javac First.java c:>appletviewer First.java
  • 15.
    import java.applet.Applet; import java.awt.*; publicclass GraphicsDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); } } <html> <body> <applet code="GraphicsDemo.class" width="300" height="300"> </applet> </body> </html>
  • 16.
    Passing Parameters toApplet • Supply user-defined parameters to an applet using <PARAM> tags. <applet> <param name=text value=“Applet> </applet>
  • 17.
    import java.awt.*; import java.applet.*; publicclass HelloParam 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,10); } <html> <body> <applet code=“HelloParam.class” width=400 height=200> <param name=“string” value=“Applet!”> </param> </body> </html>
  • 18.
    • Write aapplet program to pass name and age of a person and print it .
  • 19.
    import java.awt.*; import java.applet.*; publicclass MyApplet extends Applet { String n; String a; public void init() { n = getParameter("name"); a = getParameter("age"); } public void paint(Graphics g) { g.drawString("Name is: " + n, 20, 20); g.drawString("Age is: " + a, 20, 40); } } /* <applet code="MyApplet" height="300" width="500"> <param name="name" value="Ramesh" /> <param name="age" value="25" /> </applet> */
  • 20.