lecture26-aug12 - 8/12/09 Lecture 26 Assignment 5 due today...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
8/12/09 1 Lecture 26 Assignment 5 due today Exam 2 next time Today’s topics Event-driven programming in Java Exam & course review Introducing Event-driven programming and the JAVA event handling model Event Handling Events • An event can be defined as a type of signal to the program that something has happened. • Most events are generated by external user actions such as mouse movements, mouse button clicks, and keystrokes (or by the operating system, such as a timer interrupt). • In Event-driven programming , code (e.g. in an applet) is executed in response to the activation of an event, as opposed to, Procedural programming in which the code is executed in a defined procedural order. • Events are supported by the classes and methods found in java.awt.event The Event OO Delegation Model Source Object Event object Event listener object Event trigger generates Notification sent to registered listeners Contains the event handler methods Event Object Information id: A number that identifies the event. source: The component upon which the event occurred; e.g. the applet being run arg: Additional information about the source components. x, y coordinates: The mouse pointer location when a mouse movement event occurred (methods are getX and getY). clickCount: The number of consecutive clicks for the mouse events. For other events, it is zero. when : The time stamp of the event. key: The key that was pressed or released. Java Event Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8/12/09 2 Event class (e.g. MouseEvent ). Contains information about that type of event (e.g. mouse location) Event listener. Knows what to do when that type of event occurs. Implements listener interface (e.g. MouseListener knows what to do for mouse events ) Event source. Keeps a list of registered listeners and notifies them when the event occurs (e.g. an applet) Event classes Mouse Click Event Handling YourApplet event listener Mouse click in the applet window triggers Notification sent to the registered MouseClickListener Contains the mouseClicked event handler method Recall that an interface is like an abstract class definition, but with NO instance variables with abstract methods that have no implementation body all methods are automatically public public interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); } Listening to mouse events Objective - Print out (in the console window) the mouse clicked location in the applet window class MouseSpy implements MouseListener { public void mouseClicked(MouseEvent e) { System.out.println ("Mouse clicked. x=" + e.getX() + " y=" + e.getY()); } // similar style for other 4 listener methods } the keyword implement s means that this class inherits
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/24/2010 for the course EE 322C taught by Professor Nettles during the Summer '08 term at University of Texas at Austin.

Page1 / 7

lecture26-aug12 - 8/12/09 Lecture 26 Assignment 5 due today...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online