SlideShare a Scribd company logo
Core Java

Debasish Pratihari

Applet:


Applets are small applications that are accessed
on an internet server, transposed over the
internet, automatically installed, and run as part
of a web component.



Applets are Java programs that are embedded
within a Web page. Therefore, unlike
applications, applets require a Java-enabled
browser, such as Microsoft Internet Explorer 4.0
or later, Netscape Navigator 4.0 or later, or
HotJava. These browsers are said to be Javaenabled because they have a built-in Java
platform (JVM and Java API).



An applet is loaded and executed when you load
a Web page by using a Web browser. When a
Web page containing an applet is displayed, you
can interact with the applet. You can use applets
to add dynamic features, such as animation and
sound, to a Web page. You can also use them to
make a Web page interactive.



After an applet arrives on the client, it has
limited access to resources, so that it can
produce a reliable user interface without
introducing the risk of viruses.

More About Applet :









Are sub-classes of Applet Class
<applet> tag of html is used to load an
applet in a web page.
Are executed remotely by java-enabled
browser.
Do not have a main( ) method.
Execution begins at init( ) method
Are not independent objects like application
Are purely GUI.
It can access the resources of only the host
computer; it cannot access the files on the
computer on which it is downloaded.

Lecture/core/applet/22

Page #1

feel the Technology…
Core Java

Debasish Pratihari

The life cycle of an Applet :



init()

is executed only once



start()

Executed multiple times every time
the focus comes backs to the page



stop()

Executed multiple times every time
The focus is lost from the web Page.



destroy()

Is called only once when the page is
terminated or closed.



An applet writes to its window only when its update() or
paint() method is called by the AWT.



The fundamental architecture constraints imposed on an
applet is that it must quickly return control to the AWT
run-time system. So you shouldn’t create a loop in
side paint().



The repaint() method is defined by the AWT. It causes
the AWT run-time system to execute a call to your
applet’s update( ) method, which in its default
implementation, calls paint ( ).



The paint() method Called every time the window is
resized or restored or Called forcibly by the user by calling
repaint( ) method

Lecture/core/applet/22

Page #2

feel the Technology…
Core Java

Debasish Pratihari

Creating an Applet:
import java.awt.*
import java.appet.*
sub-class the applet class
over-ride methods as per need.
Save the file with extension .java
Compile to create .class file
Create a HTML file
Use applet tag to position the applet inside the
page
 <applet code = x width =300 height=200>
</applet>
 save the file with extension .html
 open a browser and load the .html file









Attributes of Applet Tag:
code

:

specifies the applet class file

[codebase]

:

is an optional attribute that specifies the base
URL of the Applet.

height

:

specifies the height of applet

width

:

specifies width of applet

[align]

:

specifies alignment, the align must constant are
LEFT, RIGHT, TEXTTOP, MIDDLE, ABSMIDDLE,
BOTTOM, ABSBOTTOM, BASELINE

[vspace]

:

specifies vertical space

[Hspace]

:

specifies horizontal space

[archieve]

:

specifies the jar file

[Name]

:

specifies the alias name for the applet used
for inter applet communication.

Example :
import java.awt.*;
import java.applet.*;
/*<applet code = myapplet width=300 height=400></applet>*/

public class Myapplet extends Applet{
public void paint(Graphics g){
g.drawString(“ hello every body”,10,20);
}
}

Lecture/core/applet/22

Page #3

Note :
You can test Applets
using the Java Tool
appletviewer.

feel the Technology…
Core Java

Debasish Pratihari

A summary of Methods in the Applet Package:
Method

Function

public String getAppletInfo()
public URL getDocumentBase()
public String
getParameter(String name)
public String [][]
getParameterInfo()
public AudioClip
getAudioClip(URL)

Returns information about
the applet, such as author
Returns the URL of the
HTML document
Returns the parameters for
an applet
Returns a summary of what
the parameters control
Used to load an audio clip

public Image getImage(URL)

Used to load an image file
Used to play a previously
loaded audio clip
Lets you know whether an
applet is active

public void play(URL)
public boolean isActive()
public void resize(int, int)

Used to resize the applet

public void showStatus(String
msg)

Displays a status string in
the applet's browser

public void init()

Initializes the applet
Starts the applet when it's
finished initializing
Stops the applet when you
leave the applet's page
Destroys the applet when
you leave the browser

public void start()
public void stop()
public void destroy()

Difference between Application & Applet :
Application

Applet

Are independent

Dependent

Are executed under the local
o/s.

Executed remotely by a browser.

CUI or GUI

GUI

Execution starts with main( )

Starts with init(

Terminates with main()

Terminates only when the page is
closed

Lecture/core/applet/22

Page #4

) method.

feel the Technology…
Core Java

Debasish Pratihari

Notes
The Graphics Class
The Graphics class is an abstract class that represents the display area of an applet. This class is
a part of the java.awt package and you use it to draw images within the display area of the
applet. The object of the Graphics class is used for painting the applet.
The init() Method
The init() method is called when an applet is loaded into the memory of a computer for the first
time. The init() method works like a constructor, which means that it is executed automatically
by the system. By using the init() method, you can initialize variables and add components such
as buttons and check boxes to an applet.
The start() Method
The start() method is called immediately after the init() method is called and is executed each
time you visit other pages and return to the page containing the applet. You can use this method
when you want to restart a process each time a user visits a page. For example, you can use the
start() method in situations where you want to restart an animation sequence or a thread for
your applet. If your applet does not execute any statements when a user exits from the current
Web page, you need not implement this method.
The stop() Method
The stop() method is called each time an applet loses its focus. For example, when a user exits
out of the page on which the applet is loaded, the stop() method is called. You can use this
method to reset variables and stop the threads that are running. This method gives you a chance
to stop activities that slow down the computer.
The destroy() Method
The destroy() method is called when you start viewing another Web page. You can use this
method to perform clean-up operations, such as closing a file. Java calls the stop() method
before calling the destroy() method.
The update() Method
The update() method takes the Graphics class object as a parameter. When the applet area
needs to be redrawn, the Windows system starts the painting process. The update() method is
called to clear the screen and it in turn calls the paint() method. The system then updates the
screen.
The paint() Method
The paint() method draws the graphics of an applet in the drawing area. The method is
automatically called when an applet is displayed on the screen for the first time and each time
the applet receives focus. The paint() method can be triggered by invoking the repaint() method.
The paint() method of an applet takes an object of the Graphics class as a parameter.
The repaint() Method
You can call the repaint() method when you want to redraw an applet area. The repaint() method
calls the update() method to signal that an applet has to be updated. The default action of the
update() method is to clear the applet area and call the paint() method. You can override the
update() method if you do not want the applet area to be cleared.

Lecture/core/applet/22

Page #5

feel the Technology…
Core Java

Debasish Pratihari

getDocumentBase()
Although the getDocumentBase() method simply returns the URL of the document your applet is
embedded in, this URL comes in very handy with other methods. For example, the methods
discussed in the next two sections take a URL as one of their arguments. Instead of hard coding
a URL, you can use the getDocumentBase() method to pass the URL to any methods that require
a URL, such as getAudioClip() and getImage(), as in the following example:
graphic = getParameter("graphic");
clip = getParameter("clip");
image = getImage(getDocumentBase(), graphic);
sound = getAudioClip(getDocumentBase(), clip);
getAudioClip(URL, String)
The getAudioClip() method accepts a URL, which specifies the location of sound files on the
server, and a string to represent the name of the file. Keep in mind that you can use the
getDocumentBase() method to provide the URL, so if you move your applet, you don't have to
recode the getAudioClip() method. If you wanted to load an audio file called soundfile.au, you
could use the following code:
AudioClip clip;
clip = getAudioClip(getDocumentBase(), "soundfile.au");
This code just defines a variable called clip for the audio file and then makes clip equal to the
result of the getAudioClip() method. The getAudioClip() method uses the getDocumentBase()
method to supply the URL, and then you give the getAudioClip() method the name of the sound
file directly. You could also use a variable for the name of the sound file, which would make the
filename a little more flexible.
Audio methods are contained with the Applet Package within the AudioClip interface. An interface
is a specification the ensures that certain methods will be defined for a class. For example, the
AudioClip interface ensures that the getAudioClip(), play(), and loop() methods will be defined.
Table 10.2 summarizes the available audio methods.
Audio methods.
Method

Function

getAudioClip()

Loads an audio file from the server

play()

Plays the audio file once through

loop()

Plays the audio file in a continuous loop

stop()

Stops a play() or loop() method that is in progress

Lecture/core/applet/22

Page #6

feel the Technology…

More Related Content

PPTX
Applet progming
PPT
Slide8appletv2 091028110313-phpapp01
PPT
Basic of Applet
PPTX
Java applet
PPT
Java: Java Applets
PPTX
Java applet - java
PPT
Applet Architecture - Introducing Java Applets
PPT
Applets 101-fa06
Applet progming
Slide8appletv2 091028110313-phpapp01
Basic of Applet
Java applet
Java: Java Applets
Java applet - java
Applet Architecture - Introducing Java Applets
Applets 101-fa06

What's hot (20)

PDF
Java Applet and Graphics
PPT
first-applet
PPTX
PPTX
java Unit4 chapter1 applets
PPT
Applet and graphics programming
PPT
java applets
PPTX
PPTX
Applet
PDF
27 applet programming
PPT
Client Side Programming with Applet
PDF
Oop suplemnertary september 2019
PPTX
Applet (1)
PPTX
Applet programming
PPTX
6.applet programming in java
PPTX
Awt, Swing, Layout managers
PPT
Java applets
PPTX
Applets
PPTX
Java Applets
Java Applet and Graphics
first-applet
java Unit4 chapter1 applets
Applet and graphics programming
java applets
Applet
27 applet programming
Client Side Programming with Applet
Oop suplemnertary september 2019
Applet (1)
Applet programming
6.applet programming in java
Awt, Swing, Layout managers
Java applets
Applets
Java Applets
Ad

Viewers also liked (20)

PDF
PPT
Reviewing Screen Based Content: Demo Examples
PPT
Graffiti
PDF
網路行銷
PPT
Pelajaran 2 Bm
PPS
Have You Ever Noticed
PPTX
Hazel and Kate-Unison
KEY
111219 outsourcing
PDF
Presentatie avans1
PPTX
Tema ii
PPS
Niver Paula - 28.11.07
PPS
Niver Erica E Marcos - 22.10.07
PDF
2010 Fashion Show Booklet Revise
PDF
Delphi Scalper Review | Delphi Scalper Bonus
ZIP
Battle of luoisbourg keynote0
KEY
A History Of Salento Colombia
PPS
18.05.08 Acaosocial
PPS
La Mujer Ideal
PDF
Webbit 2004: Tiger, java
PPT
Dagaz Solutions Company Presentation
Reviewing Screen Based Content: Demo Examples
Graffiti
網路行銷
Pelajaran 2 Bm
Have You Ever Noticed
Hazel and Kate-Unison
111219 outsourcing
Presentatie avans1
Tema ii
Niver Paula - 28.11.07
Niver Erica E Marcos - 22.10.07
2010 Fashion Show Booklet Revise
Delphi Scalper Review | Delphi Scalper Bonus
Battle of luoisbourg keynote0
A History Of Salento Colombia
18.05.08 Acaosocial
La Mujer Ideal
Webbit 2004: Tiger, java
Dagaz Solutions Company Presentation
Ad

Similar to Lecture 22 (20)

PDF
Smart material - Unit 3 (2).pdf
PDF
Smart material - Unit 3 (1).pdf
PPTX
Applets in Java. Learn java program with applets
PPTX
Appletjava
PPT
Applets
PDF
Java applet basics
PPTX
oops with java modules iii & iv.pptx
PPTX
Applet and graphics programming
PPTX
Applet in java new
PPT
Java files and io streams
PPT
Unit 7 Java
PDF
Class notes(week 10) on applet programming
DOCX
Class notes(week 10) on applet programming
PPTX
MSBTE Computer Engineering Java applet.pptx
PPTX
Applets in Java
PPTX
Applet.pptx
PPT
Applets
PPTX
Java applet
PDF
6applets And Graphics
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (1).pdf
Applets in Java. Learn java program with applets
Appletjava
Applets
Java applet basics
oops with java modules iii & iv.pptx
Applet and graphics programming
Applet in java new
Java files and io streams
Unit 7 Java
Class notes(week 10) on applet programming
Class notes(week 10) on applet programming
MSBTE Computer Engineering Java applet.pptx
Applets in Java
Applet.pptx
Applets
Java applet
6applets And Graphics

More from Debasish Pratihari (20)

PDF
Lecture 24
PDF
Lecture 23
PDF
Lecture 21
PDF
Lecture 20
PDF
Lecture 19
PDF
Lecture 18
PDF
Lecture 17
PDF
Lecture 16
PDF
Lecture 14
PDF
Lecture 10
PDF
PDF
PDF
PDF
PDF
PDF
PDF
PDF
PDF
PDF

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Advanced methodologies resolving dimensionality complications for autism neur...
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”

Lecture 22

  • 1. Core Java Debasish Pratihari Applet:  Applets are small applications that are accessed on an internet server, transposed over the internet, automatically installed, and run as part of a web component.  Applets are Java programs that are embedded within a Web page. Therefore, unlike applications, applets require a Java-enabled browser, such as Microsoft Internet Explorer 4.0 or later, Netscape Navigator 4.0 or later, or HotJava. These browsers are said to be Javaenabled because they have a built-in Java platform (JVM and Java API).  An applet is loaded and executed when you load a Web page by using a Web browser. When a Web page containing an applet is displayed, you can interact with the applet. You can use applets to add dynamic features, such as animation and sound, to a Web page. You can also use them to make a Web page interactive.  After an applet arrives on the client, it has limited access to resources, so that it can produce a reliable user interface without introducing the risk of viruses. More About Applet :         Are sub-classes of Applet Class <applet> tag of html is used to load an applet in a web page. Are executed remotely by java-enabled browser. Do not have a main( ) method. Execution begins at init( ) method Are not independent objects like application Are purely GUI. It can access the resources of only the host computer; it cannot access the files on the computer on which it is downloaded. Lecture/core/applet/22 Page #1 feel the Technology…
  • 2. Core Java Debasish Pratihari The life cycle of an Applet :  init() is executed only once  start() Executed multiple times every time the focus comes backs to the page  stop() Executed multiple times every time The focus is lost from the web Page.  destroy() Is called only once when the page is terminated or closed.  An applet writes to its window only when its update() or paint() method is called by the AWT.  The fundamental architecture constraints imposed on an applet is that it must quickly return control to the AWT run-time system. So you shouldn’t create a loop in side paint().  The repaint() method is defined by the AWT. It causes the AWT run-time system to execute a call to your applet’s update( ) method, which in its default implementation, calls paint ( ).  The paint() method Called every time the window is resized or restored or Called forcibly by the user by calling repaint( ) method Lecture/core/applet/22 Page #2 feel the Technology…
  • 3. Core Java Debasish Pratihari Creating an Applet: import java.awt.* import java.appet.* sub-class the applet class over-ride methods as per need. Save the file with extension .java Compile to create .class file Create a HTML file Use applet tag to position the applet inside the page  <applet code = x width =300 height=200> </applet>  save the file with extension .html  open a browser and load the .html file         Attributes of Applet Tag: code : specifies the applet class file [codebase] : is an optional attribute that specifies the base URL of the Applet. height : specifies the height of applet width : specifies width of applet [align] : specifies alignment, the align must constant are LEFT, RIGHT, TEXTTOP, MIDDLE, ABSMIDDLE, BOTTOM, ABSBOTTOM, BASELINE [vspace] : specifies vertical space [Hspace] : specifies horizontal space [archieve] : specifies the jar file [Name] : specifies the alias name for the applet used for inter applet communication. Example : import java.awt.*; import java.applet.*; /*<applet code = myapplet width=300 height=400></applet>*/ public class Myapplet extends Applet{ public void paint(Graphics g){ g.drawString(“ hello every body”,10,20); } } Lecture/core/applet/22 Page #3 Note : You can test Applets using the Java Tool appletviewer. feel the Technology…
  • 4. Core Java Debasish Pratihari A summary of Methods in the Applet Package: Method Function public String getAppletInfo() public URL getDocumentBase() public String getParameter(String name) public String [][] getParameterInfo() public AudioClip getAudioClip(URL) Returns information about the applet, such as author Returns the URL of the HTML document Returns the parameters for an applet Returns a summary of what the parameters control Used to load an audio clip public Image getImage(URL) Used to load an image file Used to play a previously loaded audio clip Lets you know whether an applet is active public void play(URL) public boolean isActive() public void resize(int, int) Used to resize the applet public void showStatus(String msg) Displays a status string in the applet's browser public void init() Initializes the applet Starts the applet when it's finished initializing Stops the applet when you leave the applet's page Destroys the applet when you leave the browser public void start() public void stop() public void destroy() Difference between Application & Applet : Application Applet Are independent Dependent Are executed under the local o/s. Executed remotely by a browser. CUI or GUI GUI Execution starts with main( ) Starts with init( Terminates with main() Terminates only when the page is closed Lecture/core/applet/22 Page #4 ) method. feel the Technology…
  • 5. Core Java Debasish Pratihari Notes The Graphics Class The Graphics class is an abstract class that represents the display area of an applet. This class is a part of the java.awt package and you use it to draw images within the display area of the applet. The object of the Graphics class is used for painting the applet. The init() Method The init() method is called when an applet is loaded into the memory of a computer for the first time. The init() method works like a constructor, which means that it is executed automatically by the system. By using the init() method, you can initialize variables and add components such as buttons and check boxes to an applet. The start() Method The start() method is called immediately after the init() method is called and is executed each time you visit other pages and return to the page containing the applet. You can use this method when you want to restart a process each time a user visits a page. For example, you can use the start() method in situations where you want to restart an animation sequence or a thread for your applet. If your applet does not execute any statements when a user exits from the current Web page, you need not implement this method. The stop() Method The stop() method is called each time an applet loses its focus. For example, when a user exits out of the page on which the applet is loaded, the stop() method is called. You can use this method to reset variables and stop the threads that are running. This method gives you a chance to stop activities that slow down the computer. The destroy() Method The destroy() method is called when you start viewing another Web page. You can use this method to perform clean-up operations, such as closing a file. Java calls the stop() method before calling the destroy() method. The update() Method The update() method takes the Graphics class object as a parameter. When the applet area needs to be redrawn, the Windows system starts the painting process. The update() method is called to clear the screen and it in turn calls the paint() method. The system then updates the screen. The paint() Method The paint() method draws the graphics of an applet in the drawing area. The method is automatically called when an applet is displayed on the screen for the first time and each time the applet receives focus. The paint() method can be triggered by invoking the repaint() method. The paint() method of an applet takes an object of the Graphics class as a parameter. The repaint() Method You can call the repaint() method when you want to redraw an applet area. The repaint() method calls the update() method to signal that an applet has to be updated. The default action of the update() method is to clear the applet area and call the paint() method. You can override the update() method if you do not want the applet area to be cleared. Lecture/core/applet/22 Page #5 feel the Technology…
  • 6. Core Java Debasish Pratihari getDocumentBase() Although the getDocumentBase() method simply returns the URL of the document your applet is embedded in, this URL comes in very handy with other methods. For example, the methods discussed in the next two sections take a URL as one of their arguments. Instead of hard coding a URL, you can use the getDocumentBase() method to pass the URL to any methods that require a URL, such as getAudioClip() and getImage(), as in the following example: graphic = getParameter("graphic"); clip = getParameter("clip"); image = getImage(getDocumentBase(), graphic); sound = getAudioClip(getDocumentBase(), clip); getAudioClip(URL, String) The getAudioClip() method accepts a URL, which specifies the location of sound files on the server, and a string to represent the name of the file. Keep in mind that you can use the getDocumentBase() method to provide the URL, so if you move your applet, you don't have to recode the getAudioClip() method. If you wanted to load an audio file called soundfile.au, you could use the following code: AudioClip clip; clip = getAudioClip(getDocumentBase(), "soundfile.au"); This code just defines a variable called clip for the audio file and then makes clip equal to the result of the getAudioClip() method. The getAudioClip() method uses the getDocumentBase() method to supply the URL, and then you give the getAudioClip() method the name of the sound file directly. You could also use a variable for the name of the sound file, which would make the filename a little more flexible. Audio methods are contained with the Applet Package within the AudioClip interface. An interface is a specification the ensures that certain methods will be defined for a class. For example, the AudioClip interface ensures that the getAudioClip(), play(), and loop() methods will be defined. Table 10.2 summarizes the available audio methods. Audio methods. Method Function getAudioClip() Loads an audio file from the server play() Plays the audio file once through loop() Plays the audio file in a continuous loop stop() Stops a play() or loop() method that is in progress Lecture/core/applet/22 Page #6 feel the Technology…