University Making Vegas 20 Catch Me If You Can Source The Hobbit Weve Gotten Ahead of Ourselves Source The Hobbit Start at the Beginning Learning Goals Write a program that can respond to mouse events ID: 934179
Download Presentation The PPT/PDF document "Events Chris Piech CS106A, Stanford" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Events
Chris Piech
CS106A, Stanford
University
Slide2Making Vegas 2.0
Slide3Catch Me If You Can
Slide4Source: The Hobbit
We’ve Gotten Ahead of Ourselves
Slide5Source: The Hobbit
Start at the Beginning
Slide6Learning Goals
Write a program that can respond to mouse events
Use an instance variable in your program
Slide7When users interact with computer they generate events (e.g., moving/clicking the
mouse)
Can
respond to events by having listener for events addMouseListeners()
Listeners get control of the program when an event happens.
Using portions of slides by Eric Roberts
Listener Model
Slide8mouseClicked(
e)
mousePressed(
e)mouseReleased(e)
mouseMoved(
e
)
mouseDragged(
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
MouseEvent
object, which provides more data about event, such as the location of mouse.
1
.
The
run
method should call
addMouseListeners
2. Write definitions of any listener methods needed
Using portions of slides by Eric Roberts
Responding to Mouse Events
Slide9Example
Slide10Hole Puncher
Slide11Now With Dancing Children
Slide12Normal Program
Run Method
Slide13Normal Program
Run Method
Slide14Normal Program
Run Method
Slide15Normal Program
Run Method
Slide16Normal Program
Run Method
Slide17Normal Program
Run Method
Slide18Normal Program
Run Method
Slide19New Listener Characters
Mouse Moved Method
Mouse Listener
Slide20Program with a Mouse Method
Run Method
Mouse Moved Method
Slide21Program Starts Running
Run Method
Mouse Moved Method
Slide22Add Mouse Listener
Run Method
Mouse Listener
Mouse Moved Method
addMouseListeners
();
Slide23Program Runs as Usual
Run Method
Mouse Listener
Mouse Moved Method
Slide24Mouse Moved!
Run Method
Mouse Listener
Mouse Moved Method
Slide25Calls Mouse Moved Method
Run Method
Mouse Listener
Mouse Moved Method
Slide26Run Method
Mouse Listener
Mouse Moved Method
When done, Run continues.
Slide27Run Method
Mouse Listener
Mouse Moved Method
Keeps Doing Its Thing
…
Slide28Mouse Moved!
Run Method
Mouse Listener
Mouse Moved Method
Slide29Calls Mouse Moved Method
Run Method
Mouse Listener
Mouse Moved Method
Slide30When done, Run continues.
Run Method
Mouse Listener
Mouse Moved Method
Slide31Mouse Tracker
Slide32Mouse Tracker
Slide33Variables exist until their inner-most control block ends.
If a variable is defined outside all methods, its inner-most control block is the entire program!
We call these variables
instance 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.
Slide34Often you need instance variables to pass information between the run method and the mouse event methods!
Instance Variables + Events
Slide35Objects have a special value called
null
which means this variable is not associated with a value yet.
Null
Slide36Debris Sweeper
Slide37New Commands
addMouseListeners();getElementAt(x
,
y);remove(obj);New IdeasThe Listener ModelInstance Variablesnull
New Concepts
Slide38mouseClicked(
e)
mousePressed(
e)mouseReleased(e)
mouseMoved(
e
)
mouseDragged(
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
MouseEvent
object, which provides more data about event, such as the location of mouse.
1
.
The
run
method should call
addMouseListeners
2. Write definitions of any listener methods needed
Using portions of slides by Eric Roberts
Responding to Mouse Events
Slide39keyPressed(
e)
keyReleased(
e)keyTyped(e)
Called when the user presses a key
Called when the key comes back up
Called when the user types
(presses and releases) a key
The parameter
e
is a
KeyEvent
object, which indicates which key is involved.
1
. T
he
run
method should call
addKeyListeners
2. Write definitions of any listener methods needed
Using portions of slides by Eric Roberts
Responding to Keyboard Events
Slide40And Here We Are
…
Slide41Catch Me If You Can?