1 / 20

Event Driven Programming, The MVC Framework And Simulating Flocking Animation

Event Driven Programming, The MVC Framework And Simulating Flocking Animation. 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: the mouse is moved the mouse is dragged

Download Presentation

Event Driven Programming, The MVC Framework And Simulating Flocking Animation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Driven Programming,The MVC FrameworkAnd SimulatingFlocking Animation

  2. 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: • 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.

  3. 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.

  4. Events and Listeners • 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

  5. Event Component Listener A component object may generate an event A corresponding listener object is designed to respond to the event Events and Listeners When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event

  6. GUI Development • 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

  7. KeyListener Example • 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

  8. Key Events • A key event is generated when the user types on the keyboard • Listeners for key events are created by implementing the KeyListenerinterface • A KeyEvent object is passed to the appropriate method when a key event occurs

  9. 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. ActionListener Example • 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.

  11. 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. MouseListener Example • The MouseListener interface listens for the following MouseEvents: • See Bubble.java, BubblesPanel.java and Bubbles.java

  13. The MVC Framework • 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.

  14. The MVC Framework • 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.

  15. The MVC FrameworkFor Simulating Flocking Animation • 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.

  16. 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 LionKing-StampedeScene.mp4(uses Reynolds’ algorithm)

  17. The Flocking Behavior Model 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/

  18. The Flocking Simulation Behavior Model 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

  19. The Flocking Simulation View 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

  20. The Flocking Simulation Controller 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

More Related