Upload
reshma-walunj
View
222
Download
0
Embed Size (px)
Citation preview
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 1/15
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
C H A P T E R T E N
Event-DrivenProgramming
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 2/15
10-2
Background:
• Previous programming paradigms haveemphasized the process for solving a problem.
• Once the program was complete we would:
provide input
run the program display the answer(s)
• Things always happened in the same sequence.
• The program always terminated when theprocess was complete (except for some Prologprograms we will not mention).
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 3/15
10-3
Background:
• Event-driven programs do not have a set
sequence of instructions to execute.• They also do not have a predetermined finish.
• The most common example of event-driven
programming is found in graphical user interfaces (GUIs):
M$ Windows
Apples Mac OS
X11 under *nix
• Other applications include embedded systems,control systems, sensor systems like home
security, etc.
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 4/15
10-4
Event-driven Languages:
• Support of even-driven programming is often grafted on
to existing languages, for example X11 is implemented asa set of function libraries under C and class libraries underC++.
• Some languages were designed around the needs of
event-driven applications such as Visual Basic andTcl/Tk .
• With the advent of the word wide web (WWW) event-driven programming has gained in popularity as a way toadd interaction to web pages.
• Such interaction is programmed in a number oflanguages including JavaScript and Java .
• We will be using Java to implement Applets , programs
intended to run inside a web browser .
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 5/15
10-5
Imperative vs. Event-Driven ParadigmsFigure 10.1
Computation is a function from its input to its output, made up of asequence of steps that produce some result as its goal.
* *
Computation is a community of persistent entities coupled togetherby their ongoing interactive behavior. Beginning and end are
special case that can often be ignored.
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 6/15
10-6
Event-driven Programs
• Input to event-driven programs come from event
sources ; sensors, input devices, objects on a web page.• Events occur asynchronously and are placed in an event queue as they arise.
• Events are removed from the event queue and
processed (“handled ”) by the program’s main processingloop.
• As a result of handling an event the program mayproduce output or modify the value of a state variable .
• There is no predefined starting or stopping point.• Java provides support for event-driven programmingthrough certain classes and methods that can be used todesign program interaction.
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 7/1510-7
Java Class AWTEvent and Its Subclasses*Figure 10.2
Events in Java are defined by subclasses of theAWTEvent abstract class.
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 8/1510-8
Java Event Classes
• Every event source in a program can generate
an event that is a member of one of the classeson the previous slide.
• For example, if a Button is an event source, itgenerates events that are members of theActionEvent class.
• Objects that can be event sources are membersof subclasses of the Component abstract class.
• This class and its subclasses are shown on thenext slide, where we will find the Button object.
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 9/1510-9
Subclasses of Component That Can Be Sources of EventsFigure 10.3
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 10/1510-10
Java EventListener Class Interface:
• For a program to handle an event it must be
equipped with a listener that will recognize whena particular event has occurred.
• For example, for a program to be able to “hear”
that a button has been selected it must send anaddActionListener message to the button object.
• The EventListener class and its subclassesprovide the interface for recognizing events by
setting up appropriate listeners for componentobjects.
• The EventListener class interface and itssubclasses are shown on the next slide.
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 11/1510-11
Java EventListener Class Interface and Its Subclasses*Figure 10.4
To respond to eventswe need to implementspecial methods calledhandlers .
Each class of eventspredefines the namesof the handlers that canbe written for it…
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 12/1510-12
Handlers Required for Button, Menu,Text Typing, and Mouse EventsFigure 10.5
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 13/1510-13
Initial Frame Design for a Graphical Drawing ToolFigure 10.7
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 14/1510-14
Overall Structure of a Java AppletFigure 10.6
7/31/2019 csci360-26
http://slidepdf.com/reader/full/csci360-26 15/1510-15
Next time…
Writing AJava Applet