SlideShare a Scribd company logo
1 of 22
Chapter 1
Java Applets
Introduction
• Part of Java’s success comes from the
fact that it is the first language specifically
designed to take advantage of the power
of the World-Wide Web
• In addition to more traditional application
programs, Java makes it possible to write
small interactive programs called applets
that run under the control of a web
browser
• Applet
– Program that runs in
• Web browser (IE, Communicator)
• appletviewer
– a program designed to run an applet as a stand-alone program
– Executes when HTML (Hypertext Markup Language)
document containing applet is opened and downloaded
– Can not run independently
– normally run within a controlled environment (sandbox)
– Used in internet computing.
– Every browser implements security policies to keep
applets from compromising system security
Introduction
Applications vs. Applets
• Similarities
– Both applets and applications are Java
programs
– Since JFrame and JApplet both are
subclasses of the Container class, all the
user interface components, layout managers,
and event-handling features are the same for
both classes.
Applications vs. Applets
• Differences
– Applications are invoked from the static main method
by the Java interpreter, and applets are run by the
Web browser.
• The Web browser creates an instance of the applet using the
applet’s no-arg constructor and controls and executes the
applet through the init, start, stop, and destroy methods.
– Applets have security restrictions
– Applications run in command windows whereas
applets run in web browsers
– Web browser creates graphical environment for
applets, GUI applications are placed in a frame.
– There is no main() method in an Applet.
Security Restrictions on Applets
• Applets are not allowed to read from, or write to,
the file system of the computer viewing the
applets.
• Applets are not allowed to run any programs on
the browser’s computer.
• Applets are not allowed to establish connections
between the user’s computer and another
computer except with the server where
the applets are stored.
• An applet cannot load libraries or define native
methods.
• It cannot read certain system properties
Applet class
• From Component, an applet inherits the ability to draw
and handle events
• From Container, an applet inherits the ability to include
other components and to have a layout manager control
the size and position of those components
• Every applet is implemented by creating a subclass of
the Applet class
Life Cycle of Applet
• An applet actually has a life cycle
– It can initialize itself.
– It can start running.
– It can stop running.
– It can perform a final cleanup, in preparation for
being unloaded.
The Applet Class
•When the applet is loaded, the Web
browser creates an instance of the applet
by invoking the applet’s no-arg
constructor.
•The browser uses the init, start, stop, and
destroy methods to control the applet.
•By default, these methods do nothing. To
perform specific functions, they need to be
modified in the user's applet so that the
browser can call your code properly.
Browser Calling Applet Methods
Browser
invokes start()
Destroyed
Browser invokes
destroy()
Browser
invokes stop()
Loaded
Initialized
Browser
invokes init()
Started Stopped
Created
Browser creates
the applet
JVM loads the
applet class
Browser
invokes stop()
Browser
invokes start()
The init() Method
Invoked when the applet is first loaded
and again if the applet is reloaded.
A subclass of Applet should override this
method if the subclass has an initialization
to perform. The functions usually
implemented in this method include
creating new threads, loading images,
setting up user-interface components, and
getting string parameter values from the
<applet> tag in the HTML page.
The start() Method
Invoked after the init() method is executed;
also called whenever the applet becomes active
again after a period of inactivity (for example,
when the user returns to the page containing the
applet after surfing other Web pages).
A subclass of Applet overrides this method if
it has any operation that needs to be
performed whenever the Web page
containing the applet is visited. An applet
with animation, for example, might use the
start method to resume animation.
The stop() Method
The opposite of the start() method, which is called
when the user moves back to the page containing the
applet; the stop() method is invoked when the user
moves off the page.
A subclass of Applet overrides this method if it
has any operation that needs to be performed
each time the Web page containing the applet is
no longer visible. When the user leaves the
page, any threads the applet has started but not
completed will continue to run. You should
override the stop method to suspend the running
threads so that the applet does not take up
system resources when it is inactive.
The destroy() Method
Invoked when the browser exits normally
to inform the applet that it is no longer
needed and that it should release any
resources it has allocated.
A subclass of Applet overrides this method if it
has any operation that needs to be performed
before it is destroyed. Usually, you won't need to
override this method unless you wish to release
specific resources, such as threads that the
applet created.
The JApplet Class
•The Applet class is an AWT class and is not
designed to work with Swing components.
•To use Swing components in Java applets, it is
necessary to create a Java applet that extends
javax.swing.JApplet, which is a subclass of
java.applet.Applet.
•JApplet inherits all the methods from the Applet
class. In addition, it provides support for laying
out Swing components.
Writing Applets
• Always extends the JApplet class, which is
a subclass of Applet for Swing components.
• Override init(), start(), stop(), and
destroy() if necessary. By default, these
methods are empty.
• Add your own methods and data if necessary.
• Applets are always embedded in an
HTML page.
Java Applet Skeleton
/*
Program MyFirstApplet
An applet that displays the text "I
Love Java"
and a rectangle around the text.
*/
import java.applet.*;
import java.awt.*;
public class MyFirstApplet extends JApplet
{
public void paint( Graphics graphic)
{
graphic.drawString("I Love
Java",70,70);
graphic.drawRect(50,50,100,30);
}
}
Comment
Import
Statements
Class Name
Method Body
First Simple Applet
// WelcomeApplet.java: Applet for
//displaying a message
import javax.swing.*;
public class WelcomeApplet extends
JApplet {
/** Initialize the applet */
public void init() {
add(new JLabel("Welcome to Java",
JLabel.CENTER));
}
}
First Simple Applet
<html>
<head>
<title>Welcome Java Applet</title>
</head>
<body>
<applet
code = "WelcomeApplet.class"
width = 350
height = 200>
</applet>
</body>
</html>
1 // Fig. 3.6: WelcomeApplet.java
2 // A first applet in Java.
3
4 // Java core packages
5 import java.awt.Graphics; // import class Graphics
6
7 // Java extension packages
8 import javax.swing.JApplet; // import class JApplet
9
10 public class WelcomeApplet extends JApplet {
11
12 // draw text on applet’s background
13 public void paint( Graphics g )
14 {
15 // call inherited version of method paint
16 super.paint( g );
17
18 // draw a String at x-coordinate 25 and y-coordinate 25
19 g.drawString( "Welcome to Java Programming!", 25, 25 );
20
21 } // end method paint
22
23 } // end class WelcomeApplet
import allows us to use
predefined classes (allowing
us to use applets and
graphics, in this case).
extends allows us to inherit the
capabilities of class JApplet.
Method paint is guaranteed to
be called in all applets. Its first
line must be defined as above.
A Simple Java Applet: Drawing a String
The <applet> HTML Tag
<applet
code=classfilename.class
width=applet_viewing_width_in_pixels
height=applet_viewing_height_in_pixels
[archive=archivefile]
[codebase=applet_url]
[vspace=vertical_margin]
[hspace=horizontal_margin]
[align=applet_alignment]
[alt=alternative_text]
>
<param name=param_name1
value=param_value1>
</applet>
Passing Parameters to Applets
<applet
code = "DisplayMessage.class"
width = 200
height = 50>
<param name=MESSAGE value="Welcome
to Java">
<param name=X value=20>
<param name=Y value=20>
alt="You must have a Java-enabled
browser to view the applet"
</applet>

More Related Content

Similar to Advanced Programming, Java Programming, Applets.ppt

Similar to Advanced Programming, Java Programming, Applets.ppt (20)

Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
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
AppletApplet
Applet
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
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
 
java applets
java appletsjava applets
java applets
 
Java applet
Java appletJava applet
Java applet
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Applet.pptx
Applet.pptxApplet.pptx
Applet.pptx
 
Applet (1)
Applet (1)Applet (1)
Applet (1)
 
JAVA APPLET BASICS
JAVA APPLET BASICSJAVA APPLET BASICS
JAVA APPLET BASICS
 
Applets
AppletsApplets
Applets
 
Java applet basics
Java applet basicsJava applet basics
Java applet basics
 
Java
JavaJava
Java
 
Java applet-basics
Java applet-basicsJava applet-basics
Java applet-basics
 
Java applet-basics
Java applet-basicsJava applet-basics
Java applet-basics
 

More from miki304759

Chapter Last.ppt
Chapter Last.pptChapter Last.ppt
Chapter Last.pptmiki304759
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.pptmiki304759
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxmiki304759
 
Chapter one_oS.ppt
Chapter one_oS.pptChapter one_oS.ppt
Chapter one_oS.pptmiki304759
 
Chapter 3 SE 2015.pptx
Chapter 3 SE 2015.pptxChapter 3 SE 2015.pptx
Chapter 3 SE 2015.pptxmiki304759
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
4_5809869271378954936.pptx
4_5809869271378954936.pptx4_5809869271378954936.pptx
4_5809869271378954936.pptxmiki304759
 

More from miki304759 (7)

Chapter Last.ppt
Chapter Last.pptChapter Last.ppt
Chapter Last.ppt
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.ppt
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
 
Chapter one_oS.ppt
Chapter one_oS.pptChapter one_oS.ppt
Chapter one_oS.ppt
 
Chapter 3 SE 2015.pptx
Chapter 3 SE 2015.pptxChapter 3 SE 2015.pptx
Chapter 3 SE 2015.pptx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
4_5809869271378954936.pptx
4_5809869271378954936.pptx4_5809869271378954936.pptx
4_5809869271378954936.pptx
 

Recently uploaded

(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
 
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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(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
 

Recently uploaded (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
 
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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
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 )
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
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...
 
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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.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...
 

Advanced Programming, Java Programming, Applets.ppt

  • 2. Introduction • Part of Java’s success comes from the fact that it is the first language specifically designed to take advantage of the power of the World-Wide Web • In addition to more traditional application programs, Java makes it possible to write small interactive programs called applets that run under the control of a web browser
  • 3. • Applet – Program that runs in • Web browser (IE, Communicator) • appletviewer – a program designed to run an applet as a stand-alone program – Executes when HTML (Hypertext Markup Language) document containing applet is opened and downloaded – Can not run independently – normally run within a controlled environment (sandbox) – Used in internet computing. – Every browser implements security policies to keep applets from compromising system security Introduction
  • 4. Applications vs. Applets • Similarities – Both applets and applications are Java programs – Since JFrame and JApplet both are subclasses of the Container class, all the user interface components, layout managers, and event-handling features are the same for both classes.
  • 5. Applications vs. Applets • Differences – Applications are invoked from the static main method by the Java interpreter, and applets are run by the Web browser. • The Web browser creates an instance of the applet using the applet’s no-arg constructor and controls and executes the applet through the init, start, stop, and destroy methods. – Applets have security restrictions – Applications run in command windows whereas applets run in web browsers – Web browser creates graphical environment for applets, GUI applications are placed in a frame. – There is no main() method in an Applet.
  • 6. Security Restrictions on Applets • Applets are not allowed to read from, or write to, the file system of the computer viewing the applets. • Applets are not allowed to run any programs on the browser’s computer. • Applets are not allowed to establish connections between the user’s computer and another computer except with the server where the applets are stored. • An applet cannot load libraries or define native methods. • It cannot read certain system properties
  • 7. Applet class • From Component, an applet inherits the ability to draw and handle events • From Container, an applet inherits the ability to include other components and to have a layout manager control the size and position of those components • Every applet is implemented by creating a subclass of the Applet class
  • 8. Life Cycle of Applet • An applet actually has a life cycle – It can initialize itself. – It can start running. – It can stop running. – It can perform a final cleanup, in preparation for being unloaded.
  • 9. The Applet Class •When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. •The browser uses the init, start, stop, and destroy methods to control the applet. •By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly.
  • 10. Browser Calling Applet Methods Browser invokes start() Destroyed Browser invokes destroy() Browser invokes stop() Loaded Initialized Browser invokes init() Started Stopped Created Browser creates the applet JVM loads the applet class Browser invokes stop() Browser invokes start()
  • 11. The init() Method Invoked when the applet is first loaded and again if the applet is reloaded. A subclass of Applet should override this method if the subclass has an initialization to perform. The functions usually implemented in this method include creating new threads, loading images, setting up user-interface components, and getting string parameter values from the <applet> tag in the HTML page.
  • 12. The start() Method Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity (for example, when the user returns to the page containing the applet after surfing other Web pages). A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might use the start method to resume animation.
  • 13. The stop() Method The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page. A subclass of Applet overrides this method if it has any operation that needs to be performed each time the Web page containing the applet is no longer visible. When the user leaves the page, any threads the applet has started but not completed will continue to run. You should override the stop method to suspend the running threads so that the applet does not take up system resources when it is inactive.
  • 14. The destroy() Method Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated. A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed. Usually, you won't need to override this method unless you wish to release specific resources, such as threads that the applet created.
  • 15. The JApplet Class •The Applet class is an AWT class and is not designed to work with Swing components. •To use Swing components in Java applets, it is necessary to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet. •JApplet inherits all the methods from the Applet class. In addition, it provides support for laying out Swing components.
  • 16. Writing Applets • Always extends the JApplet class, which is a subclass of Applet for Swing components. • Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. • Add your own methods and data if necessary. • Applets are always embedded in an HTML page.
  • 17. Java Applet Skeleton /* Program MyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text. */ import java.applet.*; import java.awt.*; public class MyFirstApplet extends JApplet { public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); } } Comment Import Statements Class Name Method Body
  • 18. First Simple Applet // WelcomeApplet.java: Applet for //displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public void init() { add(new JLabel("Welcome to Java", JLabel.CENTER)); } }
  • 19. First Simple Applet <html> <head> <title>Welcome Java Applet</title> </head> <body> <applet code = "WelcomeApplet.class" width = 350 height = 200> </applet> </body> </html>
  • 20. 1 // Fig. 3.6: WelcomeApplet.java 2 // A first applet in Java. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeApplet extends JApplet { 11 12 // draw text on applet’s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super.paint( g ); 17 18 // draw a String at x-coordinate 25 and y-coordinate 25 19 g.drawString( "Welcome to Java Programming!", 25, 25 ); 20 21 } // end method paint 22 23 } // end class WelcomeApplet import allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends allows us to inherit the capabilities of class JApplet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above. A Simple Java Applet: Drawing a String
  • 21. The <applet> HTML Tag <applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text] > <param name=param_name1 value=param_value1> </applet>
  • 22. Passing Parameters to Applets <applet code = "DisplayMessage.class" width = 200 height = 50> <param name=MESSAGE value="Welcome to Java"> <param name=X value=20> <param name=Y value=20> alt="You must have a Java-enabled browser to view the applet" </applet>