SlideShare uma empresa Scribd logo
1 de 31
Applet
Programming
Chapter-5
Marks-20
Introduction
• There are two kinds of Java programs-
–Applications (stand-alone programs)
–Applets.
• An Applet is a small Internet-based program that
has the Graphical User Interface (GUI), written in
the Java programming language.
Introduction
• A Java applet is a small application written in
Java and delivered to users in the form
of bytecode.
• A applet is typically embedded in a Web page
and can be run from a browser.
• Applets can be transported over the Internet
from one computer to another.
How applet differ from application
• Applets are the small programs, applications are
larger programs.
• Applets don't have the main method while in an
application execution starts with the main
method.
• Applications run independently while Applets
cannot. They run from inside the web page.
• Applets cannot read from or write to the files in
the local computer.
Introduction
• An applet can perform
–arithmetic operation,
–display graphics,
–play sounds,
–accept user input,
–create animation and
–play interactive games.
Local and Remote Applets
• We can embed applets into web pages in
two ways-
1. Create our own applets ( Local )
2. Download an applet from remote computer
system (Remote Applet )
Local Applet
• An applet developed locally and stored in local
system is known as local applet.
• When a web page trying to find a local applet, it
does not need to use the Internet.
• It simply searches the directories in the local
system and locates and loads the specified
applet. local applet
Remote Applet
• A remote applet is that which is developed by
someone else and stored on the remote
computer connected to internet.
• Users can download the remote applet onto
there system and run it.
Local Computer Remote Computer
Internet
Steps involved in developing applets
1. Building an applet code (. java file )
2. Creating an executable applet ( .class file )
3. Designing a Web Page using HTML tags.
4. Preparing <APPLET> tag.
5. Incorporating <APPLET> tag into the Web
Page.
6. Creating HTML file.
7. Testing the Applet Code.
Building Applet Code
• It is essential that applet code uses services of
two classes
–Applet class and
–Graphics class.
Chain of classes inherited by Applet class
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
The Applet Class
• The java.applet package is the smallest package
in Java API.
• The Applet class is the only class in the package.
• The Applet class provides life and behaviour to
applet through its methods.
• The Applet class has many methods that are used
to display images, play audio files etc but it has
no main() method.
Methods of Applet Class
• init() :
– used for whatever initializations are needed for
applet.
– Applets can have a default constructor, but it is better
to perform all initializations in the init method
instead of the default constructor.
• start() :
– This method is automatically called after Java calls
the init method.
• stop() :
– This method is automatically called when the user
moves off the page where the applet sits.
• destroy():
– Java calls this method when the browser shuts
down.
paint() Method of Applet Class
• Paint method actually displays the result of applet
code on the screen.
• The output may be text, graphics, or sound.
• Syntax-
public void paint(Graphics g)
• The paint method requires graphics object as an
argument.
• Hence we require to import Graphics class from
java.awt package.
Applet Life Cycle
Born
Running Idle
Dead End
Stopped
Destroyed
Exit browser
Begin
(Load applet) Initialization
Display
paint()
start()
stop()
start()
destroy()
Applet state transition diagram
Born or Initialization State
• An applet begins its life when the web browser
loads its classes and calls its init() method.
• This is called exactly once in Applets lifecycle.
• init() method provides initialization code such as
initialization of variables, loading images or
fonts.
Eg.
public void init()
{
//initialization
Running State
• Once the initialization is complete, the web
browser will call the start() method in the applet.
• This method must called atleast once in the
Applets lifecycle.
• The start() method can also be called if the
Applet is in “Idle” state.
Eg. public void start()
{
//Code
}
Stopped State
• The web browser will call the Applets stop()
method, if the user moved to another web page
while the applet was executing.
• The stop() method is called atleast once in
Applets Lifecycle.
Eg. public void stop()
{
//Code
}
Dead State
• Finally, if the user decides to quit the web
browser, the web browser will free up system
resources by killing the applet before it closes.
• To do so, it will call the applets destroy()
method.
Eg.
public void destroy()
{
// Code
}
Display State
• Applet moves to the display state whenever it
has to perform the output operations on the
screen.
• This happens immediately after the applet
enters into the running state.
• The paint() method is called to accomplish this
task.
Eg.
public void paint(Graphics g)
{
//Display Statements
General format of Applet Code
import java.awt.*;
import java.applet.*;
……………………
public class applet_class_name extends Applet
{ ……………
public void paint(Graphics g)
{
………….//applet operation code
}
}
Example.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("My First Applet",40,40);
}
}
Designing a Web Page
• To execute an applet in a web browser, write a
short HTML text file that contains the
appropriate APPLET tag.
• For above example it is
<html>
<body>
<applet code="SimpleApplet.class" width=200
height=100>
</applet>
</body>
</html>
The Applet tag
<applet
[codebase=codebaseURL]
code=”Applet file”
[ALT=”alternative text]
[name=AppletInstanceName]
Width=pixels
height= pixels
[align= alignment]
>
[<param name=”Attributename” value =”Attribute value”]
[<param name=”Attributename” value =”Attribute value”]
</applet>
Passing Parameter to Applet
• We can supply user-defined parameters to an
applet using <param.....> tag.
• Each <param....> tag has a name ,and a value
attribute.
• For e.g. the color of the text can be changed to
red by an applet using a <param...> tag as
follows
<applet ……>
<param name=“color” value=“RED”>
Passing Parameters to Applet
• To handle parameters, we need to do two things.
1) Include <param.....> tags in HTML document.
2) Provide code in the applet to pass these parameters.
• We can define the init() method in the applet to
get parameters defined in the <param> tags.
•
• This is done using the getParameter() method,
which takes one string argument representing
the name of the parameter and returns a string
containing the value of that parameter.
Example :Parameter Passing
import java.awt.*;
import java.applet.*;
public class HelloJava 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=“HelloJava.class”
width=400
height=400 >
<param name= “String” value= “Applet!” >
</applet>
</body>
</html>
Displaying numerical values
import java.awt.*;
import java.applet.*;
public class Hellojava 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,10,100);
}
Using Control Structure
import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet {
public void paint(Graphics g) {
int y=50;
for(int i=1;i<=10;i++)
{ g.drawString(i+” ”,50,y);
y=y+10;
} } }
/*<applet code=“Hellojava.class” width=300 height=100>
</applet> */

Mais conteúdo relacionado

Semelhante a applet.pptx (20)

Java applet
Java appletJava applet
Java applet
 
Applets
AppletsApplets
Applets
 
Java applet basics
Java applet basicsJava applet basics
Java applet basics
 
Java Applets
Java AppletsJava Applets
Java Applets
 
Java applet
Java appletJava applet
Java applet
 
Java
JavaJava
Java
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
JAVA APPLET BASICS
JAVA APPLET BASICSJAVA APPLET BASICS
JAVA APPLET BASICS
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Applet programming1
Applet programming1Applet programming1
Applet programming1
 
Applet
AppletApplet
Applet
 
Applet intro
Applet introApplet intro
Applet intro
 
Advanced Programming, Java Programming, Applets.ppt
Advanced Programming, Java Programming, Applets.pptAdvanced Programming, Java Programming, Applets.ppt
Advanced Programming, Java Programming, Applets.ppt
 
Java applet
Java appletJava applet
Java applet
 
6applets And Graphics
6applets And Graphics6applets And Graphics
6applets And Graphics
 
Applets 101-fa06
Applets 101-fa06Applets 101-fa06
Applets 101-fa06
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
 
Java applet - java
Java applet - javaJava applet - java
Java applet - java
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 

Mais de SachinBhosale73

Mais de SachinBhosale73 (15)

Dentrifries.pptx
Dentrifries.pptxDentrifries.pptx
Dentrifries.pptx
 
dental product.pptx
dental product.pptxdental product.pptx
dental product.pptx
 
Biochemistry Practicals.pptx
Biochemistry Practicals.pptxBiochemistry Practicals.pptx
Biochemistry Practicals.pptx
 
Acid-bases-buffers.pptx
Acid-bases-buffers.pptxAcid-bases-buffers.pptx
Acid-bases-buffers.pptx
 
abc.pptx
abc.pptxabc.pptx
abc.pptx
 
Manisha.pptx
Manisha.pptxManisha.pptx
Manisha.pptx
 
Research Proposal.pptx
Research Proposal.pptxResearch Proposal.pptx
Research Proposal.pptx
 
Introduction of Deep Learning.pptx
Introduction  of Deep Learning.pptxIntroduction  of Deep Learning.pptx
Introduction of Deep Learning.pptx
 
Unit I- Fundamental of Deep Learning.pptx
Unit I- Fundamental of Deep Learning.pptxUnit I- Fundamental of Deep Learning.pptx
Unit I- Fundamental of Deep Learning.pptx
 
HPLC Final.ppt
HPLC Final.pptHPLC Final.ppt
HPLC Final.ppt
 
bonding_regents_chem.ppt
bonding_regents_chem.pptbonding_regents_chem.ppt
bonding_regents_chem.ppt
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
java01.ppt
java01.pptjava01.ppt
java01.ppt
 
CSS.ppt
CSS.pptCSS.ppt
CSS.ppt
 
Role of Pharmacist in COVID19.pptx
Role of Pharmacist in COVID19.pptxRole of Pharmacist in COVID19.pptx
Role of Pharmacist in COVID19.pptx
 

Último

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Último (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 

applet.pptx

  • 2. Introduction • There are two kinds of Java programs- –Applications (stand-alone programs) –Applets. • An Applet is a small Internet-based program that has the Graphical User Interface (GUI), written in the Java programming language.
  • 3. Introduction • A Java applet is a small application written in Java and delivered to users in the form of bytecode. • A applet is typically embedded in a Web page and can be run from a browser. • Applets can be transported over the Internet from one computer to another.
  • 4. How applet differ from application • Applets are the small programs, applications are larger programs. • Applets don't have the main method while in an application execution starts with the main method. • Applications run independently while Applets cannot. They run from inside the web page. • Applets cannot read from or write to the files in the local computer.
  • 5. Introduction • An applet can perform –arithmetic operation, –display graphics, –play sounds, –accept user input, –create animation and –play interactive games.
  • 6. Local and Remote Applets • We can embed applets into web pages in two ways- 1. Create our own applets ( Local ) 2. Download an applet from remote computer system (Remote Applet )
  • 7. Local Applet • An applet developed locally and stored in local system is known as local applet. • When a web page trying to find a local applet, it does not need to use the Internet. • It simply searches the directories in the local system and locates and loads the specified applet. local applet
  • 8. Remote Applet • A remote applet is that which is developed by someone else and stored on the remote computer connected to internet. • Users can download the remote applet onto there system and run it. Local Computer Remote Computer Internet
  • 9. Steps involved in developing applets 1. Building an applet code (. java file ) 2. Creating an executable applet ( .class file ) 3. Designing a Web Page using HTML tags. 4. Preparing <APPLET> tag. 5. Incorporating <APPLET> tag into the Web Page. 6. Creating HTML file. 7. Testing the Applet Code.
  • 10. Building Applet Code • It is essential that applet code uses services of two classes –Applet class and –Graphics class.
  • 11. Chain of classes inherited by Applet class java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet
  • 12. The Applet Class • The java.applet package is the smallest package in Java API. • The Applet class is the only class in the package. • The Applet class provides life and behaviour to applet through its methods. • The Applet class has many methods that are used to display images, play audio files etc but it has no main() method.
  • 13. Methods of Applet Class • init() : – used for whatever initializations are needed for applet. – Applets can have a default constructor, but it is better to perform all initializations in the init method instead of the default constructor. • start() : – This method is automatically called after Java calls the init method.
  • 14. • stop() : – This method is automatically called when the user moves off the page where the applet sits. • destroy(): – Java calls this method when the browser shuts down.
  • 15. paint() Method of Applet Class • Paint method actually displays the result of applet code on the screen. • The output may be text, graphics, or sound. • Syntax- public void paint(Graphics g) • The paint method requires graphics object as an argument. • Hence we require to import Graphics class from java.awt package.
  • 16. Applet Life Cycle Born Running Idle Dead End Stopped Destroyed Exit browser Begin (Load applet) Initialization Display paint() start() stop() start() destroy() Applet state transition diagram
  • 17. Born or Initialization State • An applet begins its life when the web browser loads its classes and calls its init() method. • This is called exactly once in Applets lifecycle. • init() method provides initialization code such as initialization of variables, loading images or fonts. Eg. public void init() { //initialization
  • 18. Running State • Once the initialization is complete, the web browser will call the start() method in the applet. • This method must called atleast once in the Applets lifecycle. • The start() method can also be called if the Applet is in “Idle” state. Eg. public void start() { //Code }
  • 19. Stopped State • The web browser will call the Applets stop() method, if the user moved to another web page while the applet was executing. • The stop() method is called atleast once in Applets Lifecycle. Eg. public void stop() { //Code }
  • 20. Dead State • Finally, if the user decides to quit the web browser, the web browser will free up system resources by killing the applet before it closes. • To do so, it will call the applets destroy() method. Eg. public void destroy() { // Code }
  • 21. Display State • Applet moves to the display state whenever it has to perform the output operations on the screen. • This happens immediately after the applet enters into the running state. • The paint() method is called to accomplish this task. Eg. public void paint(Graphics g) { //Display Statements
  • 22. General format of Applet Code import java.awt.*; import java.applet.*; …………………… public class applet_class_name extends Applet { …………… public void paint(Graphics g) { ………….//applet operation code } }
  • 23. Example. import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("My First Applet",40,40); } }
  • 24. Designing a Web Page • To execute an applet in a web browser, write a short HTML text file that contains the appropriate APPLET tag. • For above example it is <html> <body> <applet code="SimpleApplet.class" width=200 height=100> </applet> </body> </html>
  • 25. The Applet tag <applet [codebase=codebaseURL] code=”Applet file” [ALT=”alternative text] [name=AppletInstanceName] Width=pixels height= pixels [align= alignment] > [<param name=”Attributename” value =”Attribute value”] [<param name=”Attributename” value =”Attribute value”] </applet>
  • 26. Passing Parameter to Applet • We can supply user-defined parameters to an applet using <param.....> tag. • Each <param....> tag has a name ,and a value attribute. • For e.g. the color of the text can be changed to red by an applet using a <param...> tag as follows <applet ……> <param name=“color” value=“RED”>
  • 27. Passing Parameters to Applet • To handle parameters, we need to do two things. 1) Include <param.....> tags in HTML document. 2) Provide code in the applet to pass these parameters. • We can define the init() method in the applet to get parameters defined in the <param> tags. • • This is done using the getParameter() method, which takes one string argument representing the name of the parameter and returns a string containing the value of that parameter.
  • 28. Example :Parameter Passing import java.awt.*; import java.applet.*; public class HelloJava 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)
  • 29. <html> <body> <applet code=“HelloJava.class” width=400 height=400 > <param name= “String” value= “Applet!” > </applet> </body> </html>
  • 30. Displaying numerical values import java.awt.*; import java.applet.*; public class Hellojava 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,10,100); }
  • 31. Using Control Structure import java.awt.*; import java.applet.*; public class Hellojava extends Applet { public void paint(Graphics g) { int y=50; for(int i=1;i<=10;i++) { g.drawString(i+” ”,50,y); y=y+10; } } } /*<applet code=“Hellojava.class” width=300 height=100> </applet> */