Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Event Driven Programming, The."— Presentation transcript:

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

2 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

6 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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?

10 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

12 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

14 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807  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

16 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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 http://www.red3d.com/cwr/boids/ The Flocking Behavior Model

18 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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

20 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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


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

Similar presentations


Ads by Google