Home E-Books Special Effects and Game Development in Java To capture the mouse pointer 

Special Effects and Game Development in Java(TM) - To capture the behaviour of the mouse pointer 

by Anibal Wainstein

5.0 To capture the behaviour of the mouse pointer

Interaction is a nice word that is about the user's possibilities to affect and control a program. In applets, this is often done with the mouse pointer. The operating system works like this: when you do different things with the mouse pointer, it sends a message to the Java applet about what you are doing. For instance, it could be when you move the mouse pointer, click, drag it, and so on. All these messages are only sent if you point within the applet screen. But how does this really work and how does these messages look like? In Java 1.02 the following functions are used to capture mouse messages.


mouseEnter() Is called when the user enters the applet screen area
mouseExit() Is called when the user exits the applet screen area
mouseMove() Is called when the user moves the mouse
mouseDrag() Is called when the user clicks and at the same time moves the mouse
mouseDown() Is called when the user clicks and holds down the mouse button.
mouseUp() Is called when the user let's go of the mouse button

 

The functions mentioned above are written by the developer in his Java applet. When something happens with the mouse pointer, then these functions are automatically called by the Java Virtual Machine. In these functions you can write any code you want. However, the arguments and the names of the functions must be exactly as the ones above in order for the Java Virtual Machine to recognize them. A Message in Java 1.02 are is in fact a special object that is created by the Java engine and sent to the applet. The object is based on the Event-class which can be filled with information about almost anything that's happening with the operating system. An event can happen for instance when you click on the mouse button, press a key or minimize a window. However, in this chapter I will only review messages related to the mouse pointer.

5.0.1 How your applet knows when the mouse pointer enters and exits the applet area (mouseEnter() and mouseExit())

There are two methos that detects you moving the mouse pointer across the applet area or the applet screen. These methods already exist in the Applet class but contains no code. By overwriting them you can add functionality that is executed by these events:

public boolean mouseEnter(Event e, int x, int y)
{
    //Here you add the code that you want the appleten
    //to execute when the mouse pointer enters the applet area
return true; } public boolean mouseExit(Event e, int x, int y) { //Here you add the code that you want the applet
//to execute when the mouse pointer exits the applet area
return true; }

Both functions return a boolean value. The value will always be true if you treat the event in the code. This doesn't matter much in Java applets, but in other Java objects it could be very important that the Java Virtual Machine knows if a method has treated the event or not. The Event-object that is sent contains the information about when the event happened, among other things. The position variables "x" and "y" tells you where the mouse pointer entered. Let's take an example. We will begin with an applet with a picture that reacts when you move the mouse pointer across it. We start by writing the init() method and by declaring the variables:


//The variable "currentimage" is referencing the image that 
//is being displayed right now
. Image currentimage; //The variable "normalimage" is referencing the image that //is displayed when the mouse pointer is outside the applet screen Image normalimage; //The variable "mouseoverimage" is referencing the image that is displayed //when the mouse pointer is inside the applet screen Image mouseoverimage; public void init() { //We must always have the habit about using the tracker. //It makes the applet look better. MediaTracker tracker=new MediaTracker(this); normalimage=getImage(getDocumentBase(),"normal.jpg"); //"normalimage" will be number one in the tracker's //loading list. tracker.addImage(normalimage,0); mouseoverimage=getImage(getDocumentBase(),"mouseover.jpg"); tracker.addImage(mouseoverimage,1); //At the beginning, "currentimage" is set to "normalimage" //Later in paint() "currentimage" will be used to draw //the picture currentimage=normalimage; //Finally we make sure that the tracker //loads the images. try {tracker.waitForAll();} catch(InterruptedException e) { System.out.println("Something stopped loading our images..."); } } public void paint(Graphics g) { //Draw the "current" image. g.drawImage(currentimage,0,0,this); }

The images are loaded in the init() method. There are three reference variables, "currentimage", "normalimage" and "mouseoverimage". The first one references that image that will be drawn in the paint() metoden. The last two are used to load and store the two images that will be displayed. The variable "currentimage" will at the beginning reference the normal image that looks like this:

The picture "normal.jpg" which is displayed when the mouse pointer is outside the applet screen.

This means we assume that the mouse pointer is outside the applet area when the applet starts. Note that this time we haven't written the keyword "synchronized" on the paint() method. It is not needed since we are not using threads right now. Look now on the following methods:


public boolean mouseEnter(Event e, int x, int y)
{
    //If the mouse pointer enters the applet, then we
    //set "currentimage" to "mouseoverimage" so that the
    //next time paint() is called, the mouseover picture
    //is drawn instead of the normal picture.
    currentimage=mouseoverimage;

    //The screen won't update itself only because 
    //the mouse pointer entered the applet area.
    //So we must make sure that it does using the 
    //update() method.
    update(getGraphics());
    return true;
}

public boolean mouseExit(Event e, int x, int y)
{
    //When the mouse pointer exits the area again,
    //we restore "currentimage" so that it references
    //"normalimage" again.
    currentimage=normalimage;
    update(getGraphics());
    return true;
}

When the mouse pointer enters the applet area then the mouseEnter() method is called and the "currentimage" variable is set to reference the "mouseoverimage". The screen is updated with the update() method and the following picture is displayed:

The picture "mouseover.jpg" that is displayed when the mouse pointer is inside the applet screen.

When the mouse pointer exits the applet screen, then the mouseExit() method is called and the old image will appear again. Click here to see the example. You are maybe wondering why we didn't use any double buffering in the applet? Well, the "Animation" in this case did not lead to any animation flimmering, so it really wasn't needed.

 

Next Page >>