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

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

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Web Design & Development Lecture 19. Java Graphics 2.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
Io package as Java’s basic I/O system continue’d.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Welcome to CIS 083 ! Events CIS 068.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
(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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Graphical User Interface Programming. GUI Graphical User Interface Graphical User Interface event driven programming event driven programming Java GUI.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Graphical User Interface Programming. GUI Graphical User Interface Graphical User Interface event driven programming event driven programming Java GUI.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
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.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
GUI building with the AWT
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Graphical User Interface Programming
Ellen Walker Hiram College
Event-driven programming for GUI
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Introduction to Event Handling
Graphical User Interfaces in Java Event-driven programming
Advanced Programming in Java
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Event Driven (Asynchronous) Programming

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.

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.

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

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

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

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.

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.

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

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 ); }}

WINDOWS via Swing’s JFrame

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

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

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

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!

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 ); }//…}

TIMER EVENTS

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 x/swing/Timer.html for more. See x/swing/Timer.html for more. x/swing/Timer.html x/swing/Timer.html

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.