Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.

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

Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
ITEC220 GUI Lecture – Part 2 References  Java Software Solutions,” by Lewis & Loftus  Chapter 7  Chapter 8  Chapter 9  Java Foundations-Introduction.
Event Handling.
Event Handling Events and Listeners Timers and Animation.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.
Chapter 8: Animation Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used with.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Chapter Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted.
ELC 312 Day 16. © 2004 Pearson Addison-Wesley. All rights reserved Agenda Questions? Capstone Proposals Due Problem set 3 Corrected  Poor performance.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Chapter 5: Enhancing Classes
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Graphical User Interfaces Revisited Material from Chapters
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
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.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Introduction to Java Programming
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
Polygons and Polylines © 2004 Pearson Addison-Wesley. All rights reserved 7-1 Arrays can be helpful in graphics processing For example, they can be used.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling CS 21a: Introduction to Computing I First Semester,
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Chapter 5: Enhancing Classes
Chapter 9: Graphical User Interfaces
Chapter 12 Event-Driven Programming
Chapter 5: Enhancing Classes
Event-driven programming
Processing Timer Events
Chapter 5: Enhancing Classes
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Event Handling CS 21a: Introduction to Computing I
GUI Programming III: Events
Chapter 7: Inheritance Java Software Solutions
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Event Driven Programming
Event loops.
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Chapter 6: Arrays and Vectors
Tonga Institute of Higher Education
Event loops.
Chapter 8: Exceptions and I/O Streams
Event loops.
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2000 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes.

Events b An event is an object that represents some activity to which we may want to respond b For example, we may want our program to perform some action when the following occurs: the mouse is movedthe mouse is moved a mouse button is clickeda mouse button is clicked the mouse is draggedthe mouse is dragged a graphical button is clickeda graphical button is clicked a keyboard key is presseda keyboard key is pressed a timer expiresa timer expires b Often events correspond to user actions, but not always

Events b The Java standard class library contains several classes that represent typical events b Certain objects, such as an applet or a graphical button, generate (fire) an event when it occurs b Other objects, called listeners, respond to events b We can write listener objects to do whatever we want when an event occurs

Events and Listeners Generator This object may generate an event Listener This object waits for and responds to an event Event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event

Listener Interfaces b We can create a listener object by writing a class that implements a particular listener interface b The Java standard class library contains several interfaces that correspond to particular event categories  For example, the MouseListener interface contains methods that correspond to mouse events b After creating the listener, we add the listener to the component that might generate the event to set up a formal relationship between the generator and listener

Mouse Events b The following are mouse events: mouse pressed - the mouse button is pressed downmouse pressed - the mouse button is pressed down mouse released - the mouse button is releasedmouse released - the mouse button is released mouse clicked - the mouse button is pressed and releasedmouse clicked - the mouse button is pressed and released mouse entered - the mouse pointer is moved over a particular componentmouse entered - the mouse pointer is moved over a particular component mouse exited - the mouse pointer is moved off of a particular componentmouse exited - the mouse pointer is moved off of a particular component b Any given program can listen for some, none, or all of these b See Dots.java (page 246) Dots.java b See DotsMouseListener.java (page 248) DotsMouseListener.java

Mouse Motion Events b The following are called mouse motion events: mouse moved - the mouse is movedmouse moved - the mouse is moved mouse dragged - the mouse is moved while the mouse button is held downmouse dragged - the mouse is moved while the mouse button is held down  There is a corresponding MouseMotionListener interface b One class can serve as both a generator and a listener b One class can serve as a listener for multiple event types b See RubberLines.java (page 249) RubberLines.java

Key Events b The following are called key events: key pressed - a keyboard key is pressed downkey pressed - a keyboard key is pressed down key released - a keyboard key is releasedkey released - a keyboard key is released key typed - a keyboard key is pressed and releasedkey typed - a keyboard key is pressed and released  The KeyListener interface handles key events b Listener classes are often implemented as inner classes, nested within the component that they are listening to b See Direction.java (page 253) Direction.java

Animations b An animation is a constantly changing series of pictures or images that create the illusion of movement b We can create animations in Java by changing a picture slightly over time  The speed of a Java animation is usually controlled by a Timer object  The Timer class is defined in the javax.swing package

Animations  A Timer object generates an ActionEvent every n milliseconds (where n is set by the object creator)  The ActionListener interface contains an actionPerformed method  Whenever the timer expires (generating an ActionEvent ) the animation can be updated b See Rebound.java (page 258) Rebound.java