Events Chris Piech CS 106 A Stanford University

  • Slides: 41
Download presentation
Events Chris Piech CS 106 A, Stanford University Piech, CS 106 A, Stanford University

Events Chris Piech CS 106 A, Stanford University Piech, CS 106 A, Stanford University

Making Vegas 2. 0 Piech, CS 106 A, Stanford University

Making Vegas 2. 0 Piech, CS 106 A, Stanford University

Catch Me If You Can Piech, CS 106 A, Stanford University

Catch Me If You Can Piech, CS 106 A, Stanford University

We’ve Gotten Ahead of Ourselves Piech, CS 106 A, Stanford University Source: The Hobbit

We’ve Gotten Ahead of Ourselves Piech, CS 106 A, Stanford University Source: The Hobbit

Start at the Beginning Piech, CS 106 A, Stanford University Source: The Hobbit

Start at the Beginning Piech, CS 106 A, Stanford University Source: The Hobbit

Learning Goals 1. Write a program that can respond to mouse events 2. Use

Learning Goals 1. Write a program that can respond to mouse events 2. Use an instance variable in your program Piech, CS 106 A, Stanford University

Listener Model • When users interact with computer they generate events (e. g. ,

Listener Model • When users interact with computer they generate events (e. g. , moving/clicking the mouse) • Can respond to events by having listener for events add. Mouse. Listeners() • Listeners get control of the program when an event happens. Piech, CS 106 A, Stanford University Using portions of slides by Eric Roberts

Responding to Mouse Events 1. The run method should call add. Mouse. Listeners 2.

Responding to Mouse Events 1. The run method should call add. Mouse. Listeners 2. Write definitions of any listener methods needed mouse. Clicked( e) mouse. Pressed( e) mouse. Released(e) mouse. Moved(e) mouse. Dragged(e) Called when the user clicks the mouse Called when the mouse button is pressed Called when the mouse button is released Called when the user moves the mouse Called when the mouse is dragged with the button down The parameter e is Mouse. Event object, which provides more data about event, such as the location of mouse. Piech, CS 106 A, Stanford University Using portions of slides by Eric Roberts

Example Piech, CS 106 A, Stanford University

Example Piech, CS 106 A, Stanford University

Hole Puncher Piech, CS 106 A, Stanford University

Hole Puncher Piech, CS 106 A, Stanford University

Now With Dancing Children Piech, CS 106 A, Stanford University

Now With Dancing Children Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

Normal Program Run Method Piech, CS 106 A, Stanford University

New Listener Characters Mouse Listener Mouse Moved Method Piech, CS 106 A, Stanford University

New Listener Characters Mouse Listener Mouse Moved Method Piech, CS 106 A, Stanford University

Program with a Mouse Method Run Method Mouse Moved Method Piech, CS 106 A,

Program with a Mouse Method Run Method Mouse Moved Method Piech, CS 106 A, Stanford University

Program Starts Running Run Method Mouse Moved Method Piech, CS 106 A, Stanford University

Program Starts Running Run Method Mouse Moved Method Piech, CS 106 A, Stanford University

Add Mouse Listener Run Method Mouse Moved Method add. Mouse. Listeners(); Piech, CS 106

Add Mouse Listener Run Method Mouse Moved Method add. Mouse. Listeners(); Piech, CS 106 A, Stanford University Mouse Listener

Program Runs as Usual Run Method Mouse Moved Method Piech, CS 106 A, Stanford

Program Runs as Usual Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

Mouse Moved! Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse

Mouse Moved! Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

Calls Mouse Moved Method Run Method Mouse Moved Method Piech, CS 106 A, Stanford

Calls Mouse Moved Method Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

When done, Run continues. Run Method Mouse Moved Method Piech, CS 106 A, Stanford

When done, Run continues. Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

Keeps Doing Its Thing… Run Method Mouse Moved Method Piech, CS 106 A, Stanford

Keeps Doing Its Thing… Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

Mouse Moved! Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse

Mouse Moved! Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

Calls Mouse Moved Method Run Method Mouse Moved Method Piech, CS 106 A, Stanford

Calls Mouse Moved Method Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

When done, Run continues. Run Method Mouse Moved Method Piech, CS 106 A, Stanford

When done, Run continues. Run Method Mouse Moved Method Piech, CS 106 A, Stanford University Mouse Listener

Mouse Tracker Piech, CS 106 A, Stanford University

Mouse Tracker Piech, CS 106 A, Stanford University

Mouse Tracker Piech, CS 106 A, Stanford University

Mouse Tracker Piech, CS 106 A, Stanford University

Instance Variables 1. Variables exist until their inner-most control block ends. 2. If a

Instance Variables 1. Variables exist until their inner-most control block ends. 2. If a variable is defined outside all methods, its inner-most control block is the entire program! 3. We call these variables instance variables * Instance variables have special meanings in programs with multiple files. For now you need to know that all methods can see them and that their initialization line is executed before run. Piech, CS 106 A, Stanford University

Instance Variables + Events Often you need instance variables to pass information between the

Instance Variables + Events Often you need instance variables to pass information between the run method and the mouse event methods! Piech, CS 106 A, Stanford University

Null Objects have a special value called null which means this variable is not

Null Objects have a special value called null which means this variable is not associated with a value yet. Piech, CS 106 A, Stanford University

Debris Sweeper Piech, CS 106 A, Stanford University

Debris Sweeper Piech, CS 106 A, Stanford University

New Concepts New Commands • add. Mouse. Listeners(); • get. Element. At(x, y); •

New Concepts New Commands • add. Mouse. Listeners(); • get. Element. At(x, y); • remove(obj); New Ideas • The Listener Model • Instance Variables • null Piech, CS 106 A, Stanford University

Responding to Mouse Events 1. The run method should call add. Mouse. Listeners 2.

Responding to Mouse Events 1. The run method should call add. Mouse. Listeners 2. Write definitions of any listener methods needed mouse. Clicked( e) mouse. Pressed( e) mouse. Released(e) mouse. Moved(e) mouse. Dragged(e) Called when the user clicks the mouse Called when the mouse button is pressed Called when the mouse button is released Called when the user moves the mouse Called when the mouse is dragged with the button down The parameter e is Mouse. Event object, which provides more data about event, such as the location of mouse. Piech, CS 106 A, Stanford University Using portions of slides by Eric Roberts

Responding to Keyboard Events 1. The run method should call add. Key. Listeners 2.

Responding to Keyboard Events 1. The run method should call add. Key. Listeners 2. Write definitions of any listener methods needed key. Pressed( e) key. Released( e) Called when the user presses a key Called when the key comes back up key. Typed(e) Called when the user types (presses and releases) a key The parameter e is a Key. Event object, which indicates which key is involved. Piech, CS 106 A, Stanford University Using portions of slides by Eric Roberts

And Here We Are… Piech, CS 106 A, Stanford University

And Here We Are… Piech, CS 106 A, Stanford University

Catch Me If You Can? Piech, CS 106 A, Stanford University

Catch Me If You Can? Piech, CS 106 A, Stanford University