SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding AWT Event Listeners: Maximizing User Interactivity in Java AWT Model

vonda avatar
14 diapositives · 252 Views · Oct 02, 2014

Learn about implementing KeyListeners, MouseListeners, and managing Threads in Java AWT for optimal user-driven interaction. Discover how to efficiently handle input events and navigate synchronization issues in multi-threaded environments. Master the art of incorporating gestures and movements to enhance user experience in Java applications.

Understanding AWT Event Listeners: Maximizing User Interactivity in Java AWT Model - slide 1
01 / 14Understanding AWT Event Listeners: Maximizing User Interactivity in Java AWT Model
1 / 17

Télécharger la présentation

Understanding AWT Event Listeners: Maximizing User Interactivity in Java AWT Model
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. User Interactivity Chapter 3

  2. AWT Event Model • AWT has own thread, “listening” to OS • Listener: an object that receives events from another object • Different types of listeners for different types of events. You didn’t hear about last night’s big event? You must not be on the right notification list!

  3. How Listeners Work • User presses a key • OS sends the key event to the Java runtime thread • Java posts the event to AWT’s event queue • AWT dispatch thread posts the event to all objects that are KeyListeners • The KeyListener receives the event.

  4. Listeners are Interfaces • public class MyGame extends GameCore implements KeyListener{ • Interfaces vs Superclasses : • Use interfaces for behaviors, superclasses for ISA relationships • Interfaces typically give an object a behavior secondary to its main behavior

  5. Key Listeners • Used for firing, moving, not really for String input • Create an object that implements KeyListener: • keyPressed() • keyReleased() • keyTyped() (key held down) • Register the listener with the window you want to listen to Window window = screen.getFullScreenWindow(); window.addKeyListener(keyListener);

  6. Extras • Focus Traversal Keys – keys that already have meaning in a window interface (tab goes from one field to the next) window.setFocusTraversalKeysEnabled(false); • KeyCodes – how you know which key was pressed. int keycode = e.getKeyCode(); if(keycode == KeyEvent.VK_ENTER) • Consume the event so other Listeners don’t see it: e.consume(); • Beware small differences between OS’s

  7. Mouse Input • Three Types of Mouse Events: • Button clicks (MouseListener) • Mouse motion (MouseMotionListener) • Regular movement • Drag motion • Wheel scrolls (MouseWheelListener)

  8. Changing the Mouse • Sometimes you don’t want mouse motion to stop at the end of the screen. (First-person game) • Constantly reposition mouse in middle of screen. • Calculate distance moved from center each time to update display.

  9. Changing the Mouse • Your mouse icon can also look like: • Hand • Crosshairs • Hourglass • I (text based) • Make it go away altogether

  10. Threads • Threads – executions of code. • Multithreading – many threads running at once • Multiple threads accessing the same data can cause problems • Hit key “g” Hit key “->” - Find s’ position (p1) - Move s to p2 - Resize s - Redraw s at p2 - Redraw s at p1

  11. Synchronization • Draws need to happen one at a time. • Mouse movements need to happen one at a time. • “Lock” the code so that only one thread can execute the code at a time. publicsynchronizedvoid mouseMoved(MouseEvent e) {

  12. GameAction (420 engine) • Model a user interaction • Tell if a key or mouse button is pressed, and how many times • Each GameAction will map to some animation in the game: • Ex: Hitting “->” makes the screen scroll to the right.

  13. Input Manager (420 engine) • One class that will: • Handle all key & mouse events • Save the events • Map the events to game actions • Let the user configure the keyboard • It implements all listener types • Contains a keyCode to GameAction mapping

  14. Pausing • One GameAction can map to “pause” • What does it mean to pause a game? • Continue drawing • Stop updating • Stop taking input from user • In update of GameCore extension: if (!isPaused()){ checkGameInput(); player.update(elapsedTime);} • After coming back from being paused, clear the GameActions that happened. inputManager.resetAllGameActions();