Using the Mouse in Applets

Overview

The mouse is a very useful user-interface for interacting with your application. If we wish to write an application that interacts with the mouse we need to deal with the topic of mouse events. You can think of events like the word describes - something that occurs as time passes. When we talk about "mouse events" we can think of this as an event that occurs with the mouse, such as the left button being pressed, or the mouse moving etc. To get our application to respond to a particular mouse event we add the code to our application that performs the functionality that we require for that particular event. This should become a little clearer with an example:

A Mouse Event Example

Here is an example of using the Mouse in an applet. This applet assumes that the sound file "start.wav" is in the same directory as the applet class file "MouseApplet.class". When you run the applet it will look like Figure 8.2, “Mouse Audio Applet”, which is not that informative. You really need to run the applet to hear it working correctly.

Figure 8.2. Mouse Audio Applet

Mouse Audio Applet

The source code is below and in the MouseApplet.java. You can hear the applet running corretly at MouseApplet.html. I have used a very quiet sound, so turn your volume up a little.


  // An Outline Application for Handling Mouse Events

  import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;

  public class MouseApplet extends Applet implements MouseListener
  {
	
	private AudioClip theSound;
	
	public void init()
	{
	  this.theSound = this.getAudioClip( this.getDocumentBase(), 
	                                     "start.wav" );
	  this.addMouseListener(this);
	}
	
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) 
	{ 
		this.theSound.play();
	}
  }

This applet does not do much, but it is a framework for developing applications that use mouse events. This applet uses the interface MouseListener, by using the keyword implements. When we use an interface in our code we are required to write code for the methods defined in that interface. In this case we are using the interface MouseListener which has five methods that must be implemented mousePressed(), mouseReleased(), mouseEntered(), mouseExited() and mouseClicked(). As you can see in the code segment above, I only really required mouseClicked() as I only wanted a sound to play when the mouse was clicked. However, I was required to write code for the four other methods and I did, by writing blank functionality for those methods - so nothing will occur when the other four events occur.

As well as writing the code for the events that occur, we also have to "turn on" Mouse Listening. We do this by using the line of code this.addMouseListener(this);. Now this is a little complex, but what we are doing is stating "To this MouseApplet object turn on Mouse Listening - and if a mouse event occurs then send it to this MouseApplet object". Our applet is now ready to listen for events and pass them to its own five mouse methods.

Finally - one small point. At the top of the code I wrote import java.awt.*; and I also wrote import java.awt.event.*;. This may seem unnecessary but it is not. We need the MouseEvent class from the java.awt.event package for our code, but does the import java.awt.*; not also import the classes in the java.awt.event package. No it does not - the * is not recursive and so does not import sub packages (sub directories).