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,

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

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
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.
TCU CoSc Programming with Java Handling Events.
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.
Event Handling Events and Listeners Timers and Animation.
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,
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
GUI Programming in Java
Java Implementation Software Construction Lecture 6.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
(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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
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.
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.
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.
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.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
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.
Events and Event Handling
Web Design & Development Lecture 11
Mouse Event Handling in Java (Review)
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
Ellen Walker Hiram College
CS 106A, Lecture 14 Events and Instance Variables
Introduction to Computing Using Java
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Border Layout, using Panels, Introduction to PinBallGame
Presentation transcript:

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, D. Mickunas, E. Reingold

Chapter Preview In this chapter we will: introduce event driven programming show how a program can respond to mouse events (e.g. clicks and mouse movements) demonstrate how to implement a listener interface show how mouse events can be used to build highly interactive programs

Abstract Windowing Toolkit (AWT) Part of the Java distribution, but not part of the Java language itself AWT is library of classes and methods used by Java programmers that supported are by the basic Java runtime libraries Even though this chapter is labeled optional, it is not possible to be a complete Java programmer without understanding the AWT

Java Events Events are occurrences outside of the program to which the program must respond (e.g. user key press) Event managers are methods that first catch the event Listener classes send messages to an event manager requesting to be notified when a particular message occurs Event managers send messages to listeners notifying them when a particular event occurs

Creating Mouse Click Listeners 1.Import the java.awt.event package 2.In the class header, add the words implements MouseListener 3.Send the addMouseListerner(this) message to the mouse event manager (should be done in the constructor method) 4.Define methods mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited

Mouse Click Listener Template import java.awt.event.*; … public class classname implements MouseListener { … // include manager.addMouseListener(this); // in some initialization method … public void mouseClicked (MouseEvent e) { … } public void mousePressed (MouseEvent e) { … } public void mouseReleased (MouseEvent e) { … } public void mouseEntereded (MouseEvent e) { … } public void mouseExited (MouseEvent e) { … } }

Finding Mouse Location Inside mouseClicked the argument e of type MouseEvent can be used to find the location of the mouse cursor The call e.getX( ) returns the value of the horizontal coordinate of the mouse cursor position The call e.getY( ) returns the value of the vertical coordinate of the mouse cursor position

Creating Mouse Motion Listeners 1.Import the java.awt.event package 2.In the class header, add the words implements MouseMotionListener 3.Send the message addMouseMotionListerner(this) to the mouse event manager (should be done in the constructor method) 4.Define methods mouseMoved and mouseDragged

Mouse Motion Listener Template import java.awt.event.*; … public class classname implements MouseMotionListener { … // include manager.addMouseListener(this); // in some initialization method … public void mouseMoved (MouseEvent e) { … } public void mouseDragged (MouseEvent e) { … } }

Mouse Motion mouseMoved will be called each time the mouse makes a significantly large movement mouseDragged will be called instead if thre mouse button is pressed and the mouse makes a significantly large movement

Listening to All Mouse Events Two catch all seven mouse events use the following class header public class classname implements MouseListener, MouseMotionListener { You then need to define all seven mouse methods mouseMoved, mouseDragged, mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited