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.

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.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
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.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Animation Most games have moving components, how do we make things move? –We will track the location of an object using an x,y coordinate –We will draw.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
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,
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
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.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
– 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.
The Drawing program – Java Applets
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
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.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
Object-Oriented Software Engineering Using Threads and simple Animation.
CS Lecture 04 Mice Lynda Thomas
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Lesson 33: Layout management and drawing – Java GUI.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
Quick topix 11/7/13 CSE 1102 Fall previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________.
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.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
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.
Lecture 8 Object Oriented Programming Using Java
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
Quick intro to Event Driven Programming
CS 106A, Lecture 14 Events and Instance Variables
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Presentation transcript:

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 and MouseMotionListener this is a bit more involved than the ActionListener for the Timer because there are 7 methods that must be implemented between the two Listeners –Fortunately, we can implement most as empty methods public void mouseMoved(MouseEvent e) { }// does nothing –We will often keep track of where the mouse is or where it was and now where it has been moved to example: when the mouse button is pressed, remember it’s x and y coordinates, when the mouse button is released, remember the new x and y coordinates, now draw a line from (old x, old y) to (new x, new y) –In such a way, we can build a “paint” type program

Mouse Listeners and Methods Two types of listeners for the mouse: –MouseListener – deals with mouse button operations and must implement the following methods: mousePressed mouseReleased mouseClicked mouseEntered mouseExited –when you hold down the mouse button or click the mouse button, both mousePressed and mouseClicked are called, when you release the mouse button or click the mouse button, both mouseReleased and mouseClicked are called –we won’t be using mouseEntered or mouseExited here –MouseMotionListener – deals with moving the mouse mouseMoved mouseDragged To specify that your program should listen for mouse events, you add the following instructions in your constructor: –addMouseListener(this); –addMouseMotionListener(this);

Example: Drawing a line Here, we see how to use the MouseListener to draw a line on a JPanel when the mouse is dragged –As usual, we have created a JFrame which itself adds the JPanel below (MouseExample) to its contents public class MouseExample extends JPanel implements MouseListener { private int x1, x2, y1, y2; public MouseExample( ) { x1 = x2 = y1 = y2 = 0; addMouseListener(this); } public void mousePressed(MouseEvent e) { x1 = e.getX( ); y1 = e.getY( ); } public void mouseReleased(MouseEvent e) { x2 = e.getX( ); y2 = e.getY( ); repaint( ); } public void paintComponent(Graphics g) { g.setColor(Color.black); g.drawLine(x1, y1, x2, y2); } } // end class We would also implement mouseEntered, mouseExited and mouseClicked as { }

Example 2: Drawing dots Here, we place a dot every time you drag the mouse public class DotDrawer extends JPanel implements MouseMotionListener { private static int x, y; public DotDrawer( ) { x = y = 0; addMouseMotionListener(this); } public void mouseDragged(MouseEvent e) { x = e.getX( ); y = e.getY( ); repaint( ); } public void mouseMoved(MouseEvent e) { } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.blue); g.fillOval(x-1, y-1, 2, 2); } } // end class

Storing Graphics Items If you notice, the previous program performs super.paintComponent(g); while the program before did not –this instruction cases the JPanel’s Graphics object to clear itself –if we do this, then we lose what we had already drawn –but if we don’t do this, the image can become cluttered What we want to do is perform this operation every time we do repaint( ) but we want to also draw each previous Graphics item on the screen –so we have to remember everything we had already done –we will store each item by using arrays –for our dots program, we can store each dot’s x and y coordinate an two arrays, x[i] and y[i] (for dot i) This is the first step toward building a more interesting Graphics program –Paint/drawing program –Computer game that has multiple Graphics items on the screen