Java Reference
In-Depth Information
Sample output from this program is shown here:
Let's look closely at this example. The
MouseEvents
class extends
Applet
and imple-
ments both the
MouseListener
and
MouseMotionListener
interfaces. These two inter-
faces contain methods that receive and process the various types of mouse events. Notice
that the applet is both the source and the listener for these events. This works because
Component
, which supplies the
addMouseListener( )
and
addMouseMotionListener( )
methods, is a superclass of
Applet
. Being both the source and the listener for events is a
common situation for applets.
Inside
init( )
, the applet registers itself as a listener for mouse events. This is done by us-
ing
addMouseListener( )
and
addMouseMotionListener( )
, which are members of
Com-
ponent
. They are shown here:
void addMouseListener(MouseListener
ml
)
void addMouseMotionListener(MouseMotionListener
mml
)
Here,
ml
is a reference to the object receiving mouse events, and
mml
is a reference to the
object receiving mouse motion events. In this program, the same object is used for both.