Chapter 18 - Mouse, keyboard, sounds, and images. This is essentially a “learning by doing” chapter. Not much theory here. This chapter also introduces bit-wise logical operators in the context of identifying the status of keyboard modifier keys (Alt, Shift, Ctrl). | Mouse, Keyboard, Sounds, and Images Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 3rd AP edition 18- This is essentially a “learning by doing” chapter. Not much theory here. 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. 18- This chapter also introduces bit-wise logical operators in the context of identifying the status of keyboard modifier keys (Alt, Shift, Ctrl). 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 18- A mouse listener can be . | Mouse, Keyboard, Sounds, and Images Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 3rd AP edition 18- This is essentially a “learning by doing” chapter. Not much theory here. 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. 18- This chapter also introduces bit-wise logical operators in the context of identifying the status of keyboard modifier keys (Alt, Shift, Ctrl). 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 18- A mouse listener can be implemented as an inner class or even an inline class, as follows: public class MyPanel extends JPanel { public MyPanel() // constructor { . addMouseListener(new MouseAdapter() // inline class { public void mouseClicked(MouseEvent e) { . // process click at (), () } } ); . } } 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 18- mouseClicked checks that the button was pressed and released in the same place, which may result in missed clicks. 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 .