Event-driven programming. Most modern computer programs that people use have Graphical User...

36
Event-driven programming

Transcript of Event-driven programming. Most modern computer programs that people use have Graphical User...

Page 1: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Event-driven programming

Page 2: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Most modern computer programs that people use have Graphical User Interfaces (GUIs).

A GUI has icons on the computer screen and a mouse (or other device) to control a pointer that can be used to operate the computer.

Event-Driven Programming

Page 3: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Event-Driven Programming

An Introduction to Programming Using Alice

Most modern computer programs that people use have Graphical User Interfaces (GUIs).

A GUI has icons on the computer screen and a mouse (or other device) to control a pointer that can be used to operate the computer.

Page 4: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

This slide is an image of Microsoft Word 2007. It is an example of a modern graphical user interface.

Page 5: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Modern GUIs like the Word 2007 interface require the use of event-driven programming.

An event occurs whenever an event listener detects an event trigger and responds by running a method called an event handler.

Page 6: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

An event occurs whenever an event listener detects an event trigger and responds by running a method called an event handler.

Page 7: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

An event occurs whenever an event listener detects an event trigger and responds by running a method called an event handler.

Modern operating systems and programming languages contain facilities to let programmers set up event listeners.

Page 8: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

An event trigger can be almost any activity or condition selected by the programmer, such as a mouse movement, someone pressing the enter key, or a bank account balance changing.

Page 9: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

An event handler is a method that is activated when the event trigger occurs.

Almost any method can serve as an event handler, such as those that play sounds, initiate data communications, or perform calculations.

Page 10: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

In Alice…

• Events can be used to control objects moving in 3D space.– Detect when some object moves– Detect when two objects collide– Make objects move in response to mouse or

keyboard commands.

• So before discussing events in Alice, let’s talk about 3D space a little more.

Page 11: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

3D space

• A dimension is a way of measuring something.

• We create a dimension whenever we assign a value on a continuous scale to some property.

• Example: On a scale of 1 to 100, how much do you approve of George Bush’s performance as president?

Page 12: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Measuring distance

• If you wish to measure the location of a point on a straight line, then you only need one number.

• You could mark a start point on the line, and then measure how far a point is from the start point

• By using negative and positive numbers, you could also indicate which direction the distance spans.

Page 13: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Orientation

• In addition, to the concepts of distance and direction, we also have the concept of orientation.

• Orientation means the direction the object is facing.

• If an object is facing the positive direction on the line, its orientation is forward.

• If facing negative, then backward.

Page 14: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Point of view• Location and orientation together are

known as the point of view of an object.

Page 15: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Two ideas of direction• Absolute vs. object-relative.• Absolute direction of boy on bike I backward, but

forward in relation to Alice

Page 16: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Object-relative positions• From the coach’s point of view, Alice is behind

the boy, while the coach is in front of the boy.

Page 17: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Position in 2D

• On a flat surface, like a sheet of paper, you need two values to specify an object’s position (two dimensions).

• Rene Descarte developed a system of quantification for two dimensions.

• This is called Cartesian coordinates.• Cartesian coordinates have an X axis

(dimension) and Y axis. • The coordinates of a point are always specified

as x,y.

Page 18: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.
Page 19: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

A third dimension

• Moving up and down off a flat plane requires a third axis or dimension.

• Instead of an ordered pair to represent position, we need an ordered triplet (x,y,z).

• In Alice the z axis provides a measurement of depth.

• 3D space is sometimes called Euclidean 3-space after the Greek mathematician, Euclid.

Page 20: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.
Page 21: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Absolute vs. Object-relative

• For all objects we must be aware of absolute position and direction

• However, we are most commonly concerned with object-relative position and direction.

Page 22: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.
Page 23: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

More on object-relativity

• We must also frequently consider the movement of objects from the perspective of other objects.

• See SharkAsSeenByExample.a2w

Page 24: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in Alice

Page 25: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in AliceAlice events are created in the events area of the standard Alice interface.

events area

Page 26: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in AliceAlice events are created in the events area of the standard Alice interface.

events area

Page 27: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in Alice

An Alice event tile specifies the event trigger

and the event handler.

In this example, the event trigger is the spacebar, being

pressed and the event handler is the iceSkater’s simpleSpin

method.

Page 28: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in Alice

create new event button

The create new event button will reveal a menu with nine different event types.

Page 29: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in AliceThe create new event button will reveal a menu with nine different event types.

Page 30: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in AliceA programmer can pick the event type that has the desired event trigger.

Page 31: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in Alice

Some event types also have built-in event handlers…

A programmer can pick the event type that has the desired event trigger.

Page 32: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Events in Alice

Some event types also have built-in event handlers… In other cases, programmers must specify the handler.

A programmer can pick the event type that has the desired event trigger.

Page 33: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Event handlers are specified by dragging method tiles from the details area and dropping them into the event tile.

Page 34: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Exploring Events

• Start Alice and open the amusementPark example world.

• In the events area, you should see seven event tiles.

Page 35: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Amusement Park Events

Page 36: Event-driven programming. Most modern computer programs that people use have Graphical User Interfaces (GUIs). A GUI has icons on the computer screen.

Event-Driven Programming

• Events are an important part of Alice programming, allowing the creation of interactive worlds with keyboard and mouse user controls.

• Most modern programming languages allow programmers to add events to new software.

• Events can be used for many other purposes, such as controlling burglar alarms, checking remote sensors, or checking for system events, like printers running out of paper.

• Events are an important part of modern computer programming.