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

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
© The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
Event Handling Events and Listeners Timers and Animation.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
OOP (Java): GUI II/ OOP Objectives – –describe some more GUI features: JPanel, and mouse listeners/adapters Semester 2,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Layout Managers Arranges and lays out the GUI components on a container.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
COSC 4126 User Interaction User Interaction capturing and responding to input events.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
Creating User Interfaces Event-Driven Programming.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Applets.
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Presentation transcript:

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

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.

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

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(); }

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

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

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);

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.

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’)... }

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.

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.

16-12 The Drawing Editor Applet

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.

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

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 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.

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

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)

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)

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);

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);

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?

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?

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)?