Applets
Applets
Applet Fundamentals
• Another type of program is the applet.
• applets are small applications that are accessed on an
Internet server, transported over the Internet,
automatically installed, and run as part of a web
document.
• After an applet arrives on the client, it has limited
access to resources so that it can produce a graphical
user interface and run complex computations without
introducing the risk of viruses or breaching data
integrity.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
• This applet begins with two import statements. The first imports
the Abstract Window Toolkit (AWT) classes. Applets interact with
the user (either directly or indirectly) through the AWT, not
through the console-based I/O classes. The AWT contains support
for a window-based, graphical user interface. Applets can also use
Swing to provide the graphical user interface.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
• The second import statement imports the applet package, which
contains the class Applet. Every applet that you create must be a
subclass of Applet.
• Inside SimpleApplet, paint( ) is declared. This method is defined
by the AWT and must be overridden by the applet. paint( ) is
called each time that the applet must redisplay its output.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
• The paint( ) method has one parameter of type Graphics. This parameter contains
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.
• Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
This method outputs a string beginning at the specified X,Y location. It has the
following general form:
void drawString(String message, int x, int y)
Applet Architecture
• An applet is a window-based program. As such, its
architecture is different from the console-based programs.
• applets are event driven: An applet resembles a set of
interrupt service routines.
• An applet waits until an event occurs. The run-time
system 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.
Applet Architecture
the user initiates interaction with an applet:
• in a command based program, when the program needs input,
it will prompt the user and then call some input method, such
as readLine( ). –This does not happen in applets
• These interactions are sent to the applet as events to which the
applet must respond. For example, when the user clicks the
mouse inside the applet’s window, a mouse-clicked event is
generated. If the user presses a key while the applet’s window
has input focus, a keypress event is generated.
Simple Applet Display Methods
void drawString(String message, int x, int y)
void setBackground(Color newColor)
void setForeground(Color newColor)
setBackground(Color.green);
setForeground(Color.red);
Repainting
• As a general rule, an applet writes to its window only when its update( ) or paint( ) method
is called by the AWT.
• Whenever your applet needs to update the information displayed in its window, it simply
calls repaint( ).
• 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( ).
void repaint( )
void repaint(int left, int top, int width, int height)
void repaint(long maxDelay)
void repaint(long maxDelay, int x, int y, int width, int height)
• the coordinates of the upper-left corner of the region are specified by left and top, and the
width and height of the region are passed in width and height. These dimensions are
specified in pixels.
• maxDelay specifies the maximum number of milliseconds that can elapse before update( ) is
called
Event Handling
The Delegation Event Model
• a source generates an event and sends it to one or more listeners
• the listener simply waits until it receives an event
• the listener processes the event and then returns
The advantage of this design is that the application logic that
processes events is cleanly separated from the user interface logic
that generates those events. Auser interface element is able to
“delegate” the processing of an event to a separate piece of code.
Events
• an event is an object that describes a state change in a source.
• Some of the activities that cause events to be generated are:
• pressing a button
• entering a character via the keyboard
• selecting an item in a list
• clicking the mouse, etc.
• Events may also occur that are not directly caused by
interactions with a user interface.
• an event may be generated when a timer expires
• a counter exceeds a value
• a software or hardware failure occurs
• an operation is completed, etc.
Event Sources
• A source is an object that generates an event.
• This occurs when the internal state of that object changes in some way.
• Sources may generate more than one type of event.
• A source must register listeners in order for the listeners to receive notifications
about a specific type of event. Each type of event has its own registration method.
Here is the general form:
public void addTypeListener(TypeListener el)
• Here, Type is the name of the event, and el is a reference to the event listener.
• For example,
• The method that registers a keyboard event listener is called
addKeyListener().
• The method that registers a mouse motion listener is called
addMouseMotionListener().
Event Listeners
• A listener is an object that is notified when an event occurs.
• It has two major requirements.
• First, it must have been registered with one or more sources to
receive notifications about specific types of events.
• Second, it must implement methods to receive and process these
notifications.
• The methods that receive and process events are defined in a set of
interfaces found in java.awt.event. For example, the
MouseMotionListener interface defines two methods to receive
notifications when the mouse is dragged or moved.
Event Classes
• The classes that represent events are at the core of Java’s event handling
mechanism.
• At the root of the Java event class hierarchy is EventObject, which is in
java.util. It is the superclass for all events. Its one constructor is shown
here:
EventObject(Object src)
• Here, src is the object that generates this event.
• EventObject contains two methods: getSource( ) and toString( ).
• The getSource( ) method returns the source of the event. Its general
form is shown here:
Object getSource( )
• toString( ) returns the string equivalent of the event.
Sources of Events
Event Listener Interfaces
• As explained, the delegation event model has two parts: sources
and listeners.
• Listeners are created by implementing one or more of the
interfaces defined by the java.awt.event package.
• When an event occurs, the event source invokes the appropriate
method defined by the listener and provides an event object as
its argument.
List of
commonly
used
listener
interfaces
and a brief
description