Event Driven Programming The MVC Framework And Simulating

  • Slides: 20
Download presentation
Event Driven Programming, The MVC Framework And Simulating Flocking Animation Liang, Introduction to Java

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. 0132130807 1

Events An event is an object that represents some activity to which a program

Events 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: q the mouse is moved q the mouse is dragged q a mouse button is clicked q a graphical button is clicked q a keyboard key is pressed q 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. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Events Driven Programming Event-Driven Programming is when you write a program primarily to handle

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

Events and Listeners q The Java standard class library contains several classes that represent

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

Events and Listeners Component Listener A component object may generate an event A corresponding

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

GUI Development q Generally we use components and events that are predefined by classes

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

Key. Listener Example q Listener classes are written by implementing a listener interface which

Key. Listener Example q Listener classes are written by implementing a listener interface which list the methods that the implementing class must define q 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 q Key. Adapter implements the Key. Listener interface q One method in the Key. Listener interface that is implemented in the Key. Adapter class is the Key. Typed method, and this method is sent the event when a keyboard character is typed q The Java class library contains interfaces for many event types Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Key Events q q q A key event is generated when the user types

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

Key Events q The component that generates a key event is the one that

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

Action. Listener Example q The Action. Listener is the listener interface that you can

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

Action. Listener Example q One Action. Listener object can be used to listen to

Action. Listener Example q One Action. Listener object can be used to listen to two different components q The source of the event can be determined by using the get. Source() method of the event passed to the listener q See Left. Right. Panel. java See Left. Right. java q Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

Mouse. Listener Example q q The Mouse. Listener interface listens for the following Mouse.

Mouse. Listener Example q q The Mouse. Listener interface listens for the following Mouse. Events: mouse pressed the mouse button has been pressed without releasing mouse clicked the mouse button has been clicked (pressed and released) mouse released the mouse button has been released mouse entered the mouse cursor has entered a component mouse exited the mouse cursor has exited a component See Bubble. java, Bubbles. Panel. java and Bubbles. java Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The MVC Framework q The Model—View—Controller architecture is widely used to help deal with

The MVC Framework q 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. q Typically, programmers will work with classes in these different groups in different ways. q Sometimes the classes in a group are provided in a library and sometimes they written by the programmer for the application. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The MVC Framework q Model Ø The model contains classes that represent the data

The MVC Framework q 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. q View Ø The view is what the user sees. This is the visible interface. q Controller Ø The controller contains code that is executed in response to user actions and some other external events. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The MVC Framework For Simulating Flocking Animation q The Model—code which computes how an

The MVC Framework For Simulating Flocking Animation q 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. q The View—code which displays the entire set of birds in their individual position in the GUI. q The Controller—code which initiates the program and that responds to user actions. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The Flocking Behavior Model Flocking behavior is the behavior exhibited when a group of

The Flocking Behavior Model 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 Lion. King-Stampede. Scene. mp 4 (uses Reynolds’ algorithm) Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The Flocking Behavior Model The three basic rules for controlling flocking behavior are the

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

The Flocking Simulation Behavior Model The Dove. Flock. Model. java class extends the Dove

The Flocking Simulation Behavior Model The Dove. Flock. Model. 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). q See Dove. Flock. Model. java Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The Flocking Simulation View The Dove. Flock. View. java class extends the JPanel class

The Flocking Simulation View The Dove. Flock. View. java class extends the JPanel class and manages a view for a list of Dove. Flock. Model 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. q See Dove. Flock. View. java Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

The Flocking Simulation Controller The Dove. Flock. Contoller. java class extends the JFrame class

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