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).
Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin Chapter Mouse, Keyboard, Sounds, and Images Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved 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 182 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 183 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) Called when the mouse cursor enters/exits component’s visible area • One click and release causes several calls Using only mouseReleased is usually a safe bet 184 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(); } 185 Mouse Events (cont’d) • The MouseMotionListener interface adds two methods: void mouseMoved (MouseEvent e) void mouseDragged (MouseEvent e) Called when the mouse has moved with a button held down • These methods are often used together with MouseListener methods (the same class implements both interfaces) 186 Keyboard Events • Keyboard events are captured by an object which is a KeyListener • The object to which a key listener is attached must first obtain keyboard “focus.” This is done by calling the component’s requestFocus method • A component (for example, a JPanel) can serve as its own key listener: addKeyListener(this); 187 Keyboard Events (cont’d) • The KeyListener interface defines three methods: void keyPressed (KeyEvent e) void keyReleased (KeyEvent e) void keyTyped (KeyEvent e) • One key pressed and released causes several events 188 Keyboard Events (cont’d) • Use keyTyped to capture character keys (that is, keys that correspond to printable characters) • e.getKeyChar() returns a char, the typed character: public void keyTyped (KeyEvent e) { char ch = e.getKeyChar(); if (ch == ‘A’) } 189 Keyboard Events (cont’d) • Use keyPressed or keyReleased to handle “action” keys, such as cursor keys, , function keys, and so on • e.getKeyCode() returns and int, the key’s “virtual code.” • The KeyEvent class defines constants for various virtual keys For example: VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN Cursor keys VK_HOME, VK_END, VK_PAGE_UP, etc Home, etc 1810 Keyboard Events (cont’d) • e.isShiftDown(), e.isControlDown(), e.isAltDown() return the status of the respective modifier keys • e.getModifiers() returns a bit pattern that represents the status of all modifier keys • KeyEvent defines “mask” constants CTRL_MASK, ALT_MASK, SHIFT_MASK, and so on 1811 Playing Audio Clips • The JApplet class has a method getAudioClip that returns an AudioClip object import java.applet.AudioClip; AudioClip tune = getAudioClip ( URLPath, relativePathName); There may be a slight tune.play(); delay The audio clip is actually loaded only when you call its play method for the first time 1812 Playing Audio Clips (cont’d) • URLPath is the path part of the URL where the audio clip file is located getDocumentBase() is often used for this parameter • getDocumentBase() returns the path part of the absolute URL of this applet’s HTML file: AudioClip bells = getAudioClip ( getDocumentBase(), “sounds/Bells.wav"); 1813 Images • The JApplet class has a method getImage that returns an Image object import java.awt.Image; private Image picture; picture = getImage (getDocumentBase(), "flower.gif"); public void paintComponent(Graphics g) { ImageObserver (often null) if (picture != null) g.drawImage (picture, x, y, this); } 1814 Images (cont’d) • In applications, it may be easier to work with Swing’s ImageIcon objects: import javax.swing.ImageIcon; private ImageIcon picture; picture = new ImageIcon ("flower.gif"); public void paintComponent (Graphics g) { if (picture != null) picture.drawIcon (this, g, x, y); } The component on which this icon is displayed (usually this) 1815 Images (cont’d) • Icons can be placed on JButtons, JLabels, JMenuItems, etc: import javax.swing.ImageIcon; ImageIcon arrow = new ImageIcon ("rightarrow.gif"); JButton next = new JButton(“Next”, arrow); • Icon’s getImage method returns its image (an Image object) 1816 The Drawing Editor Program 1817 Drawing Editor (cont’d) • Allows to move and shape objects on “canvas” using the mouse and the cursor keys • Here the shapes are circles (“balloons”) • Uses JColorChooser to pick a color 1818 Drawing Editor (cont’d) BalloonDraw ControlPanel DrawingPanel Balloon • You write the whole program; there is a detailed step-by-step plan in the book (Section 18.4) 1819 Review: • Name the five methods of the MouseListener interface • Can a class implement MouseMotionListener but not MouseListener? • What are the units and the origin for the coordinates returned by the MouseEvent’s getX and getY methods? • How many methods does the KeyListener interface specify? 1820 Review (cont’d): • Which KeyListener’s method is used to capture an action key event? • Which KeyEvent’s method returns the actual character typed? • Which KeyEvent’s methods return the status of modifier keys? • When we need a requestFocus() call? 1821 Review (cont’d): • What does an applet’s getDocumentBase() method return? • Which class was written earlier: Image or ImageIcon? • How can we display an icon on a panel (a JPanel object)? • How can we display an icon on a button (a JButton object)? 1822 ... 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 18? ?2 Mouse... (an Image object) 18? ?16 The Drawing Editor Program 18? ?17 Drawing Editor (cont’d) • Allows to move and shape objects on “canvas” using the mouse and the cursor keys • Here the shapes are circles... pick a color 18? ?18 Drawing Editor (cont’d) BalloonDraw ControlPanel DrawingPanel Balloon • You write the whole program; there is a detailed step-by-step plan in the book (Section 18. 4) 18? ?19 Review: