SlideShare a Scribd company logo
1 of 20
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

More Related Content

What's hot

Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
 

What's hot (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
 
Packages
PackagesPackages
Packages
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
Java IO
Java IOJava IO
Java IO
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Similar to Java Applets

Applet Life Cycle in Java with brief introduction
Applet Life Cycle in Java with brief introductionApplet Life Cycle in Java with brief introduction
Applet Life Cycle in Java with brief introduction
devicse
 

Similar to Java Applets (20)

Applet
AppletApplet
Applet
 
Java applet
Java appletJava applet
Java applet
 
applet.pptx
applet.pptxapplet.pptx
applet.pptx
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Java applets
Java appletsJava applets
Java applets
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
 
Jsp applet
Jsp appletJsp applet
Jsp applet
 
Java applet - java
Java applet - javaJava applet - java
Java applet - java
 
APPLET.pptx
APPLET.pptxAPPLET.pptx
APPLET.pptx
 
Applet Life Cycle in Java with brief introduction
Applet Life Cycle in Java with brief introductionApplet Life Cycle in Java with brief introduction
Applet Life Cycle in Java with brief introduction
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Class notes(week 10) on applet programming
Class notes(week 10) on applet programmingClass notes(week 10) on applet programming
Class notes(week 10) on applet programming
 
Applet programming1
Applet programming1Applet programming1
Applet programming1
 
Applet
AppletApplet
Applet
 
Applet
AppletApplet
Applet
 
Class notes(week 10) on applet programming
Class notes(week 10) on applet programmingClass notes(week 10) on applet programming
Class notes(week 10) on applet programming
 

More from Dr. Jasmine Beulah Gnanadurai

More from Dr. Jasmine Beulah Gnanadurai (20)

Data Warehouse_Architecture.pptx
Data Warehouse_Architecture.pptxData Warehouse_Architecture.pptx
Data Warehouse_Architecture.pptx
 
DMQL(Data Mining Query Language).pptx
DMQL(Data Mining Query Language).pptxDMQL(Data Mining Query Language).pptx
DMQL(Data Mining Query Language).pptx
 
Stacks.pptx
Stacks.pptxStacks.pptx
Stacks.pptx
 
Quick Sort.pptx
Quick Sort.pptxQuick Sort.pptx
Quick Sort.pptx
 
KBS Architecture.pptx
KBS Architecture.pptxKBS Architecture.pptx
KBS Architecture.pptx
 
Knowledge Representation in AI.pptx
Knowledge Representation in AI.pptxKnowledge Representation in AI.pptx
Knowledge Representation in AI.pptx
 
File allocation methods (1)
File allocation methods (1)File allocation methods (1)
File allocation methods (1)
 
Segmentation in operating systems
Segmentation in operating systemsSegmentation in operating systems
Segmentation in operating systems
 
Mem mgt
Mem mgtMem mgt
Mem mgt
 
Decision tree
Decision treeDecision tree
Decision tree
 
Association rules apriori algorithm
Association rules   apriori algorithmAssociation rules   apriori algorithm
Association rules apriori algorithm
 
Big data architecture
Big data architectureBig data architecture
Big data architecture
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
Aritificial intelligence
Aritificial intelligenceAritificial intelligence
Aritificial intelligence
 
Java threads
Java threadsJava threads
Java threads
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
 
Css Text Formatting
Css Text FormattingCss Text Formatting
Css Text Formatting
 
CSS - Cascading Style Sheet
CSS - Cascading Style SheetCSS - Cascading Style Sheet
CSS - Cascading Style Sheet
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

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 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.
  • 3. 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.
  • 4. Lifecycle of Java Applet Applet is initialized. Applet is started. Applet is painted. Applet is stopped. Applet is destroyed.
  • 6. 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.
  • 7. • Each applet has four major events in its lifetime: –Initialization --- init() –Starting --- start() –Painting --- paint(Graphics) –Stopping --- stop() –Destroying --- destroy()
  • 8. 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.
  • 9. How to run an Applet? • There are two ways to run an applet By html file. By appletViewer tool
  • 10. 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.
  • 11. //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); } }
  • 13. 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
  • 14. //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
  • 15. 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>
  • 16. Passing Parameters to Applet • Supply user-defined parameters to an applet using <PARAM> tags. <applet> <param name=text value=“Applet> </applet>
  • 17. 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>
  • 18. • Write a applet program to pass name and age of a person and print it .
  • 19. 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> */