Event handling in applets Handle mouse events

Event handling in applets

Handle mouse events

I think there is no game that doesn't "interact" with the player. In our case, as a programmer, you have two possibilities to allow the player to interact with the computer: the player has to use the mouse or the keyboard. In the next two chapters I want to tell you how to handle mouse and keyboard events in an applet. Again this is much more easier to do in an applet as in a real application programmed for example using Java Swing. Also you won't be able to use the things you will learn here about event handling in an application, because event handling is very different in applications. But every good book about Java tells you how to handle events in applications so I think it will be no problem for you to learn it by yourself if you need to.
Now I want to change our "ball movement" applet in that way, that the ball changes the direction if the user clicks on the applet.

 

The Unchained Tour offers the best Java training, and if you’re from Chennai make sure to check them out for your Java requirements

To do this you have to implement just one method in our Main class called mouseDown. This method should look like this:

// Method to handle mouse down events
public boolean mouseDown (Event e, int x, int y)
{

// Change direction
x_speed = - (x_speed);

// DON'T FORGET (although not necessary here)!!
return true;

}

Of course you can handle other events than the mouseDown event. Java makes it possible to overwrite the following methods the same way as I did above and handle other mouse events that way. Here comes a list of the methods:

  1. Mouse click events:
    1. public boolean mouseDown (Event e, int x, int y): handles events that occur if mouse button is pressed down
    2. public boolean mouseUp (Event e, int x, int y): handles events that occur if mouse button is released again.
    3. Use the variable e.clickCount to get the number of clicks performed. That's how you can handle for example double clicks! You'll see a example for this in chapter 5 ("Our first game").
  2. Mouse movement:
    1. public boolean mouseMove (Event e, int x, int y): handles events that occur if mouse is moved over the applet
    2. public boolean mouseDrag (Event e, int x, int y): handles events that occur if mouse is moved over the applet with pressed mouse button
  1. Mouse position:
    1. public boolean mouseEnter(Event e, int x, int y): handles events that occur if mouse enters the applet
    2. public boolean mouseExit (Event e, int x, int y): handles events that occur if mouse leaves the applet

With help of the variables int x and int y you can get the coordinates where the event happend, which is very important for many games. Also you have to take care of the return statement "return true;" that has often no meaning for the funktionality of your game but is expected by the method.


Event handling of key events

 
 

 

Handling key events in applets works very similar to mouse event handling, I talked about in the chapter before. There is one special method for every possible event (key down, key up). You have to overwrite the corrosponding method, if you want to handle, for example, a key down - event. In this chapter we'll have a short look at these two methods. Afterwards we will change our "moving ball" applet that way, that one can change the direction of the ball movement by pressing left or right cursor key and stop ball movement by pressing the space bar. Be carefull!! An applet is only able to handle key events once you have clicked in the applet window!!!
Here comes the explanation of the methods:

  1. public boolean keyDown (Event e, int key): This method listens to events that occur if a key is pressed down.

Every key has a value (ASCII). This value is given to the method with help of the "key" variable. Space bar has value 32. If you want your applet to listen to Space bar pressed down, you only have to test, if the value of "key" is 32 (details later). Sometimes you don't know which value a certain key has. Then you can print out this value to the standard output by writing this line in your keyDown(...) - Method:

System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);

Some frequently used keys have special variables. For example the cursor keys have Event.LEFT, Event.RIGHT, Event.UP, Event.DOWN. These values are field variables in the class Event. For more variables please read the API!

  1. public boolean keyDown (Event e, int key): This method reacts to key up - events. You can use this method exactly the same way as the keyDown - Method!

Now we want to modify our "moving ball" - applet a little bit. The user should be able to change the direction of the ball movement by pressing the left or right cursor key. If he presses the space bar, the ball should stop its movement. To make this possible, we have to add the following lines to our applet:

// method to handle key - down events
public boolean keyDown (Event e, int key)
{

// user presses left cursor key
if (key == Event.LEFT)
{

// changing x - speed so that ball moves to the left side (x_speed negative)
x_speed = -1;

}
// user presses right cursor key
else if (key == Event.RIGHT)
{

// changing x - speed so that ball moves to the right side (x_speed positive)
x_speed = 1;

}
// user presses space bar (value = 32!)
else if (key == 32)
{

// Stop ball (x_speed = 0)
x_speed = 0;

}
else
{

/* Additionally the method prints out the ASCII - value if an other key is pressed. This is not necessary but a possibility for you to test which value a key has.*/
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);

}

// DON'T FORGET (although it has no meaning here)
return true;

}

Now you are able to program every essential thing used in a game. You can move objects, handle events and load sounds and images. In the next chapter we will program our first complete game (which was the first game I programmed in Java). To understand the next chapter you have to know something about classes, objects, calling methods of other classes... . And of course you have to know everything I talked about in the last chapters. By programming this game I will show you some new techniques (how to "hit" a ball, changing mouse pointer, random ball movement...) and after programming this game you should be able to program your own games. Well Ok have fun in the next chapter and now you can watch the applet you programmed in this chapter and download the source code!