0% found this document useful (0 votes)
27 views6 pages

Lecture 12 Applete

The document discusses event handling in Java, classifying events into foreground and background types, and explaining the Delegation Event Model involving sources and listeners. It also covers Java Applets, their lifecycle, and key characteristics, noting that applets are no longer widely used due to deprecation in Java 9. The lifecycle methods of applets, including init(), start(), paint(), stop(), and destroy(), are explained in detail.

Uploaded by

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

Lecture 12 Applete

The document discusses event handling in Java, classifying events into foreground and background types, and explaining the Delegation Event Model involving sources and listeners. It also covers Java Applets, their lifecycle, and key characteristics, noting that applets are no longer widely used due to deprecation in Java 9. The lifecycle methods of applets, including init(), start(), paint(), stop(), and destroy(), are explained in detail.

Uploaded by

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

Lecture-12-Applete

Event Handling in Java


An event is a change in the state of an object triggered by some action such as Clicking a button,
Moving the cursor, Pressing a key on the keyboard, Scrolling a page, etc. In Java,
the java.awt.event package provides various event classes to handle these actions.

Classification of Events

Events in Java can be broadly classified into two categories based on how they are generated:
1. Foreground Events: Foreground events are the events that require user interaction to generate.
Examples of these events include Button clicks, Scrolling the scrollbar, Moving the cursor, etc.
2. Background Events: Events that don’t require interactions of users to generate are known as
background events. Examples of these events are operating system failures/interrupts, operation
completion, etc.

Event Handling Mechanism


Event handling is a mechanism that allows programs to control events and define what should
happen when an event occurs. Java uses the Delegation Event Model to handle events. This model
consists of two main components:
Lecture-12-Applete
 Source: Events are generated from the source. There are various sources like buttons,
checkboxes, list, menu-item, choice, scrollbar, text components, windows, etc., to generate
events.
 Listeners: Listeners are used for handling the events generated from the source. Each of these
listeners represents interfaces that are responsible for handling events.
Lecture-12-Applete
Java Applet Basics
Java Applets was once a very popular feature of web applications. Java Applets were small programs
written in Java that ran inside a web browser. Learning about Applet helps us understand how Java has
evolved and how it handles graphics.

Note: java.applet package has been deprecated in Java 9 and later versions, as applets are no longer
widely used on the web.

Java Applets
A Java Applet is a Java program that runs inside a web browser. An Applet is embedded in an HTML
file using <applet> or <objects> tags. Applets are used to make the website more dynamic and
entertaining. Applets are executed in a sandbox for security, restricting access to local system
resources.
Key Points:
 Applet Basics: Every applet is a child/subclass of the java.applet.Applet class.
 Not Standalone: Applets don’t run on their own like regular Java programs. They need a web
browser or a special tool called the applet viewer (which comes with Java).
 No main() Method: Applets don’t start with main() method.
 Display Output: Applets don’t use System.out.prinln() for displaying the output, instead they use
graphics methods like drawString() from the AWT (Abstract Window ToolKit).
Lecture-12-Applete

Java Applet Life Cycle


The below diagram demonstrates the life cycle of Java Applet:

It is important to understand the order in which the various methods shown in the above
image are called.
 When an applet begins, the following methods are called, in this sequence:
o init( )
o start( )
o paint( )
 When an applet is terminated, the following sequence of method calls takes place:
o stop( )
o destroy( )
Lecture-12-Applete
Let’s look more closely at these methods.
1. 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.
2. start( ): The start( ) method is called after init( ). It is also called to restart an applet after
it has been stopped.
Note: init( ) is called once i.e. when the first time an applet is loaded whereas 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 applet resumes execution at start( )
3. paint( ): The paint( ) method is called each time an AWT-based 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. Whatever the cause, whenever
the applet must redraw its output, paint( ) is called.
 The paint( ) method has one parameter of type Graphics. This parameter will contain the
graphics context, which describes the graphics environment in which the applet is
running. This context is used whenever output to the applet is required.
 paint() is the only method among all the methods mention above (which is
parameterized).
This method is crucial for updating or redrawing the visual content of the applet.

This method is crucial for updating or redrawing the visual content of the applet.
Example:
public void paint(Graphics g)
{
// Drawing a string on the applet window
// g is an object reference of class Graphic.
g.drawString(“Hello, Applet!”, 50, 50);}
Lecture-12-Applete

4. stop( ): The stop( ) method is called when a web browser leaves the HTML document
containing the applet, when it goes to another page.
For example: 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 returns to the page.

5. 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( ).

You might also like