Java Methods ObjectOriented Programming and Data Structures 2
Java Methods Object-Oriented Programming and Data Structures 2 nd AP edition with Grid. World Maria Litvin ● Gary Litvin Mouse, Keyboard, Sounds, and Images Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
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. 2
Mouse Events • Mouse events are captured by an object which is a Mouse. Listener and possibly a Mouse. Motion. Listener. • 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 My. Panel() {. . . add. Mouse. Listener(this); add. Mouse. Motion. Listener(this); // optional 3
Mouse Events (cont’d) • The Mouse. Listener interface defines five methods: void mouse. Pressed (Mouse. Event e) void mouse. Released (Mouse. Event e) void mouse. Clicked (Mouse. Event e) void mouse. Entered (Mouse. Event e) void mouse. Exited (Mouse. Event e) Called when the mouse cursor enters/exits component’s visible area • One click and release causes several calls. Using only mouse. Released is usually a safe bet. 4
Mouse Events (cont’d) • Mouse listener methods receive a Mouse. Event object as a parameter. • A mouse event can provide the coordinates of the event and other information: public void mouse. Pressed (Mouse. Event e) { int x = e. get. X(); int y = e. get. Y(); int b = e. get. Button(); } 5
Mouse Events (cont’d) • The Mouse. Motion. Listener interface adds two methods: void mouse. Moved (Mouse. Event e) void mouse. Dragged (Mouse. Event e) Called when the mouse has moved with a button held down • These methods are often used together with Mouse. Listener methods (the same class implements both interfaces). 6
Keyboard Events • Keyboard events are captured by an object which is a Key. Listener. • The object to which a key listener is attached must first obtain keyboard “focus. ” This is done by calling the component’s request. Focus method. • A component (for example, a JPanel) can serve as its own key listener: add. Key. Listener(this); 7
Keyboard Events (cont’d) • The Key. Listener interface defines three methods: void key. Pressed (Key. Event e) void key. Released (Key. Event e) void key. Typed (Key. Event e) • One key pressed and released causes several events. 8
Keyboard Events (cont’d) • Use key. Typed to capture character keys (that is, keys that correspond to printable characters). • e. get. Key. Char() returns a char, the typed character: public void key. Typed (Key. Event e) { char ch = e. get. Key. Char(); if (ch == ‘A’). . . } 9
Keyboard Events (cont’d) • Use key. Pressed or key. Released to handle “action” keys, such as cursor keys, <Enter>, function keys, and so on. • e. get. Key. Code() returns and int, the key’s “virtual code. ” • The Key. Event 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. 10
Keyboard Events (cont’d) • e. is. Shift. Down(), e. is. Control. Down(), e. is. Alt. Down() return the status of the respective modifier keys. • e. get. Modifiers() returns a bit pattern that represents the status of all modifier keys. • Key. Event defines “mask” constants CTRL_MASK, ALT_MASK, SHIFT_MASK, and so on. 11
Playing Audio Clips • The JApplet class has a method get. Audio. Clip that returns an Audio. Clip object. import java. applet. Audio. Clip; . . . Audio. Clip tune = get. Audio. Clip ( URLPath, relative. Path. Name); . . . 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. 12
Playing Audio Clips (cont’d) • URLPath is the path part of the URL where the audio clip file is located. get. Document. Base() is often used for this parameter. • get. Document. Base() returns the path part of the absolute URL of this applet’s HTML file: Audio. Clip bells = get. Audio. Clip ( get. Document. Base(), “sounds/Bells. wav"); 13
Images • The JApplet class has a method get. Image that returns an Image object. import java. awt. Image; . . . private Image picture; . . . picture = get. Image (get. Document. Base(), "flower. gif"); . . . public void paint. Component(Graphics g) { Image. Observer. . . (often null) if (picture != null) g. draw. Image (picture, x, y, this); } 14
Images (cont’d) • In applications, it may be easier to work with Swing’s Image. Icon objects: import javax. swing. Image. Icon; . . . private Image. Icon picture; . . . picture = new Image. Icon ("flower. gif"); . . . public void paint. Component (Graphics g) {. . . if (picture != null) picture. draw. Icon (this, g, x, y); } The component on which this icon is displayed (usually this) 15
Images (cont’d) • Icons can be placed on JButtons, JLabels, JMenu. Items, etc: import javax. swing. Image. Icon; . . . Image. Icon arrow = new Image. Icon ("rightarrow. gif"); JButton next = new JButton(“Next”, arrow); • Icon’s get. Image method returns its image (an Image object). 16
The Drawing Editor Program 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 (“balloons”). • Uses JColor. Chooser to pick a color. 18
Drawing Editor (cont’d) Balloon. Draw Control. Panel Drawing. Panel Balloon • You write the whole program; there is a detailed step-by-step plan in the book (Section 18. 4). 19
Review: • Name the five methods of the Mouse. Listener interface. • Can a class implement Mouse. Motion. Listener but not Mouse. Listener? • What are the units and the origin for the coordinates returned by the Mouse. Event’s get. X and get. Y methods? • How many methods does the Key. Listener interface specify? 20
Review (cont’d): • Which Key. Listener’s method is used to capture an action key event? • Which Key. Event’s method returns the actual character typed? • Which Key. Event’s methods return the status of modifier keys? • When do we need a request. Focus() call? 21
Review (cont’d): • What does an applet’s get. Document. Base() method return? • Which class was written earlier: Image or Image. Icon? • How can we display an icon on a panel (a JPanel object)? • How can we display an icon on a button (a JButton object)? 22
- Slides: 22