Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.

Slides:



Advertisements
Similar presentations
Chapter 14 Advanced GUI ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Advertisements

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.
Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Inner Classes Ancora eventi. 2 Design considerations The most important rule to keep in mind about event listeners that they should execute very quickly.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
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.
JAVA1 1. JFrame. JAVA2 JApplet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HerhalingAppletH10 extends JApplet implements.
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).
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.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
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.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
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,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
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.
Swing II. Swing II Topics WindowListener interface WindowListener interface Icons Icons Scrollbars Scrollbars The Graphics class The Graphics class Colors.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Layout Managers Arranges and lays out the GUI components on a container.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
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.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
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.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
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.
Event Handling. User actions are called “events” – Low-level window events Window changes – Low-level component events Mouse events, Keyboard events,
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
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.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Lecture 8 Object Oriented Programming Using Java
Handling User Events with Swing
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
Interfaces.
Programming in Java Event Handling
Introduction to Computing Using Java
Swing II.
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Web Design & Development Lecture 13
Presentation transcript:

Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor spreco di risorse In Java

User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window)WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visibleComponentListener Table or list selection changesListSelectionListener Un oggetto può essere notificato delloccorrenza di un evento registrandosi come event listener implementando lapposita interfaccia. Ogni Swing Component è un event source e può avere più EventListener in ascolto Loccorrenza di un evento chiama la rispettivo metodo dellEventListener. (es. mouse clicked MouseListener.mouseClicked() ) Handling Mouse Events

MouseEvents (il puntatore entra o esce da un componente, lutente preme un bottone) MouseListener (Eventi che richiedono il tracciamento del movimento del cursore) MouseMotionListener (MouseWheelEvents MouseWheelListener) Handling Mouse Events

MouseMotionListener Interface MethodPurpose mouseDragged(MouseEvent) Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component. mouseMoved(MouseEvent) Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor. Handling Mouse Events MouseListener Interface MethodPurpose mouseClicked(MouseEvent) Called just after the user clicks the listened-to component. mouseEntered(MouseEvent) Called just after the cursor enters the bounds of the listened-to component. mouseExited(MouseEvent) Called just after the cursor exits the bounds of the listened-to component. mousePressed(MouseEvent) Called just after the user presses a mouse button while the cursor is over the listened-to component. mouseReleased(MouseEvent) Called just after the user releases a mouse button after a mouse press over the listened-to component.

Handling Mouse Events MouseEvent Class MethodPurpose int getClickCount() Return the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click. int getX() int getY() Point getPoint() Return the (x,y) position at which the event occurred, relative to the component that fired the event. int getButton() Return which mouse button, if any, has changed state. One of the following constants is returned: NOBUTTON, BUTTON1, BUTTON2, or BUTTON3. Introduced in release 1.4. boolean isPopupTrigger() Return true if the mouse event should cause a popup menu to appear. Because popup triggers are platform dependent, if your program uses popup menus, you should call isPopupTrigger for all mouse-pressed and mouse-released events fired by components over which the popup can appear. See Bringing Up a Popup Menu for more information about popup menus.Bringing Up a Popup Menu String getMouseModifiersText(int) Return a String describing the modifier keys and mouse buttons that were active during the event, such as "Shift", or "Ctrl+Shift". These strings can be localized using the awt.properties file. Introduced in release 1.4.

Implementando le Interfacce MouseListener e MouseMotionListener si è costretti ad implementare tutti i metodi Spesso ci interessano solo alcuni eventi (pochi metodi) Classi Adapter (estendono le interfacce e danno una imlementazione di default dei metodi) MouseAdapter mplements MouseListener MouseMotionAdapter implements MouseMotionListener MouseInputadapter implements MouseInputListener and MouseMotionListener Handling Mouse Events

Esempio: MouseXY Handling Mouse Events

class MouseXY extends Frame implements WindowListener, MouseListener { public MouseXY(String text) { super(text); addWindowListener(this); addMouseListener(this); } public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} ……. Handling Mouse Events Esempio: MouseXY

public void mousePressed(MouseEvent event) { int xCoord = event.getX(); int yCoord = event.getY(); Graphics g = getGraphics(); g.drawString("+ [" + String.valueOf(xCoord) + "," + String.valueOf(yCoord) + "]", xCoord,yCoord); } Handling Mouse Events Esempio: MouseXY

public void windowClosed(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowIconified(WindowEvent event) {} public void windowActivated(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} public void windowOpened(WindowEvent event) {} public void windowClosing(WindowEvent event) { System.exit(0); } public static void main(String[] args) { MouseXY screen = new MouseXY("Mouse example"); screen.setSize(400,300); screen.setVisible(true); } Handling Mouse Events Esempio: MouseXY

Handling Mouse Events Esempio: MousePainter

public class MousePainter extends JFrame{ private int xValue = -10, yValue = -10; boolean rubber = false; Label label; Button toolButton; public MousePainter () { super("Mouse Painter Example"); label = new Label("Drag mouse to paint"); toolButton = new Button("Use Rubber"); toolButton.addMouseListener( new MouseInputAdapter () { public void mouseClicked(MouseEvent e){ if (e.getButton() == 1) { String newLabel = rubber ? "Use rubber" : "Use pen"; toolButton.setLabel(newLabel); rubber = rubber ? false : true; } ); Handling Mouse Events Esempio: MousePainter

getContentPane().add(toolButton,BorderLayout.NORTH); getContentPane().add(label,BorderLayout.SOUTH); setSize(350,350); addMouseMotionListener ( new MouseMotionAdapter () { public void mouseDragged(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); repaint(); } ); show(); }; Handling Mouse Events Esempio: MousePainter

public void paint(Graphics g) { if (rubber) { g.clearRect(xValue,yValue,15,15); } else { g.fillOval(xValue,yValue,3,3); }; public static void main(String[] args){ MousePainter app = new MousePainter(); app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } ); } Handling Mouse Events Esempio: MousePainter

addMouseListener ( new MouseInputAdapter () { public void mouseDragged(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); repaint(); } public void mouseClicked(MouseEvent e){ if (e.getComponent().equals(toolButton)) { String newLabel = rubber ? "Use rubber" : "Use pen"; toolButton.setLabel(newLabel); rubber = rubber ? false : true; } ); Handling Mouse Events Esempio: MousePainter