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
VIKRANTHMALLIKARJUN
 
PPT
Slide8appletv2 091028110313-phpapp01
Abhishek Khune
 
PPT
Basic of Applet
suraj pandey
 
PPTX
Java applet
GaneshKumarKanthiah
 
PPT
Java: Java Applets
Tareq Hasan
 
PPTX
Java applet - java
Rubaya Mim
 
PPT
Applet Architecture - Introducing Java Applets
amitksaha
 
PPT
Applets 101-fa06
nrayan
 
Applet progming
VIKRANTHMALLIKARJUN
 
Slide8appletv2 091028110313-phpapp01
Abhishek Khune
 
Basic of Applet
suraj pandey
 
Java applet
GaneshKumarKanthiah
 
Java: Java Applets
Tareq Hasan
 
Java applet - java
Rubaya Mim
 
Applet Architecture - Introducing Java Applets
amitksaha
 
Applets 101-fa06
nrayan
 

What's hot (20)

PDF
Java Applet and Graphics
Abdul Rahman Sherzad
 
PPT
first-applet
Mohit Patodia
 
PPTX
Applet
Priyanka Pradhan
 
PPTX
java Unit4 chapter1 applets
raksharao
 
PPT
Applet and graphics programming
mcanotes
 
PPT
java applets
Waheed Warraich
 
PPTX
Applet life cycle
V.V.Vanniapermal College for Women
 
PPTX
Applet
swapnac12
 
PDF
27 applet programming
Ravindra Rathore
 
PPT
Client Side Programming with Applet
backdoor
 
PDF
Oop suplemnertary september 2019
ktuonlinenotes
 
PPTX
Applet (1)
DEEPIKA T
 
PPTX
Applet programming
Devyani Vaidya
 
PPTX
6.applet programming in java
Deepak Sharma
 
PPTX
Awt, Swing, Layout managers
swapnac12
 
PPT
Java applets
Khan Mac-arther
 
PPTX
Applets
Nuha Noor
 
PPTX
Java Applets
Danial Mirza
 
Java Applet and Graphics
Abdul Rahman Sherzad
 
first-applet
Mohit Patodia
 
java Unit4 chapter1 applets
raksharao
 
Applet and graphics programming
mcanotes
 
java applets
Waheed Warraich
 
Applet
swapnac12
 
27 applet programming
Ravindra Rathore
 
Client Side Programming with Applet
backdoor
 
Oop suplemnertary september 2019
ktuonlinenotes
 
Applet (1)
DEEPIKA T
 
Applet programming
Devyani Vaidya
 
6.applet programming in java
Deepak Sharma
 
Awt, Swing, Layout managers
swapnac12
 
Java applets
Khan Mac-arther
 
Applets
Nuha Noor
 
Java Applets
Danial Mirza
 
Ad

Viewers also liked (20)

PDF
Microcamp
Marco Magnocavallo
 
PPT
Reviewing Screen Based Content: Demo Examples
Rhonda Bracey
 
PPT
Graffiti
michellchd
 
PDF
網路行銷
omeganet001
 
PPT
Pelajaran 2 Bm
amoi286
 
PPS
Have You Ever Noticed
Maher R. Boktor
 
PPTX
Hazel and Kate-Unison
Paul McElvaney
 
KEY
111219 outsourcing
Rodrigo Sepúlveda Schulz
 
PDF
Presentatie avans1
Sjef Kerkhofs
 
PPTX
Tema ii
cubs2000
 
PPS
Niver Paula - 28.11.07
Jubrac Jacui
 
PPS
Niver Erica E Marcos - 22.10.07
Jubrac Jacui
 
PDF
2010 Fashion Show Booklet Revise
Ng Lim
 
PDF
Delphi Scalper Review | Delphi Scalper Bonus
Coty Schwabe
 
ZIP
Battle of luoisbourg keynote0
iamcanehdian
 
KEY
A History Of Salento Colombia
Sergio Pino
 
PPS
18.05.08 Acaosocial
Jubrac Jacui
 
PPS
La Mujer Ideal
Francisco Martinez
 
PDF
Webbit 2004: Tiger, java
Matteo Baccan
 
PPT
Dagaz Solutions Company Presentation
Doug de Urioste
 
Reviewing Screen Based Content: Demo Examples
Rhonda Bracey
 
Graffiti
michellchd
 
網路行銷
omeganet001
 
Pelajaran 2 Bm
amoi286
 
Have You Ever Noticed
Maher R. Boktor
 
Hazel and Kate-Unison
Paul McElvaney
 
111219 outsourcing
Rodrigo Sepúlveda Schulz
 
Presentatie avans1
Sjef Kerkhofs
 
Tema ii
cubs2000
 
Niver Paula - 28.11.07
Jubrac Jacui
 
Niver Erica E Marcos - 22.10.07
Jubrac Jacui
 
2010 Fashion Show Booklet Revise
Ng Lim
 
Delphi Scalper Review | Delphi Scalper Bonus
Coty Schwabe
 
Battle of luoisbourg keynote0
iamcanehdian
 
A History Of Salento Colombia
Sergio Pino
 
18.05.08 Acaosocial
Jubrac Jacui
 
La Mujer Ideal
Francisco Martinez
 
Webbit 2004: Tiger, java
Matteo Baccan
 
Dagaz Solutions Company Presentation
Doug de Urioste
 
Ad

Similar to Lecture 22 (20)

PPT
Applets
Abhishek Khune
 
PPT
Applets
Inayat Sharma
 
PPTX
Appletjava
DEEPIKA T
 
PPTX
Applet in java new
Kavitha713564
 
PDF
6applets And Graphics
Adil Jafri
 
PPTX
L18 applets
teach4uin
 
PPTX
Applet intro
Nitin Birari
 
PPTX
Introduction To Applets methods and simple examples
MsPariyalNituLaxman
 
PPT
Applets
SanthiNivas
 
PPT
Advanced Programming, Java Programming, Applets.ppt
miki304759
 
PPT
Applets(1)cusdhsiohisdhfshihfsihfohf.ppt
Vijay Bhaskar Thatty
 
PPT
Appletsbjhbjiibibibikbibibjibjbibbjb.ppt
Vijay Bhaskar Thatty
 
PPT
java programming - applets
HarshithaAllu
 
PPTX
oops with java modules iii & iv.pptx
rani marri
 
PPT
Java files and io streams
RubaNagarajan
 
PPT
Applets
poojapainter
 
PPTX
Applet Programming in Advance Java Programming
jayshah562401
 
PPTX
Java applet
Elizabeth alexander
 
PPTX
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
PPTX
Till applet skeleton
SouvikKole
 
Applets
Inayat Sharma
 
Appletjava
DEEPIKA T
 
Applet in java new
Kavitha713564
 
6applets And Graphics
Adil Jafri
 
L18 applets
teach4uin
 
Applet intro
Nitin Birari
 
Introduction To Applets methods and simple examples
MsPariyalNituLaxman
 
Applets
SanthiNivas
 
Advanced Programming, Java Programming, Applets.ppt
miki304759
 
Applets(1)cusdhsiohisdhfshihfsihfohf.ppt
Vijay Bhaskar Thatty
 
Appletsbjhbjiibibibikbibibjibjbibbjb.ppt
Vijay Bhaskar Thatty
 
java programming - applets
HarshithaAllu
 
oops with java modules iii & iv.pptx
rani marri
 
Java files and io streams
RubaNagarajan
 
Applets
poojapainter
 
Applet Programming in Advance Java Programming
jayshah562401
 
Java applet
Elizabeth alexander
 
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
Till applet skeleton
SouvikKole
 

More from Debasish Pratihari (20)

PDF
Lecture 24
Debasish Pratihari
 
PDF
Lecture 23
Debasish Pratihari
 
PDF
Lecture 21
Debasish Pratihari
 
PDF
Lecture 20
Debasish Pratihari
 
PDF
Lecture 19
Debasish Pratihari
 
PDF
Lecture 18
Debasish Pratihari
 
PDF
Lecture 17
Debasish Pratihari
 
PDF
Lecture 16
Debasish Pratihari
 
PDF
Lecture 14
Debasish Pratihari
 
PDF
Lecture 10
Debasish Pratihari
 
PDF
Lecture 9
Debasish Pratihari
 
PDF
Lecture 8
Debasish Pratihari
 
PDF
Lecture 7
Debasish Pratihari
 
PDF
Lecture 6
Debasish Pratihari
 
PDF
Lecture 5
Debasish Pratihari
 
PDF
Lecture 4
Debasish Pratihari
 
PDF
Lecture 3
Debasish Pratihari
 
PDF
Lecture 2
Debasish Pratihari
 
PDF
Lecture 1
Debasish Pratihari
 
PDF
Lecture25
Debasish Pratihari
 

Recently uploaded (20)

PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
The Future of Artificial Intelligence (AI)
Mukul
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Doc9.....................................
SofiaCollazos
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 

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…