Mouse Events l Event related to the mouse

  • Slides: 10
Download presentation
Mouse Events l Event related to the mouse are separated into l l Mouse

Mouse Events l Event related to the mouse are separated into l l Mouse events and mouse motion events Mouse Events: mouse pressed the mouse button is pressed down mouse released the mouse button is released mouse clicked the mouse button is pressed down and released without moving the mouse in between mouse entered the mouse pointer is moved onto (over) a component mouse exited the mouse pointer is moved off of a component

Mouse Events (cont’d) l l Mouse Motion Events: the mouse is moved mouse dragged

Mouse Events (cont’d) l l Mouse Motion Events: the mouse is moved mouse dragged the mouse is moved while the mouse button is pressed down Listeners for mouse events are created using l l mouse moved Mouse. Listener and Mouse. Motion. Listener interfaces A Mouse. Event object is passed to l the appropriate method when a mouse event occurs

Mouse Events (cont’d) l For a given program, we may only care about l

Mouse Events (cont’d) l For a given program, we may only care about l l To satisfy the implementation of a listener interface, l l one or two mouse events empty methods must be provided for unused events See l Drawing. Dots. App project

Mouse Events (cont’d) l Rubberbanding is the visual effect in which l l The

Mouse Events (cont’d) l Rubberbanding is the visual effect in which l l The following example continually redraws a line l l a shape is "stretched" as it is drawn using the mouse as the mouse is dragged See l Rubber. Lines. App project

Mouse Events (cont’d) l Each mouse event-handling method receives l a Mouse. Event object

Mouse Events (cont’d) l Each mouse event-handling method receives l a Mouse. Event object that contains information about l l The methods and constants of class Input. Event l l the mouse event that occurred, including the x- and y-coordinates § of the location where the event occurred. enable you to determine which mouse button the user clicked. See Mouse. Tracker project

Event Adapter Classes l Many event-listener interfaces contain multiple methods. l An adapter class

Event Adapter Classes l Many event-listener interfaces contain multiple methods. l An adapter class implements an interface and provides l a default implementation (with an empty method body) of l l l each method in the interface. You extend an adapter class to l inherit the default implementation of every method and l override only the method(s) you need for event handling See Mouse. Details project

Event Adapter Classes (cont’d) l A mouse can have one, two or three buttons.

Event Adapter Classes (cont’d) l A mouse can have one, two or three buttons. l Class Mouse. Event inherits several methods from l Input. Event that can distinguish among mouse buttons or l mimic a multibutton mouse with l l a combined keystroke and mouse-button click. Java assumes that every mouse l contains a left mouse button.

Event Adapter Classes (cont’d) l The number of consecutive mouse clicks is returned l

Event Adapter Classes (cont’d) l The number of consecutive mouse clicks is returned l l by Mouse. Event method get. Click. Count. Methods is. Meta. Down and is. Alt. Down l determine which mouse button the user clicked.

Event Adapter Classes (cont’d) l Java also gives us an alternate technique l l

Event Adapter Classes (cont’d) l Java also gives us an alternate technique l l A listener class can be created l l for creating listener classes by extending an event adapter class If a listener interface has more than one method, l it has a corresponding adapter class, l such as the Mouse. Adapter class

Event Adapter Classes (cont’d) l Each adapter class implements the l l corresponding listener,

Event Adapter Classes (cont’d) l Each adapter class implements the l l corresponding listener, providing empty method definitions When you derive a listener class from an adapter class, l you only need to override the event methods that l l Empty definitions for unused event methods l l pertain to the program are automatically provided via inheritance See Distance. To. Center project