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

DOCX
Lecture1 oopj
Dhairya Joshi
 
PPTX
Applets in Java
Gary Mendonca
 
PPTX
Java applet
Elizabeth alexander
 
PDF
ITFT- Applet in java
Atul Sehdev
 
PPTX
Applets in Java
RamaPrabha24
 
PPTX
Applet1 (1).pptx
FahanaAbdulVahab
 
PPTX
Till applet skeleton
SouvikKole
 
PPT
Jsp applet
Sanoj Kumar
 
Lecture1 oopj
Dhairya Joshi
 
Applets in Java
Gary Mendonca
 
Java applet
Elizabeth alexander
 
ITFT- Applet in java
Atul Sehdev
 
Applets in Java
RamaPrabha24
 
Applet1 (1).pptx
FahanaAbdulVahab
 
Till applet skeleton
SouvikKole
 
Jsp applet
Sanoj Kumar
 

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

PPT
Java programming Java programming Java programming
Fadlie Ahdon
 
PDF
Java basics notes
Gomathi Gomu
 
PPT
Applet and graphics programming
mcanotes
 
PPTX
applet.pptx
SachinBhosale73
 
PPTX
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
PPTX
Applet Life Cycle in Java with brief introduction
devicse
 
PDF
Java basics notes
sanchi Sharma
 
PDF
Java programming basics notes for beginners(java programming tutorials)
Daroko blog(www.professionalbloggertricks.com)
 
PDF
Java basics notes
Nexus
 
PDF
Class notes(week 10) on applet programming
Kuntal Bhowmick
 
PPTX
Java Apple dndkorksnsbsjdkkdjejdjrdndjdj
midhunmsd143
 
PPT
java applets
Waheed Warraich
 
PPT
Java applet
Arati Gadgil
 
PPT
Applet Architecture - Introducing Java Applets
amitksaha
 
PPTX
Applet.pptx
LakachewYezihalem
 
PPTX
Applet (1)
DEEPIKA T
 
PPT
JAVA APPLET BASICS
Shanid Malayil
 
PPTX
Applets
Nuha Noor
 
PDF
Java applet basics
Sunil Pandey
 
Java programming Java programming Java programming
Fadlie Ahdon
 
Java basics notes
Gomathi Gomu
 
Applet and graphics programming
mcanotes
 
applet.pptx
SachinBhosale73
 
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
Applet Life Cycle in Java with brief introduction
devicse
 
Java basics notes
sanchi Sharma
 
Java programming basics notes for beginners(java programming tutorials)
Daroko blog(www.professionalbloggertricks.com)
 
Java basics notes
Nexus
 
Class notes(week 10) on applet programming
Kuntal Bhowmick
 
Java Apple dndkorksnsbsjdkkdjejdjrdndjdj
midhunmsd143
 
java applets
Waheed Warraich
 
Java applet
Arati Gadgil
 
Applet Architecture - Introducing Java Applets
amitksaha
 
Applet.pptx
LakachewYezihalem
 
Applet (1)
DEEPIKA T
 
JAVA APPLET BASICS
Shanid Malayil
 
Applets
Nuha Noor
 
Java applet basics
Sunil Pandey
 
Ad

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)

PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
Introduction of deep learning in cse.pptx
fizarcse
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
Color Model in Textile ( RGB, CMYK).pptx
auladhossain191
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PPTX
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Introduction of deep learning in cse.pptx
fizarcse
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Color Model in Textile ( RGB, CMYK).pptx
auladhossain191
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 

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>