Event Handling Events and Listeners Timers and Animation.

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 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
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.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
GUI Programming in Java
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,
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Programming: Guided Learning with Early Objects
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
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.
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.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
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.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
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.
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.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Events and Event Handling
CompSci 230 S Programming Techniques
Advanced User Interfaces
Programming in Java Event Handling
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Event-driven programming for GUI
CSE 331 Software Design and Implementation
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Making Java GUIs Functional
Final project.
Presentation transcript:

Event Handling Events and Listeners Timers and Animation

Computer Graphics and User Interfaces Event Handling The event-handling model of Java is based on the concept known as the delegation-based event model. With this model, event handling is implemented by two types of objects: –event sources - most components can generate events –event listeners

Computer Graphics and User Interfaces Event Handling Most components can generate events java.awt.Event is the superclass for all events –ActionEvent –ItemEvent –MouseEvent –… Each component that generates an event needs to know what listener to send the event to

Connecting Source and Listener JButton Handler event source event listener notify register A listener must be registered to a event source. Once registered, it will get notified when the event source generates events.

Computer Graphics and User Interfaces Listener Interfaces Each event class has a corresponding Listener interface –ActionListener –ItemListener –MouseListener –… When you create a component, you need to tell it which listener should get the events it generates –the component classed have add___Listener methods for doing this

Computer Graphics and User Interfaces ActionListener public interface ActionListener extends EventListener { public void actionPerformed( ActionEvent e); } Any class that implements this interface needs to define the actionPerformed method which has code to execute the desired response to the event.

Computer Graphics and User Interfaces Implementing Listeners One approach is to have the container class that holds the components implement the listener interface. –This is the approach used by your text You can also create inner classes that implement the appropriate listener interface.

Computer Graphics and User Interfaces Timer Timer is a Component that can be used to generate events at regular intervals –Use it for creating animated graphics –A Timer generates ActionEvents

Computer Graphics and User Interfaces Using a Timer public class extends JApplet public void init() { panel = new (); Timer alarm = new Timer( int millis, panel); } public class extends JPanel implements ActionListener { public void paintComponent( Graphics g) { //drawing depends on some state variable(s) } public void actionPerformed( ActionEvent e) { // modify state of panel and repaint }

Computer Graphics and User Interfaces Building Menus JMenuBar –JFrame and JApplet have a JMenuBar associated with them JMenu –Add JMenu objects to the JMenuBar JMenuItem –Add JMenuItems or JMenus to a Jmenu See DrawShapes.java, Transformations.java

Handling Mouse Events Mouse events include such user interactions as –moving the mouse –dragging the mouse (moving the mouse while the mouse button is being pressed) –clicking the mouse buttons. Mouse interactions are handled by two different Listener interfaces –MouseListener handles events generated by the mouse buttons –MouseMotionListener handles mouse movement

MouseListener Three methods for button events –mousePressed –mouseReleased –mouseClicked - both clicked and released Two methods detect when the mouse moves over and away form a component –mouseEntered –mouseExited All have a MouseEvent parameter Generally only need one or two of these –Others will have empty bodies

MouseMotionListener Handles mouse movement –mouseMoved –mouseDragged - mouse moves with button down Parameter is a MouseEvent Usually use mouseDragged in conjunction with mousePressed

Computer Graphics and User Interfaces MouseEvent getPoint returns the coordinates of the mouse when the event occurred There are methods for determining which button was pressed and whether a meta-key was also pressed

Computer Graphics and User Interfaces Using Components To make your program more interactive, add JButtons and JTextFields Both generate ActionEvents –JButton when it is clicked –JTextField when the enter key is pressed Generally divide the JFrame or JApplet into areas and put the components in a separate panel from the drawing panel

Computer Graphics and User Interfaces JApplet as Listener The interactive components are created in the JApplet –If they are instance variables, a JApplet Listener can identify the component event.getSource == componentName –They are used to modify parameters in the JPanel The panel class needs set methods for the parameters that need to change These parameters need to be instance variables Example: transformType in TransformPanel

Computer Graphics and User Interfaces JPanel as Listener If the panel is the listener, it doesn't have direct access to the Component –could pass references to the panel but not usually done this way How to identify the component –If there is only one component of a particular type, check the type with instanceOf –ActionEvent has a getActionCommand which can be used to identify a component like a JMenuItem or a JButton