An All-Purpose Event Queue

What we have so far is a number of event lists, with each list containing a certain type of event (e.g., a list for key events, a list for mouse events, etc.). When these events are handled in the main loop thread, the lists are processed individually (i.e., we may first process all of the key events that were added to the key event list within the last main loop cycle and then process all of the mouse events). With this system, the order of events can get mixed up (say, if two key events come in, perhaps key pressed followed by key released, and a mouse press event in between them within one main loop cycle). With separate event lists, the two key events will be processed first and then finally the mouse event, when in reality that wasn't their order. However, it's important to note that your main loop will probably run at a superior rate to the possibilities of the user, creating a mixture of events in this fashion, all generated within the same main loop cycle. But synchronization issues, however small, can arise if the code can find a way through the cracks. For this, we propose an all-around event processor that takes in all events in which you are interested, as and when they are received, and handles them in that order from the main loop.

The class java.awt.AWTEvent is the root class of all AWT events, such as MouseEvent, KeyEvent, and FocusEvent (which we'll look at later), so for the all-around event processor we can cast all events to this type for handling. The following two source listings show the code for an event processor that works just like the MouseProcessor and KeyProcessor we made earlier in this chapter. Here is the code for the interface EventProcessable and the class EventProcessor.

Code Listing 10-9: EventProcessable

start example
import java.awt.*;      public interface EventProcessable {     public void handleEvent(AWTEvent e); } 
end example

Code Listing 10-10: EventProcessor

start example
import java.awt.*; import java.util.*;      public class EventProcessor {     public EventProcessor(EventProcessable handler)     {         eventList = new LinkedList();         this.handler = handler;     }          public void addEvent(AWTEvent event)     {            synchronized(eventList)         {             eventList.add(event);         }     }        public void processEventList()     {         AWTEvent event;             while(eventList.size() > 0)         {             synchronized(eventList)             {                 event = (AWTEvent) eventList.removeFirst();             }                 handler.handleEvent(event);         }     }          private LinkedList eventList;     private EventProcessable handler; }
end example

Using the same techniques that we learned in the earlier examples, the handleEvent method when set up properly could then be defined as follows, provided, for this example, that we have added a mouse listener and a key listener for the process.

public void handleEvent(AWTEvent e)     {         switch(e.getID())         {             case KeyEvent.KEY_PRESSED:                 System.out.println("Key Pressed");                 break;             case KeyEvent.KEY_RELEASED:                 System.out.println("Key Released");                 break;             case MouseEvent.MOUSE_PRESSED:                 System.out.println("Mouse Pressed");                 break;         }     }

So with this system, we deal with events in the order that Java sends them in our main loop. We will now implement this new system and look at handling the loss and gain of focus along the way.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net