Package codedraw
Class EventScanner
java.lang.Object
codedraw.EventScanner
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
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.
If you use an older version of Java you can utilize the
Common mistakes when using the EventScanner:
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.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
hasEvent()
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Compared tohasEvent()
this method does not wait until the next event is available, but instead returns immediately.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.boolean
Waits until the next event is available or returns immediately if there is an event in the EventScanner already.iterator()
Creates an iterator containing all the currently available events.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.Waits until the next event is available or returns immediately if there is an event in the EventScanner already.void
removeEventsOlderThan
(Duration duration) Removes all events that are older than the duration given as a parameter.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
hasEventNow
public boolean hasEventNow()Compared tohasEvent()
this method does not wait until the next event is available, but instead returns immediately. If there is currently an event available returns true otherwise false.- Returns:
- whether there are currently events available
-
hasEvent
public boolean hasEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns false if there are no more events, true otherwise.- Returns:
- whether there are more events available
-
hasMouseClickEvent
public boolean hasMouseClickEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse click event, otherwise false. The mouse click event is triggered once every time a mouse button is pressed down and quickly released again.- Returns:
- whether the next event is a mouse click event.
-
hasMouseMoveEvent
public boolean hasMouseMoveEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse move event, otherwise false. The mouse move event is triggered continuously while the mouse is being moved.- Returns:
- whether the next event is a mouse move event.
-
hasMouseDownEvent
public boolean hasMouseDownEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse down event, otherwise false. The mouse down event is triggered exactly once every time a mouse button is pressed down.- Returns:
- whether the next event is a mouse down event.
-
hasMouseUpEvent
public boolean hasMouseUpEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse up event, otherwise false. The mouse up event is triggered every time a mouse button is released.- Returns:
- whether the next event is a mouse up event.
-
hasMouseEnterEvent
public boolean hasMouseEnterEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse enter event, otherwise false. The mouse enter event is triggered every time the mouse enters the canvas.- Returns:
- whether the next event is a mouse enter event.
-
hasMouseLeaveEvent
public boolean hasMouseLeaveEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse leave event, otherwise false. The mouse leave event is triggered every time the mouse leaves the canvas.- Returns:
- whether the next event is a mouse leave event.
-
hasMouseWheelEvent
public boolean hasMouseWheelEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a mouse wheel event, otherwise false. The mouse wheel event is triggered each time the mouse wheel is turned.- Returns:
- whether the next event is a mouse wheel event.
-
hasKeyDownEvent
public boolean hasKeyDownEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a key down event, otherwise false. The key down event is triggered exactly once every time a key is pressed down.- Returns:
- whether the next event is a key down event.
-
hasKeyUpEvent
public boolean hasKeyUpEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a key up event, otherwise false. The key up event is triggered exactly once every time a key is released.- Returns:
- whether the next event is a key up event.
-
hasKeyPressEvent
public boolean hasKeyPressEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a key press event, otherwise false. The key press event is triggered continuously while a key is being held down.- Returns:
- whether the next event is a key press event.
-
hasWindowMoveEvent
public boolean hasWindowMoveEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a window move event, otherwise false. The window move event is triggered every time the CodeDraw window is moved.- Returns:
- whether the next event is a window move event.
-
hasWindowCloseEvent
public boolean hasWindowCloseEvent()Waits until the next event is available or returns immediately if there is an event in the EventScanner already. Returns true if the next event is a window close event, otherwise false. The window close event is triggered exactly once after the user closes the window orCodeDraw.close()
is called.- Returns:
- whether the next event is a window close event.
-
nextEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. The event is returned as a generic Event object which is one of the twelve event subtypes. CheckhasEvent()
orhasEventNow()
before calling this function.
You can use this method to either discard events or use the switch type matching feature of Java.while (es.hasEventNow()) { switch (es.nextEvent()) { case MouseDownEventArgs a: System.out.println("The mouse has been pressed at x=" + a.getX() + " y=" + a.getY() + "."); break; case KeyDownEventArgs a: System.out.println("The " + a.getKey() + " has been pressed."); break; default: break; } }
- Returns:
- The next available event.
- Throws:
NoSuchElementException
- if there are no more events.
-
nextMouseClickEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse click event the event is returned otherwise an InputMismatchException is thrown. The mouse click event is triggered once every time a mouse button is pressed down and quickly released again. CheckhasMouseClickEvent()
before calling this function.- Returns:
- a mouse click event.
- Throws:
InputMismatchException
- if the next event is not a mouse click event.NoSuchElementException
- if there are no more events.
-
nextMouseMoveEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse move event the event is returned otherwise an InputMismatchException is thrown. The mouse move event is triggered continuously while the mouse is being moved. CheckhasMouseMoveEvent()
before calling this function.- Returns:
- a mouse move event.
- Throws:
InputMismatchException
- if the next event is not a mouse move event.NoSuchElementException
- if there are no more events.
-
nextMouseDownEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse down event the event is returned otherwise an InputMismatchException is thrown. The mouse down event is triggered exactly once every time a mouse button is pressed down. CheckhasMouseDownEvent()
before calling this function.- Returns:
- a mouse down event.
- Throws:
InputMismatchException
- if the next event is not a mouse down event.NoSuchElementException
- if there are no more events.
-
nextMouseUpEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse up event the event is returned otherwise an InputMismatchException is thrown. The mouse up event is triggered every time a mouse button is released. CheckhasMouseUpEvent()
before calling this function.- Returns:
- a mouse up event.
- Throws:
InputMismatchException
- if the next event is not a mouse up event.NoSuchElementException
- if there are no more events.
-
nextMouseEnterEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse enter event the event is returned otherwise an InputMismatchException is thrown. The mouse enter event is triggered every time the mouse enters the canvas. CheckhasMouseEnterEvent()
before calling this function.- Returns:
- a mouse enter event.
- Throws:
InputMismatchException
- if the next event is not a mouse enter event.NoSuchElementException
- if there are no more events.
-
nextMouseLeaveEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse leave event the event is returned otherwise an InputMismatchException is thrown. The mouse leave event is triggered every time the mouse leaves the canvas. CheckhasMouseLeaveEvent()
before calling this function.- Returns:
- a mouse leave event.
- Throws:
InputMismatchException
- if the next event is not a mouse leave event.NoSuchElementException
- if there are no more events.
-
nextMouseWheelEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a mouse wheel event the event is returned otherwise an InputMismatchException is thrown. The mouse wheel event is triggered each time the mouse wheel is turned. CheckhasMouseWheelEvent()
before calling this function.- Returns:
- a mouse wheel event.
- Throws:
InputMismatchException
- if the next event is not a mouse wheel event.NoSuchElementException
- if there are no more events.
-
nextKeyDownEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a key down event the event is returned otherwise an InputMismatchException is thrown. The key down event is triggered exactly once every time a key is pressed down. CheckhasKeyDownEvent()
before calling this function.- Returns:
- a key down event.
- Throws:
InputMismatchException
- if the next event is not a key down event.NoSuchElementException
- if there are no more events.
-
nextKeyUpEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a key up event the event is returned otherwise an InputMismatchException is thrown. The key up event is triggered exactly once every time a key is released. CheckhasKeyUpEvent()
before calling this function.- Returns:
- a key up event.
- Throws:
InputMismatchException
- if the next event is not a key up event.NoSuchElementException
- if there are no more events.
-
nextKeyPressEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a key press event the event is returned otherwise an InputMismatchException is thrown. The key press event is triggered continuously while a key is being held down. CheckhasKeyPressEvent()
before calling this function.- Returns:
- a key press event.
- Throws:
InputMismatchException
- if the next event is not a key press event.NoSuchElementException
- if there are no more events.
-
nextWindowMoveEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a window move event the event is returned otherwise an InputMismatchException is thrown. The window move event is triggered every time the CodeDraw window is moved. CheckhasWindowMoveEvent()
before calling this function.- Returns:
- a window move event.
- Throws:
InputMismatchException
- if the next event is not a window move event.NoSuchElementException
- if there are no more events.
-
nextWindowCloseEvent
Waits until the next event is available or returns immediately if there is an event in the EventScanner already. If the event is a window close event the event is returned otherwise an InputMismatchException is thrown. The window close event is triggered exactly once after the user closes the window orCodeDraw.close()
is called. CheckhasWindowCloseEvent()
before calling this function.- Returns:
- a window close event.
- Throws:
InputMismatchException
- if the next event is not a window close event.NoSuchElementException
- if there are no more events.
-
removeEventsOlderThan
Removes all events that are older than the duration given as a parameter.- Parameters:
duration
- any duration.
-
iterator
Creates an iterator containing all the currently available events. By calling this method all events in the EventScanner are consumed and the EventScanner will be empty until new events are generated.
-