Java Events
(and how to handle them)
Clicking on any of the four different colors below will cause an event
A different sound is triggered when you press the mouse
button down
within the borders of a color region.
We trigger events in Java in many ways. The sections on sound and animation used the start and stop events that are automatically triggered when you enter or leave a page that contains an applet. The mouse can be pressed to cause an event called mouseDown. When you release the mouse button you trigger a mouseUp event. Mouse actions and movements (mouseDrag, mouseEnter, mouseExit & mouseMove) , keystrokes on the keyboard (keyDown & keyUp) all are examples of events we can use to trigger actions by handling the event when it is triggered. This particular applet uses the mouseDown event and handles it in the code below.
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.applet.AudioClip;
public class events1
extends java.applet.Applet {
|
We want the graphics, color and event handling facilities of the Java Abstract Windowing Toolkit We also want to use the audio capabilities. And create an Applet called events1 |
| public void paint(Graphics g) { for (int i=0 ; i <= 4 ; i++) { g.setColor( new Color(63*i,100,(255-(63*i)))); g.fillRect((i-1)*100,0,i*100,80); } } |
The paint method will create four .... different colors ** and draw rectangles filled with those colors |
| public boolean mouseDown(Event evt, int x, int y) { if (x < 100) play(getCodeBase(),"bark.au"); if ((x > 100)&(x < 200)) play(getCodeBase(),"honk.au"); if ((x > 200)&(x < 300)) play(getCodeBase(),"cellular.au"); if (x > 300) play(getCodeBase(),"feryhorn.au"); return true; } } |
This is the event handling code. The x and y coordinates of the mouse pointer will be passed to this routine when the event occurs. We simply need to consider the x coordinate if its less than 100 we have a click on the first rectangle and play the bark.au sound Between 100 and 200 ... Play the honk.au sound and so on When finished processing this event return whatever boolean value we want for the result. |
** we can create custom colors by using 'new Color(red,green,blue)' where red,green and blue are integers from 0 to 255 that represent the level of the primary color in the custom composite color.
| Java does have a switch statement that is similar to the one in C++.
Unfortunately it only handles discrete values and not ranges. We could enter
the following, switch (x){ It will handle only the values 100, 200, ... but there is no way to handle ranges of values. It makes the switch statement somewhat limited in use. |
The easiest way to copy the code for this example is to click here, copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "events1.java".
After compiling this program we need to create an HTML page with a reference to the class file. It does not need a graphic window unless you are running it with an image or animation.
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute as below.
View of the applet's output
If you try really hard, you will be able to click on the border between
two rectangles and not get any sound. Notice in the code that there is no option to handle
the event if x = 100, 200 or 300. We can choose to ignore or handle any event as we see
fit.
Other events are handled in a similar fashion. The components of the AWT covered in the
next section can also trigger events and allow us to program true Windows style GUI
Applets and Applications.
Go to the next topic: AWT Components