SlideShare a Scribd company logo
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 (20)

PPTX
Introduction To Applets methods and simple examples
MsPariyalNituLaxman
 
PPTX
Applet (1)
DEEPIKA T
 
PPTX
Applets
Nuha Noor
 
PPTX
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
PPT
Applets
Inayat Sharma
 
PPT
Applets
Abhishek Khune
 
PPTX
applet.pptx
SachinBhosale73
 
PDF
Class notes(week 10) on applet programming
Kuntal Bhowmick
 
PPTX
Applet1 (1).pptx
FahanaAbdulVahab
 
PPT
Basic of Applet
suraj pandey
 
PPTX
Applets in java
Wani Zahoor
 
PPT
Java applet
Arati Gadgil
 
PPT
Applets
SanthiNivas
 
PDF
Lecture 22
Debasish Pratihari
 
PPT
Applet and graphics programming
mcanotes
 
PPT
JAVA APPLET BASICS
Shanid Malayil
 
PPTX
Applet intro
Nitin Birari
 
PPTX
Applet
sweetysweety8
 
PPTX
Applet in java new
Kavitha713564
 
PDF
Advanced programming chapter 2 - Java Applet.pdf
fikadumeuedu
 
Introduction To Applets methods and simple examples
MsPariyalNituLaxman
 
Applet (1)
DEEPIKA T
 
Applets
Nuha Noor
 
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
Applets
Inayat Sharma
 
Applets
Abhishek Khune
 
applet.pptx
SachinBhosale73
 
Class notes(week 10) on applet programming
Kuntal Bhowmick
 
Applet1 (1).pptx
FahanaAbdulVahab
 
Basic of Applet
suraj pandey
 
Applets in java
Wani Zahoor
 
Java applet
Arati Gadgil
 
Applets
SanthiNivas
 
Lecture 22
Debasish Pratihari
 
Applet and graphics programming
mcanotes
 
JAVA APPLET BASICS
Shanid Malayil
 
Applet intro
Nitin Birari
 
Applet
sweetysweety8
 
Applet in java new
Kavitha713564
 
Advanced programming chapter 2 - Java Applet.pdf
fikadumeuedu
 

More from miki304759 (9)

PPTX
Software Evolution and maintenance chapter 1
miki304759
 
PPTX
Software Evolution and maintenance chapter 3
miki304759
 
PPT
Chapter Last.ppt
miki304759
 
PPT
Chapter 1- Introduction.ppt
miki304759
 
PPTX
Elements of Graph Theory for IS.pptx
miki304759
 
PPT
Chapter one_oS.ppt
miki304759
 
PPTX
Chapter 3 SE 2015.pptx
miki304759
 
PPTX
Chapter One Function.pptx
miki304759
 
PPTX
4_5809869271378954936.pptx
miki304759
 
Software Evolution and maintenance chapter 1
miki304759
 
Software Evolution and maintenance chapter 3
miki304759
 
Chapter Last.ppt
miki304759
 
Chapter 1- Introduction.ppt
miki304759
 
Elements of Graph Theory for IS.pptx
miki304759
 
Chapter one_oS.ppt
miki304759
 
Chapter 3 SE 2015.pptx
miki304759
 
Chapter One Function.pptx
miki304759
 
4_5809869271378954936.pptx
miki304759
 
Ad

Recently uploaded (20)

PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
Depth First Search Algorithm in đź§  DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Depth First Search Algorithm in đź§  DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Day2 B2 Best.pptx
helenjenefa1
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Ad

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>