cis 068 welcome to cis 083 ! events. cis 068 overview subjects: structured programming vs. event...
TRANSCRIPT
![Page 1: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/1.jpg)
CIS 068
Welcome to CIS 083 !
Events
![Page 2: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/2.jpg)
CIS 068
Overview
Subjects:
• Structured Programming vs. Event Driven Programming(EDP)
• Events
• Events in JAVA
• GUIs as Example for EDP
![Page 3: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/3.jpg)
CIS 068
An ExampleThe task: write a program,showing
• a menu
• 2 circles
• 2 buttons
Menu
ON / OFF ON / OFF
Reset
Exit
![Page 4: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/4.jpg)
CIS 068
An Example
Menu
ON / OFF ON / OFF
Reset
Exit
Show both circles
Exit Program
The circles
Buttons toggle circles
![Page 5: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/5.jpg)
CIS 068
An Example
Menu
ON / OFF ON / OFF
Reset
Exit
Confirm command
by dialog
Do you really want to do that ?
YES NO
![Page 6: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/6.jpg)
CIS 068
An Example
Required:
All commands must be executable at every time
![Page 7: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/7.jpg)
CIS 068
Sequential Approach
A first approach could be:
Check Menu
Check Button 1
Check Button 2
Show Window
Check Button Y
Check Button N
Exit or Loop
![Page 8: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/8.jpg)
CIS 068
Sequential Approach
Of course this approach doesn’t fulfill the requirements !
(If button 1 is activated, button 2 and the menu are disabled)
![Page 9: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/9.jpg)
CIS 068
Sequential Approach 2Check Menu
Check Button 1
Check Button 2
Show Dialog A
If dialogA
Check Button Y / N
If dialogB
Check Button Y / N
Set dialogA = true
Show Dialog B
Set dialogB = true
Loop
…
…
![Page 10: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/10.jpg)
CIS 068
Sequential Approach 2
Does this approach meet the requirements ?
What does the program, i.e. the processor, do most of the time ?
What about more complicated structures (nested command structures,…) ?
![Page 11: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/11.jpg)
CIS 068
Event Driven ApproachWhat about this approach:
Process Menu
Process Button 1
Process Button 2
Process Button Y
Process Button N
Menu activated
Button 1 activatedButton 2 activated
Button Y activated
Button N activated
![Page 12: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/12.jpg)
CIS 068
Example: Discussion
And again:
•Does this approach meet the requirements ?
•What does the program, i.e. the processor, do most of the time ?
•What about more complicated structures (nested command structures,…) ?
•Who is in control of the program’s flow ?
![Page 13: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/13.jpg)
CIS 068
Sequential vs. Event-driven Programming
Comparison between Sequential
And
Event Driven Programming
![Page 14: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/14.jpg)
CIS 068
Sequential Programming
• In sequential programs, the program is under control
• The user is required to synchronize with the program:
•Program tells user it’s ready for more input
•User enters more input and it is processed
• While … if then… structures highly define the user’s path through the program
• Program is predefined with respect to time
• Handling of sequentially independent commands requires enormous amount of work
![Page 15: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/15.jpg)
CIS 068
Sequential Programming
Shouldn’t the program be required to synchronize with the user?
• Flow of a typical sequential program• Prompt the user• Read input from the keyboard• Parse the input (determine user action)• Evaluate the result• Generate output• Repeat
![Page 16: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/16.jpg)
CIS 068
Sequential Programming
• Advantages– Architecture is iterative (one step at a time)– Easy to model (flowcharts, state machines)– Easy to build
• Limitations– Can’t implement complex interactions– Only a small number of features possible– Interaction must proceed according to a pre-defined
sequence
• To the rescue… Event-driven programming
![Page 17: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/17.jpg)
CIS 068
Event Driven Programming
• Instead of a user synchronizing with the program, the program synchronizes with, or reacts to, the user
• All communication from user to computer occurs via EVENTS and the code that handles the events
• An event is an action that happens in the system– A mouse button pressed or released– A keyboard key is hit– A window is moved, resized, closed, etc.
![Page 18: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/18.jpg)
CIS 068
Event Driven Programming
• Typically two different classes of events– User-initiated events
• Events that result directly from a user action• e.g., mouse click, move mouse, button press
– System-initiated events• Events created by the system, as it
responds to a user action• e.g., scrolling text, re-drawing a window
![Page 19: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/19.jpg)
CIS 068
Event Driven Programming
• There’s no top-down flow of control, i.e. no ‘Main’-program defining the sequential flow
• Code fragments are associated with events and invoked when events occur
• Order of execution is decoupled
• Don’t have to deal with order of events– This is especially helpful, when the order is unknown !
![Page 20: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/20.jpg)
CIS 068
Typical Applications
• GUIs– Modern GUIs are event driven. Event occurs when the user does
something:• Move the mouse
• Press a button
• Activate a menu
• Resize Window
• …
![Page 21: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/21.jpg)
CIS 068
Typical Applications
Other Examples:• Event driven I/O
– Especially networking, where I/O is very slow– Events occur, when
• Connection is made
• When ready to send
• When data arrives
• LEGO Mindstorms– Events occur, when
• Sensors report changes in environment
![Page 22: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/22.jpg)
CIS 068
Event Driven Programming
How does it work
or
where’s the ‘MAIN’ ?
![Page 23: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/23.jpg)
CIS 068
Participants in an OO - Event Driven SystemEvent Source
• objects that generate events, e.g. buttons
Event
• represent occurences, changes of state or requests which a program might need to handle
• store all event-specific information (e.g. mouseEvent: x/y coordinates)
Handler (Listener)
• objects that respond to events
• Provide the code needed to handle the event
![Page 24: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/24.jpg)
CIS 068
Participants in an OO - Event Driven System
Event
Source
(e.g. mouse)
ListenerEvent Public void mouseClicked(
MouseEvent e){
…
}
![Page 25: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/25.jpg)
CIS 068
•The listener must be known to the event source, such that the event can be sent
Listener is REGISTERED
• Listeners specify what kind of events they are interested in, i.e. specify the event sources
• Typically done by addXXXListener(Listener)
Registering Listeners
![Page 26: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/26.jpg)
CIS 068
Registering Listeners
Source
(eg. Button)
Listener 1
Listener 2
Listener 3
Listener 4REGISTER (‘please inform me’)
![Page 27: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/27.jpg)
CIS 068
Registering Listeners: JAVA example
here !
![Page 28: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/28.jpg)
CIS 068
Where’s the ‘MAIN’ ?
• The Operating System (or JAVA) manages an event-queue
• The event-queue contains information about event-sources and their registered listeners
• As events occur, they are placed in the queue to be dispatched by the event - loop
• The OS (or JAVA) loops through the event-queue, passing the command to the specified listeners
• The MAIN – control is passed to the OS (or JAVA), the program is in idle state until activated by events
• The MAIN is replaced by the event-loop
![Page 29: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/29.jpg)
CIS 068
Where’s the ‘MAIN’ ?
The advantage:
• The OS (or JAVA) can optimize the processing time for managing the loop very efficiently
• The OS provides the management, the programmer only writes (and registers) listeners
![Page 30: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/30.jpg)
CIS 068
Events in JAVA: Sources
Event Sources:
• Mouse
• Keyboard
• GUI – Objects (AWT / Swing)
• Button
• ChoiceBox etc.
![Page 31: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/31.jpg)
CIS 068
Events in JAVA: Events
EventObject
AWTEvent
ActionEvent ComponentEvent
InputEvent WindowEvent
MouseEvent KeyEvent
Note: this diagram is not complete. It just shows the most common event classes
![Page 32: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/32.jpg)
CIS 068
Events in JAVA: Listeners
Classes implementing the ‘Listener’ – interfaces (package java.awt.event)
![Page 33: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/33.jpg)
CIS 068
Events in JAVA: the example revisited
Creates the event source
ButtonDemo is able to
receive ActionEvents
Adds button to frame but does
NOT register ButtonDemo !
Registers THIS instance of
ButtonDemo to theButton
If theButton is pressed, an
ActionEvent is sent to the
Method actionPerformed(..),
i.e. the method is invoked with
the event as argument.
![Page 34: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/34.jpg)
CIS 068
JAVA-Events: GUI – Example
Implementation of the first example (without Menu and Dialog)
![Page 35: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/35.jpg)
CIS 068
JAVA-Events: GUI – Example
JFrame
![Page 36: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/36.jpg)
CIS 068
JAVA-Events: GUI – Example
JFrame layout: GridLayout(1,2)
Provides 1 x 2 Grid. Each area of grid can contain a new
GUI – component.
AREA 1 AREA 2
![Page 37: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/37.jpg)
CIS 068
JAVA-Events: GUI – Example
•Each area contains a JPanel.
• JPanel – layout: BorderLayout()
• Used are only the CENTER and SOUTH areas.
CENTER
SOUTH
![Page 38: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/38.jpg)
CIS 068
JAVA-Events: GUI – Example
•The CENTER contains a JComponent
• JComponent can be used for custom – drawing using the paint() method
•The SOUTH area contains a JButton
![Page 39: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/39.jpg)
CIS 068
JAVA-Events: GUI – Example
The Main - Window: JFrame
Layout
Add JPanels
![Page 40: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/40.jpg)
CIS 068
JAVA-Events: GUI – Example
The JPanel, containing the circle and the button:
Layout
Create JComponent
And Button
Register JComponent
to Button !
Add JComponent
and Button
![Page 41: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/41.jpg)
CIS 068
GUI – ExampleThe JComponent,
responsible for
drawing the circle:
Constructor
This method is invoked
by JButton !
Paint green background
and, if required, red
circle
![Page 42: CIS 068 Welcome to CIS 083 ! Events. CIS 068 Overview Subjects: Structured Programming vs. Event Driven Programming(EDP) Events Events in JAVA GUIs as](https://reader036.vdocument.in/reader036/viewer/2022062304/56649e665503460f94b60efe/html5/thumbnails/42.jpg)
CIS 068
Outlook
Next time:
GUI – Elements !