Ch17 - Java Methods Java A & AB Object-Oriented...

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

View Full Document Right Arrow Icon
Java Methods Java Methods Object-Oriented Programming and Data Structures Maria Litvin Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved . Mouse, Keyboard, Sounds, and Images Chapter
Background image of page 1

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

View Full DocumentRight Arrow Icon
17-2 Objectives: Learn how to handle mouse and keyboard events in Java. Implement a simple drawing editor application. Learn the basics of playing sounds and displaying images in applets and applications.
Background image of page 2
17-3 Mouse Events Mouse events are captured by an object which is a MouseListener and possibly a MouseMotionListener . A mouse listener is often attached to a JPanel component. It is not uncommon for a panel to serve as its own mouse listener: public MyPanel() { ... addMouseListener(this); addMouseMotionListener(this); // optional
Background image of page 3

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

View Full DocumentRight Arrow Icon
17-4 Mouse Events (cont’d) The MouseListener interface defines five methods: void mousePressed (MouseEvent e) void mouseReleased (MouseEvent e) void mouseClicked (MouseEvent e) void mouseEntered (MouseEvent e) void mouseExited (MouseEvent e) One click and release causes several calls. Using only mouseReleased is usually a safe bet. Called when the mouse cursor enters/exits component’s visible area
Background image of page 4
17-5 Mouse Events (cont’d) Mouse listener methods receive a MouseEvent object as a parameter. A mouse event can provide the coordinates of the event and other information: public void mousePressed (MouseEvent e) { int x = e. getX (); int y = e. getY (); int b = e. getButton (); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
17-6 Mouse Events (cont’d) The
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

Ch17 - Java Methods Java A & AB Object-Oriented...

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

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