Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,

Similar presentations


Presentation on theme: "Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,"— Presentation transcript:

1 Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

2 16-2 Objectives: l Learn how to handle mouse and keyboard events in Java. l Implement a simple drawing editor application. l Learn the basics of playing sounds and displaying images in applets.

3 16-3 Mouse Events l Mouse events are captured by an object which is a MouseListener and possibly a MouseMotionListener. l A mouse listener is usually attached to a JPanel component. l It is not uncommon for a panel to serve as its own mouse listener: addMouseListener(this); addMouseMotionListener(this); // optional

4 16-4 Mouse Events (cont’d) l Mouse listener methods receive a MouseEvent object as a parameter. l 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 clicks = e.getClickCount(); }

5 16-5 Mouse Events (cont’d) l 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) l 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

6 16-6 Mouse Events (cont’d) l The MouseMotionListener interface adds two methods: void mouseMoved (MouseEvent e) void mouseDragged (MouseEvent e) l These methods are usually used together with MouseListener methods (i.e., a class implements both interfaces). Called when the mouse has moved with a button held down

7 16-7 Keyboard Events l Keyboard events are captured by an object which is a KeyListener. l A key listener object must first obtain keyboard “focus.” This is done by calling the component’s requestFocus method. l If keys are used for moving objects (as in a drawing program), the “canvas” panel may serve as its own key listener: addKeyListener(this);

8 16-8 Keyboard Events (cont’d) l The KeyListener interface defines three methods: void keyPressed (KeyEvent e) void keyReleased (KeyEvent e) void keyTyped (KeyEvent e) l One key pressed and released causes several calls.

9 16-9 Keyboard Events (cont’d) l Use keyTyped to capture character keys (i.e., keys that correspond to printable characters). l e.getKeyChar() returns a char, the typed character: public void keyTyped (KeyEvent e) { char ch = e.getKeyChar(); if (ch == ‘A’)... }

10 16-10 Keyboard Events (cont’d) l Use keyPressed or keyReleased to handle “action” keys, such as cursor keys,, function keys, and so on. l e.getKeyCode() returns and int, the key’s “virtual code.” l The KeyEvent class defines constants for numerous virtual keys. For example: VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN VK_HOME, VK_END, VK_PAGE_UP,...etc. Cursor keys Home, etc.

11 16-11 Keyboard Events (cont’d) l e.isShiftDown(), e.isControlDown(), e.isAltDown() return the status of the respective modifier keys. l e.getModifiers() returns a bit pattern that represents the status of all modifier keys. l KeyEvent defines “mask” constants CTRL_MASK, ALT_MASK, SHIFT_MASK, and so on.

12 16-12 The Drawing Editor Applet

13 16-13 Drawing Editor (cont’d) l Allows to move and shape objects on “canvas” using the mouse and the cursor keys. l Here the shapes are circles (“balloons”). In the next chapter extended to different shapes. l Uses JColorChooser to pick a color.

14 16-14 Drawing Editor (cont’d) DrawingPanel canvas Balloon list[] DrawBalloons (applet)

15 16-15 Drawing Editor (cont’d) l The canvas maintains a list of balloons and an “active” (last added) balloon. l The Balloon class is provided; has methods to determine whether a given point is inside or “on the border,” and to draw. l You write the GUI/graphics part; there is a detailed step-by-step plan in the book.

16 16-16 Playing Audio Clips l The JApplet class has a method getAudioClip that returns an AudioClip object. import java.applet.AudioClip;... AudioClip tune = getAudioClip ( URLPath, ralativePathName);... tune.play(); There may be a slight delay. The audio clip is actually loaded only when you call its play method for the first time.

17 16-17 Playing Audio Clips (cont’d) l URLPath is the path part of the audio clip file is located. getDocumentBase() is often used for this parameter. l getDocumentBase() returns the path part of the absolute URL of this applet’s HTML file: AudioClip bells = getAudioClip ( getDocumentBase(), "audio/Bells.wav"); Use forward slashes for compatibility

18 16-18 Images l 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) {... if (picture != null) g.drawImage (picture, x, y, this); } ImageObserver (usually this)

19 16-19 Images (cont’d) l 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)

20 16-20 Images (cont’d) l In applets, the ImageIcon constructors expect an absolute URL: URL absUrl; try { absUrl = new URL(getDocumentBase(), iconFileName); } catch (MalformedURLException e) { System.err.println(...); } ImageIcon icon = new ImageIcon(absUrl);

21 16-21 Images (cont’d) l Icons can be added to JButtons, JLabels, JMenuItems, etc: l Icon’s getImage method returns its image (an Image object). import javax.swing.ImageIcon;... ImageIcon arrow = new ImageIcon ("rightarrow.gif"); JButton next = new JButton(“Next”, arrow);

22 16-22 Review: l Name the five methods of the MouseListener interface? l Can a class implement MouseMotionListener but not MouseListener? l What are the units and the origin for the coordinates returned by the MouseEvent’s getX and getY methods? l How many methods does the KeyListener interface specify?

23 16-23 Review (cont’d): l Which KeyListener’s method is used to capture an action key event? l Which KeyEvent’s method returns the actual character typed? l Which KeyEvent’s methods return the status of modifier keys? l When do we need a requestFocus() call?

24 16-24 Review (cont’d): l What does an applet’s getDocumentBase() method return? l Which class was written earlier: Image or ImageIcon? l How can we display an icon in a panel (a JPanel object)? l How can we display an icon on a button (a JButton object)?


Download ppt "Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,"

Similar presentations


Ads by Google