Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.

Similar presentations


Presentation on theme: "Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods."— Presentation transcript:

1 Event Driven (Asynchronous) Programming

2 Event handling in Unity Subclass a class that contains event handling methods, and then override those methods. Subclass a class that contains event handling methods, and then override those methods. Ex. MonoBehaviour class Ex. MonoBehaviour class Update is called every frame, if the MonoBehaviour is enabled. Update is called every frame, if the MonoBehaviour is enabled. OnMouseOver is called every frame while the mouse is over the GUIElement or Collider. OnMouseOver is called every frame while the mouse is over the GUIElement or Collider. OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.

3 Event driven programming in Java Uses a signal-and-response approach Uses a signal-and-response approach events and event handlers events and event handlers asynchronous asynchronous Event = object that act as a signal to another object Event = object that act as a signal to another object Listener = event receiver Listener = event receiver One event might have zero or more listeners. One event might have zero or more listeners. Listeners can receive events from different objects. Listeners can receive events from different objects.

4 GUI Graphical User Interface Graphical User Interface event driven programming event driven programming Java GUI programming Java GUI programming 1. AWT (Abstract Window Toolkit) 2. Swing

5 Event driven programming One event might have zero or more listeners. One event might have zero or more listeners. Button X Listener A Listener B Listener C

6 Event driven programming Listeners can receive events from different objects. Listeners can receive events from different objects. Button Y Button Z Button X Listener A Listener B

7 Typical events User moves the mouse. User moves the mouse. User clicks the mouse button. User clicks the mouse button. User clicks the mouse button in a button in a window. User clicks the mouse button in a button in a window. User presses a key on the keyboard. User presses a key on the keyboard. Timer event occurs. Timer event occurs.

8 Typical programming and event driven programming Up to now your programs consisted of lists of statements executed in order. Up to now your programs consisted of lists of statements executed in order. But in event-driven programming, But in event-driven programming, 1. you create objects that can fire events, 2. and you create listener objects that react to the events. You don’t know the order (of events) ahead of time. You don’t know the order (of events) ahead of time. Typically, your code never directly calls the listener methods. Typically, your code never directly calls the listener methods.

9 Example: Buttons (JButton in Swing) Different kinds of components require different kinds of listener classes to handle the events they fire. Different kinds of components require different kinds of listener classes to handle the events they fire. A button fires events (known as action events), which are handled by listeners (know as action listeners). A button fires events (known as action events), which are handled by listeners (know as action listeners).

10 Our button listener import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class MyButtonListener implements ActionListener { public void actionPerformed ( ActionEvent e ) { System.exit( 0 ); }}

11 WINDOWS via Swing’s JFrame

12 Creating a window in Swing import javax.swing.JFrame; … JFrame f = new JFrame( "My Simple Frame" ); f.setSize( 300, 200 ); //w, h f.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); … f.setVisible( true ); Note the ‘x.’

13 Adding a button to a window (JFrame) import javax.swing.JButton; … //create the button JButton b = new JButton( "Click to end program" ); //associate the listener with this button MyButtonListener listener = new MyButtonListener(); b.addActionListener( listener ); f.add( b );//add the button to our frame, f

14 (Typical) Steps 1. Create the frame. 2. Create the button. 3. Create the action listener for the button. 4. Add the action listener to the button (register the action listener with the button). 5. Add the button to the frame. 6. Make the frame visible.

15 Back to creating a window in Swing import javax.swing.JFrame; … JFrame f = new JFrame( "My Simple Frame" ); f.setSize( 300, 200 ); //w, h f.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); //button setup here f.setVisible( true ); This is not a very OO approach!

16 A more OO approach to creating a window in Swing import javax.swing.JFrame; public class MyFrame extends JFrame { public static final intsWidth = 300; public static final intsHeight = 200; MyFrame ( ) { super( "My More OO Simple Frame" ); setSize( sWidth, sHeight ); setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); //button setup here setVisible( true ); }//…}

17 TIMER EVENTS

18 Handling Timer Events Timers and timer events are remarkably similar (to buttons and button events)! Timers and timer events are remarkably similar (to buttons and button events)! import javax.swing.Timer; import javax.swing.Timer; 1. Causes one ActionEvent to be delivered in the future after a specified delay. 2. Or causes an ActionEvent to be delivered repeatedly with the specified delay between the events. See http://download.oracle.com/javase/6/docs/api/java x/swing/Timer.html for more. See http://download.oracle.com/javase/6/docs/api/java x/swing/Timer.html for more. http://download.oracle.com/javase/6/docs/api/java x/swing/Timer.html http://download.oracle.com/javase/6/docs/api/java x/swing/Timer.html

19 Events Assignment Write a program that creates a window that contains a single button. Whenever the button is pressed, print out a message. Write a program that creates a window that contains a single button. Whenever the button is pressed, print out a message. The same program should also create a timer that fires repeatedly (every 5 seconds), and prints out a different message when this occurs. The same program should also create a timer that fires repeatedly (every 5 seconds), and prints out a different message when this occurs.


Download ppt "Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods."

Similar presentations


Ads by Google