0% found this document useful (0 votes)
32 views24 pages

Understanding Java Applet Class Basics

The document provides an overview of the Applet class in Java, detailing its methods, execution process, and interaction with users through events. It explains how applets are executed within a web browser using HTML and the APPLET tag, as well as the lifecycle methods such as init(), start(), stop(), and destroy(). Additionally, it covers the graphical output methods and color settings available for applets using the AWT framework.

Uploaded by

saikumarvenkat60
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views24 pages

Understanding Java Applet Class Basics

The document provides an overview of the Applet class in Java, detailing its methods, execution process, and interaction with users through events. It explains how applets are executed within a web browser using HTML and the APPLET tag, as well as the lifecycle methods such as init(), start(), stop(), and destroy(). Additionally, it covers the graphical output methods and color settings available for applets using the AWT framework.

Uploaded by

saikumarvenkat60
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

THE APPLET CLASS

RS NANDHINI
INTRODUCTION
• The applet class is contained in the java.applet package.
• Applet contains several methods that gives you detailed control over the
execution of your applet.
• In addition, java.applet also defines three interfaces:
• AppletContext
• AudioClip
• AppletStub
APPLETS
• All the applets are subclasses of Applet.
• Thus, all the applets must import java.applet.
• Applets must also import java.awt.
• AWT stands for Abstract Window Toolkit.
• Since, all the applets run in a window, it is necessary to include support for the
window.
• Applets are not executed by console-based Java run-time interpreter.
• Rather, they are executed by either a web browser or an applet viewer.
EXECUTION
• Execution of an applet does not begin at main().
• Instead, execution of an applet is started and controlled with an entirely different
mechanism.
• Output to your applet window is not performed by System.out.println().
• Rather, it is handled with various AWT methods, such as drawString(), which
outputs a string to a specified X,Y location.
• Input is also handled differently than in an application.
EXECUTION
• Once an applet has been compiled, it is included in an HTML file using the APPLET
tag.
• The applet will be executed by a Java-enabled web browser when it encounters
the APPLET tag within the HTML file.
• To view and test an applet more conveniently, simply include a comment at the
head of your Java source code file that contains the APPLET tag.
APPLET TAG

/*
<applet code = “MyApplet” width=200 height=60>
</applet>
*/
The APPLET Class
• Applet provides all necessary support for applet execution, such as starting and
stopping.
• It also provides methods that load and display images, and methods that load and
play audio clips.
• Applets extends the AWT class Panel. In turn, Panel extends Container, which
extends Component.
• These classes provide support for Java’s window-based, graphical interface.
• Thus, Applet provides all of the necessary support for window-based activities.
Method Description
void destroy() Called by the browser just before an applet is terminated. Your
applet will override this method if it needs to perform any
cleanup prior to its destruction.

AccessibleContext Returns the accessibility context for the invoking object.


getAccessibleContext()
AppletContext – getAppletContext() Returns the context associated with the applet.
String getAppletInfo() Returns a string that describes the applet.
AudioClip getAudioClip(URL url) Returns an AudioClip object that encapsulates the audio clip
found at the location specified by the URL.
AudioClip getAudioClip(URL url, String clipName) Returns an AudioClip object that encapsulates the audio clip
found at the location specified by the URL and having the name
specified by clipName.

URL getCodeBase() Returns the URL associated with the invoking applet.
URL getDocumentBase() Returns the URL of the HTML document that invokes the
applet.
Image getImage(URL url) Returns an Image object that encapsulates the image found at
the location specified by url.
Image getImage(URL url, String imageName) Returns an Image object that encapsulates the image found at
the location specified by url and having the name specified by
imageName.
METHOD DESCRIPTION

void init() Called when an applet begins execution. It is the first method
called for any applet.

void play(URL url) If an audio clip is found at the location specified by the url, then
it is played.

void resize(int width, int height) Resizes the applet according to the dimensions specified by
width and height

void start() Called by the browser when an applet should start (or resume)
execution. It is automatically called after init() when an applet
first begins.

void stop() Called by the browser to suspend execution of the applet. Once
stopped, an applet is restarted when the browser calls start().
APPLET ARCHITECTURE
• An applet is a window-based program.
• As such, its architecture is different from the so-called normal, console-
based programs.
• Applets are event driven.
• An applet resembles a set of interrupt service routines. An applet waits until
an event occurs.
• The AWT notifies the applet about an event by calling an event handler that
has been provided by the applet.
• Once this happens, the applet must take appropriate action and then quickly
return the control to the AWT.
APPLET INTERACTION
• The user initiates interaction with an applet – not the other way around.
• As you know in standalone programs – when the program needs the input, it will
prompt the user and then call some input method (readLine()).
• Instead, the user interacts with the applet whenever and however it is needed.
• These interactions are sent to the applet as events to which the applet must
respond.
• Example: when the user clicks a mouse inside the applet’s window, a mouse
clicked event is generated.
An Applet skeleton
• Four methods – init(), start(), stop() and destroy() are defined by the applet.
• Another paint(), is defined by the AWT Component class.
• Default implementations for all these methods are defined.
• Applets do not need to override those methods they do not use.
• An example below shows these five methods being assembled into the skeleton
APPLET INITIALIZATION AND
TERMINATION
• init()
• start()
• paint()
• stop()
• destroy()
init()
• The init() method is the first method to be called.
• This is where you should initialize variables.
• This method is called only once during the run time of your applet.
start()
• The start() method is called after init().
• It is also called to restart an applet after it has been stopped.
• Whereas init() is called once – the first time an applet is loaded – start() is called
each time an applet’s HTML document is displayed onscreen.
• So, if a user leaves a web page and comes back, the resumes execution at start().
paint ()
• The paint() method is called each time your applet’s output must be
redrawn.
• This situation can occur for several reasons.
• For example: the window in which the applet is running may be overwritten
by another window and then uncovered.
• Or the applet window may be minimized and then restored.
• paint() is also called when the applet begins execution.
• The paint() method has one parameter of type Graphics.
• This parameter will contain the graphics context, which describes the
graphics environment in which applet is running.
stop()
• The stop() method is called when a web browser leaves the HTML
document containing the applet – when it goes to another page.
• When stop() is called, the applet is probably running.
• You should use stop() to suspend threads that don’t need to run when the
applet is not visible.
• You can restart them when start() is called if the user return to the page.
destroy()
• The destroy() method is called when the environment determines that your
applet needs to be removed completely from memory.
• At this point, you should free up any resources the applet may be using.
• The stop() method is always called before destroy().
Overriding update()
• Your applet may need to override another method defined by the AWT, called
update().
• This method is called when your applet has requested that a portion of its
window be redrawn.
• The default version of update() simply calls paint().
• However, you can override the update() method so that it performs more subtle
repainting.
Simple Applet Display Methods
• Applets are displayed in a window, and they use the AWT to perform input and
output.
• To output a string to an applet, we use drawString(), which is a member of the
Graphics class.
• Typically, it is called from within either update() or paint().
• void drawString(String message, int x, int y)
• Here, message is the string to be output beginning at x,y. (In Java window, upper-left
corner is location 0,0)
• To set the background color of an applet’s window, use setBackground().
• To set the foreground color(the color in which the text is shown) use
setForeground().
• These methods are defined by Component, and they have the following general
forms:
• void setBackground(Color newColor)
• void setForeground(Color newColor)
COLORS
Color.black Color.magenta
Color.blue Color.orange
Color.cyan Color.pink
Color.drakGray Color.red
Color.gray Color.white
Color.green Color.yellow
Color.lightGray

• The following example sets the background color to green and the text color to red.
• setBackground(Color.green);
• setForeground(Color.red);

You might also like