mouse events

mouse events - CSc 2700: Lecture # 20 Mouse Events November...

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

View Full Document Right Arrow Icon
CSc 2700: Lecture # 20 Mouse Events November 6, 2008 1 Introduction Clicking a button with the mouse generates an action event. This, however, is not what is meant by a mouse generated event. This action event is generated by the button. Clicking, dragging, and moving the mouse generate events independent of the ”clicked components. ” Mouse movements generate several diFerent kinds of events events but not all such events warrant a response. Dragging or moving the mouse over a component generates a MouseEvent. A listener class that handles these events implements the MouseMotionLis- tener interface with methods: void mouseDragged(MouseEvent e) and void mouseMoved(MouseEvent e) Clicking the mouse, entering a component, or exiting a component also gen- erates a MouseEvent. In this case, the appropriate interface is MouseListener with methods: void mouseClicked(MouseEvent e) mouse is pressed and released void mouseEntered(MouseEvent e) mouse enters a component void mouseExited(MouseEvent e) mouse leaves a component 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
void mousePressed(MouseEvent e) mouse is pressed void mouseReleased(MouseEvent e) mouse is released It is not necessary to have executable code for each of these methods, but the listener must still implement each one, even with an empty set of statements. A few useful MouseEvent methods are: Component getComponent() returns component where the MouseEvent occurred. int getX() returns the horizontal coordinate of the event. int getY() returns the vertical coordinate of the event. Point getPoint() returns a reference to a two-dimensional Point object such that the public Felds x and y hold the horizontal and vertical coordinates of the event. 2 A Simple Paint Program Some graphics programs provide facilities to draw all types of predeFned shapes as well ”freehand” sketches using the mouse. Paint provides ”pencil drawing” as well as thick-lined paintbrush drawing and spray-paint drawing. Each of these options utilizes the mouse as a pencil, a paintbrush, or a can of spray paint. Below, we can implement a simple system that provides pencil drawing. All we need to do is respond to mouse events. We now discuss the implementation of a simple drawing application that allows a user to pencil- draw Fgures by dragging the mouse. The application draws Fgures in one of three colors chosen via an Input Dialog Box. The application also provides an ”Erase” button that clears the screen. Program 1. The artwork is accomplished by drawing tiny line segments that join ”close” points. When the mouse is pressed, a ”starting point” is recorded. As the user drags the mouse, lines are drawn from the start point to 2
Background image of page 2
Figure 1: Drawing A Graphics Pencil the current mouse position, then from that point to the next mouse position, etc. For example, if the mouse is pressed at starting position (38, 32) and then dragged over (39, 32), (40, 32), (41, 33), and (41, 34), very short line segments are drawn connecting
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/06/2009 for the course CSC 2700 taught by Professor Duncan during the Spring '08 term at LSU.

Page1 / 11

mouse events - CSc 2700: Lecture # 20 Mouse Events November...

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

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