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…