15
C H A P T E R T E N C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Embed Size (px)

Citation preview

Page 1: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

C H A P T E R T E NC H A P T E R T E N

Event-Driven Programming

Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Page 2: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Background:

• Previous programming paradigms have emphasized 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 the process was complete (except for some Prolog programs we will not mention).

Page 3: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

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.

Page 4: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Event-driven Languages:

• Support of even-driven programming is often grafted on to existing languages, for example X11 is implemented as a set of function libraries under C and class libraries under C++.

• Some languages were designed around the needs of event-driven applications such as Visual Basic and Tcl/Tk.

• With the advent of the word wide web (WWW) event-driven programming has gained in popularity as a way to add interaction to web pages.

• Such interaction is programmed in a number of languages including JavaScript and Java.

• We will be using Java to implement Applets, programs intended to run inside a web browser.

Page 5: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Imperative vs. Event-Driven ParadigmsFigure 10.1

Computation is a function from its input to its output, made up of a sequence of steps that produce some result as its goal.

* *

Computation is a community of persistent entities coupled together by their ongoing interactive behavior. Beginning and end are special case that can often be ignored.

Page 6: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

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 processing loop.

• As a result of handling an event the program may produce output or modify the value of a state variable.

• There is no predefined starting or stopping point.

• Java provides support for event-driven programming through certain classes and methods that can be used to design program interaction.

Page 7: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Java Class AWTEvent and Its Subclasses*Figure 10.2

Events in Java are defined by subclasses of the AWTEvent abstract class.

Page 8: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Java Event Classes

• Every event source in a program can generate an event that is a member of one of the classes on the previous slide.

• For example, if a Button is an event source, it generates events that are members of the ActionEvent class.

• Objects that can be event sources are members of subclasses of the Component abstract class.

• This class and its subclasses are shown on the next slide, where we will find the Button object.

Page 9: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Subclasses of Component That Can Be Sources of EventsFigure 10.3

Page 10: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Java EventListener Class Interface:

• For a program to handle an event it must be equipped with a listener that will recognize when a particular event has occurred.

• For example, for a program to be able to “hear” that a button has been selected it must send an addActionListener message to the button object.

• The EventListener class and its subclasses provide the interface for recognizing events by setting up appropriate listeners for component objects.

• The EventListener class interface and its subclasses are shown on the next slide.

Page 11: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Java EventListener Class Interface and Its Subclasses*Figure 10.4

To respond to events we need to implement special methods called handlers.Each class of events predefines the names of the handlers that can be written for it…

Page 12: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Handlers Required for Button, Menu, Text Typing, and Mouse EventsFigure 10.5

Page 13: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Initial Frame Design for a Graphical Drawing ToolFigure 10.7

Page 14: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Overall Structure of a Java AppletFigure 10.6

Page 15: C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Next time…

example : Writing A

Java Applet