Package codedraw

Class EventScanner

java.lang.Object
codedraw.EventScanner
All Implemented Interfaces:
Iterable<Event>

public class EventScanner extends Object implements Iterable<Event>
The EventScanner can be used to handle events that are produced in CodeDraw. There is two ways to consume events from the EventScanner, either through the Iterable<Event> interface or through the Scanner like structure by calling has- and next-methods.

The example below displays the current mouse position and the amount the mouse has been pressed. Before each frame is drawn all events that happened between the previous frame and the current frame are processed by a foreach loop. Each event inside that foreach loop is then processed based on the type of the event. If it is a MouseMoveEvent then the position is saved, if it is a MouseClickEvent then the clickCount is increased. All other event types are discarded in the default branch.

 CodeDraw cd = new CodeDraw();

 int mouseX = 0;
 int mouseY = 0;
 int clickCount = 0;

 while (!es.isClosed()) {
     for (var e : cd.getEventScanner()) {
         switch (e) {
             case MouseMoveEvent a -> {
                 mouseX = a.getX();
                 mouseY = a.getY();
             }
             case MouseClickEvent a -> clickCount++;
             default -> { }
         }
     }

     cd.clear();
     cd.drawText(100, 100, "Position: " + x + " " + y + "\nClick: " + clickCount);
     cd.show();
 }
 


If you use an older version of Java you can utilize the Scanner like properties of the EventScanner. The inner while loop in the example below processes all currently available events. In each iteration a new event will be at the head of the queue and the inner while loop will only stop once all currently available events are consumed. Inside the inner loop depending on which type of event is at the head of the queue one branch of the if/else will be selected. If the head of the queue is a MouseMoveEvent event the hasMouseMoveEvent() method will return true and then the nextMouseMoveEvent() method will be called which returns the MouseMoveEvent from the head of the queue. All other events inside the queue will then shift forward and there will be a new event at the head of the queue. Do not forget to call the next method because otherwise the program will enter an endless loop because the same event will always be at the head of the queue. After the MouseMoveEvent has been returned from the nextMouseMoveEvent() method the event can be used to update the state of the program. In this case it just updates the current mouse position. After all currently available events are processed the changes are displayed by clearing the canvas and then calling the drawText method.

 CodeDraw cd = new CodeDraw();
 EventScanner es = cd.getEventScanner();
 int mouseX = 0;
 int mouseY = 0;
 int clickCount = 0;

 while (!es.isClosed()) {
     while (es.hasEventNow()) {
         if (es.hasMouseMoveEvent()) {
             MouseMoveEvent a = es.nextMouseMoveEvent();
             mouseX = a.getX();
             mouseY = a.getY();
         }
         else if (es.hasMouseClickEvent()) {
             es.nextEvent();
             clickCount++;
         }
         else {
             es.nextEvent();
         }
     }

     cd.clear();
     cd.drawText(100, 100, "Position: " + mouseX + " " + mouseY + "\nClick: " + clickCount);
     cd.show();
 }
 


Common mistakes when using the EventScanner:
  • The next method is not called within an if branch.
  • When consuming more than two events ifs are used instead of elseif.
  • The last else branch is forgotten and the remaining events are not discarded.
When a CodeDraw window is closed all remaining events can still be consumed from the EventScanner but no new events will appear.