Events

 

 

 

Foreword

Resources

Code Listings

Foil Sets

Assignments

Solutions

External Resources

SiteMap

Search

Home

 

Up ] Thread Base Applet ] Math Class ] String Classes ] Off Screen GC ] JavaApplet Context ] [ Events ] Networking ] Java Appl Design ]

Introduction to Event

Java events are part of the Java AWT package. An event is the way that the AWT communicates to you, as the programmer, and to other Java AWT components that something has happened. That something can be input from the user (mouse movements or clicks, keypresses), changes in the system environment (a window opening or closing, the window being scrolled up or down), or a host of other things that might, in some way, be interesting to the operation of the program.

The Mouse Event

    public boolean mouseDown(Event ev, int x, int y);
    public boolean mouseUp(Event ev, int x, int y);

The AWT will generate mouseDown event when you press the Mouse button, and a mouseUp event when the button is released.

    public boolean mouseMove(Event ev, int x, int y);
    public boolean mouseDrag(Event ev, int x, int y);

Every time the mouse is moved a single pixel in any direction, a mouse event is generated. MouseMove event is generate for plain mouse pointer movements without the mouse button pressed. MouseDrag event handle mouse movements made with the mouse button pressed down.

    public boolean mouseEnter(Event ev, int x, int y);
    public boolean mouseExit(Event ev, int x, int y);

These two methods are called when the mouse pointer enters the applet or when it exits the applet. It is more useful on components of user interfaces that you put inside an applet.

The Keyboard Event

Keyboard events are generated whenever users press a key on the keyboard. By using key events, you can get hold of the values of the keys they pressed to perform an action or merely to get character input from the users of your applet.

    public boolean keyDown(Event ev, int key);
    public boolean keyUp(Event ev, int key);

The AWT will generate keyDown event when you press a key, and a keyUp event when the key is released. The key argument are integers representing ASCII character values, which include alphanumeric characters, function keys, tabs, return, and so on. Please see JAVA API reference : java.awt.Event

The following is an example that show you the usage of Mouse events and Keyboard events. You will see a Rectangle, a Triangle, and a Circle in this applet. When you move the mouse inside the applet, the applet's boundry will become cyan color, or it is red. When you move the mouse inside the object (Rectangle, Triagnle, or Circle), it will become cyan, or it is red. When the object become cyan, you can click on the object and move it to any position in the applet. In the mean time, the object also accept keyboard events. You can press UP, DOWN, LEFT, or RIGHT key to change object's posotion, too.

XOR Example

Source code :
1. EventTest.java
2. mPoint.java (Movable Point)
3. mRectangle.java (Movable Rectangle)
4. mCircle.java (Movable Circle)
5. mTriangle.java (Movable Triangle)

Double Buffering Example

Source code :
1. EventTest.java
2. mPoint.java (Movable Point)
3. mRectangle.java (Movable Rectangle)
4. mCircle.java (Movable Circle)
5. mTriangle.java (Movable Triangle)

User Interface Action

In "Introduction on Applet Context", we have a simple example as follow:

That show you the usage of showStatus() and showDocument(). Here I would like to disscus more about UI Action.

When the AWT components (in this case is two buttons) receive some events, it will then do something properly for itself (such as redraw the button, so it looks pressed or unpressed). After that it may generate an UI action to its parent (in this case is the main applet). When the parent receive the action, it can determine that it come from which component and do something it. You may see something like this:

    public boolean action(Event ev, Object arg) {
      if ( ev.target == status ) {
        getAppletContext().showStatus("You Click on Show Status!");
        return true;
      } else if ( ev.target == document ) {
        try {
          URL url = new URL(getCodeBase(), "Context.java");
          getAppletContext().showDocument(url);
        } catch (MalformedURLException e);
        return true;
      }
      return false;
    }

The status is the ShowStatus button's instance, the document is the ShowDocument's instance. Usually we will have a series of if-else flowcontrol statements. By compres component's instance to event's target variable member, we can deside which component and what action we should do.