Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Event Driven Programming, The.

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

Introduction to Java 2 Programming
Graphic User Interfaces Layout Managers Event Handling.
ITEC220 GUI Lecture – Part 2 References  Java Software Solutions,” by Lewis & Loftus  Chapter 7  Chapter 8  Chapter 9  Java Foundations-Introduction.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Event-Driven Programming
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.
Graphical User Interface (GUI) Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
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.
Copyright © 2012 Pearson Education, Inc. Chapters 3-8 Graphical User Interfaces Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
Creating User Interfaces Event-Driven Programming.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
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,
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Programming in Java Event Handling
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Event Driven Programming
Chapter 15 Event-Driven Programming and Animations
Event loops.
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Event loops.
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The MVC Framework And Simulating Flocking Animation

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved An event is an object that represents some activity to which a program can respond. For example, the program may perform some action when the following occurs:  the mouse is moved  the mouse is dragged  a mouse button is clicked  a graphical button is clicked  a keyboard key is pressed  a timer expires Events often correspond to user actions, but not always, such as when a timer triggers an event that indicates a certain amount of time has elapsed. Events

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event-Driven Programming is when you write a program primarily to handle events as they happen. Handling an event is executing some code when an event occurs. The general pattern is that for selected events, each event is associated with a method. When the event occurs, the method is executed. Typically, the method will have parameters that are used to pass helpful data to the method. Events are the driving force behind most GUI applications. Once the application is initialized, events drive what happens through their interaction with the event handlers. In Java, the event handlers are called listeners. Events Driven Programming

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  The Java standard class library contains several classes that represent typical events  Components, such as a graphical button, generate (or fire) an event when it occurs  A listener object "waits" for an event to occur and responds accordingly  We can design listener objects to take whatever actions are appropriate when an event occurs Events and Listeners

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event Event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event Events and Listeners

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  Generally we use components and events that are predefined by classes in the Java class library  Therefore, to create a Java program that uses a Graphical User Interface (GUI) we must:  instantiate and set up the necessary components (like JFrames)  implement listener classes for any events we care about  establish the relationship between listeners and components that generate the corresponding events  Let's now look at one such listener that works for the JFrame components and see how this all comes together GUI Development

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  Listener classes are written by implementing a listener interface which list the methods that the implementing class must define  Adapter classes implement the methods of a listener class using empty method bodies so you can inherit the Adapter class and then just override the methods you intend to use, without having to show empty method bodies for those you won’t use  KeyAdapter implements the KeyListener interface  One method in the KeyListener interface that is implemented in the KeyAdapter class is the KeyTyped method, and this method is sent the event when a keyboard character is typed  The Java class library contains interfaces for many event types KeyListener Example

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  A key event is generated when the user types on the keyboard key presseda key on the keyboard is pressed down key releaseda key on the keyboard is released key typeda key on the keyboard is pressed down and released  Listeners for key events are created by implementing the KeyListener interface  A KeyEvent object is passed to the appropriate method when a key event occurs Key Events

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Key Events  The component that generates a key event is the one that has the current keyboard focus  Constants in the KeyEvent class can be used to determine which key was pressed  The following example "moves" an image of an arrow as the user types the keyboard arrow keys  See DirectionPanel.java  See Direction.java => How would you keep the arrow from going off the screen by adding code in the DirectionListener handler?

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  The ActionListener is the listener interface that you can use for handling the ActionEvents of many of the Swing GUI components.  You can create a separate component listener (like a ButtonListener or a TimerListener) that implement the ActionListener interface, which means it must have an actionPerformed() method to receive and handle ActionEvents.  If you do not want to create a separate component listener, you can write your own actionPerformed() method a component class that you have defined and handle ActionEvents there. ActionListener Example

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved ActionListener Example  One ActionListener object can be used to listen to two different components  The source of the event can be determined by using the getSource() method of the event passed to the listener  See LeftRightPanel.java  See LeftRight.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  The MouseListener interface listens for the following MouseEvents: MouseListener Example mouse pressedthe mouse button has been pressed without releasing mouse clickedthe mouse button has been clicked (pressed and released) mouse releasedthe mouse button has been released mouse enteredthe mouse cursor has entered a component mouse exitedthe mouse cursor has exited a component  See Bubble.java, BubblesPanel.java and Bubbles.java

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  The Model—View—Controller architecture is widely used to help deal with the complexity of GUI applications. Each of the three terms refers to a subset of the classes and objects that make up an application. The groupings are functional.  Typically, programmers will work with classes in these different groups in different ways.  Sometimes the classes in a group are provided in a library and sometimes they written by the programmer for the application. The MVC Framework

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  Model  The model contains classes that represent the data used by the application. This will also include methods for manipulating and computing with that data.  View  The view is what the user sees. This is the visible interface.  Controller  The controller contains code that is executed in response to user actions and some other external events. The MVC Framework

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved  The Model—code which computes how an individual bird in the flock behaves by implementing a model of how to compute the particular bird’s position at any point in the simulation.  The View—code which displays the entire set of birds in their individual position in the GUI.  The Controller—code which initiates the program and that responds to user actions. The MVC Framework For Simulating Flocking Animation

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Flocking behavior is the behavior exhibited when a group of birds, called a flock, are moving together in flight. There are parallels with the shoaling behavior of fish, the swarming behavior of insects, and herd behavior of land animals. Computer simulations that have been developed to emulate the flocking behaviors of birds can generally be applied to similar behavior of other species, and so the term "flocking" is often applied, in computer science, to species other than birds. Flocking behavior was first simulated on a computer in 1986 by Craig Reynolds with his simulation program, Boids, in which simple agents (boids) move according to a set of 3 basic rules. See LionKing-StampedeScene.mp4 (uses Reynolds’ algorithm)LionKing-StampedeScene.mp4 The Flocking Behavior Model

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved The three basic rules for controlling flocking behavior are the following:  Rule 1: Separation - avoid crowding neighbors (short range repulsion)  Rule 2: Alignment - steer towards average heading of neighbors  Rule 3: Cohesion - steer towards average position of neighbors (long range attraction)  See The Flocking Behavior Model

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved The DoveFlockModel.java class extends the Dove class and adds a move() method that implements the Flocking Algorithm rules. Along with those rules, that method also implements a simple rule for rebounding off of the boundaries of the borders of the screen. The class also adds a draw() method that resizes the doves to a given ratio determined by an argument passed to the constructor. The ratio allows you to create doves that are larger or smaller than the size of the normal Dove images (e.g. a ratio of 2.0 doubles the dove sizes, and 0.5 cuts them in half).  See DoveFlockModel.java The Flocking Simulation Behavior Model

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved The DoveFlockView.java class extends the JPanel class and manages a view for a list of DoveFlockModel objects (Doves). This class includes a constructor that sets up an initial list of Doves, and it also sets up the size and background color of the JPanel, and it instantiates a timer to handle updates to each of the Doves. The class also calls the methods that move and draw the Doves, and methods that add and remove Doves from the simulation, and a method that pauses or resumes the simulation.  See DoveFlockView.java The Flocking Simulation View

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved The DoveFlockContoller.java class extends the JFrame class and it acts as the controller for the simulation. This class instantiates the DoveFlockView.java and then adds buttons to the View that give the user controller of various elements of the simulation.  See DoveFlockController.java The Flocking Simulation Controller