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.

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

Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Web Design & Development Lecture 19. Java Graphics 2.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
 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).
CSE 341, S. Tanimoto Java-PolyDraw- 1 A Java Case Study: An Applet for Drawing Polygons Motivation -- See a number of AWT features integrated. Get ideas.
Graphical User Interfaces, 2D Graphics & Game Programming.
TCU CoSc Programming with Java Handling Events.
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.
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.
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.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Event Handling Events and Listeners Timers and Animation.
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,
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
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,
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications.
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.
– 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.
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.
Layout Managers Arranges and lays out the GUI components on a container.
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.
Java Direct Manipulation Chris North cs3724: HCI.
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.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
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.
Handling User Events with Swing
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Presentation transcript:

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 JButtons and other GUI components, mouse movements generate Events –To have your program respond, you must implement either or both of MouseListener and MouseMotionListener this is a bit more involved than the action listener 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)

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 will add a mouse listener or motion listener as in –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

Using Graphics and GUI Together Imaging that we want to create a Paint program –The Paint program will have a drawing area like we just saw –It also needs JButtons to control such things as changing shapes and colors Can we just add JButtons to our DotDrawer? –Unfortunately no, if we did that, the GUI interferes with the Graphics object causing some bizarre things to happen –So instead, we will create 2 JPanels in our main and add them both to the JFrame –The 2 JPanels will be of different classes One class will implement MouseListener/MouseMotionListener One class will implement ActionListener –See the Paint program for details