/
Events Chris Piech CS106A, Stanford Events Chris Piech CS106A, Stanford

Events Chris Piech CS106A, Stanford - PowerPoint Presentation

Thunderbolt
Thunderbolt . @Thunderbolt
Follow
342 views
Uploaded On 2022-08-03

Events Chris Piech CS106A, Stanford - PPT Presentation

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

method mouse listener run mouse method run listener moved called program events variables normal button methods user instance addmouselisteners

Share:

Link:

Embed:

Download Presentation from below link

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.


Presentation Transcript

Slide1

Events

Chris Piech

CS106A, Stanford

University

Slide2

Making Vegas 2.0

Slide3

Catch Me If You Can

Slide4

Source: The Hobbit

We’ve Gotten Ahead of Ourselves

Slide5

Source: The Hobbit

Start at the Beginning

Slide6

Learning Goals

Write a program that can respond to mouse events

Use an instance variable in your program

Slide7

When 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

Slide8

mouseClicked(

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

Slide9

Example

Slide10

Hole Puncher

Slide11

Now With Dancing Children

Slide12

Normal Program

Run Method

Slide13

Normal Program

Run Method

Slide14

Normal Program

Run Method

Slide15

Normal Program

Run Method

Slide16

Normal Program

Run Method

Slide17

Normal Program

Run Method

Slide18

Normal Program

Run Method

Slide19

New Listener Characters

Mouse Moved Method

Mouse Listener

Slide20

Program with a Mouse Method

Run Method

Mouse Moved Method

Slide21

Program Starts Running

Run Method

Mouse Moved Method

Slide22

Add Mouse Listener

Run Method

Mouse Listener

Mouse Moved Method

addMouseListeners

();

Slide23

Program Runs as Usual

Run Method

Mouse Listener

Mouse Moved Method

Slide24

Mouse Moved!

Run Method

Mouse Listener

Mouse Moved Method

Slide25

Calls Mouse Moved Method

Run Method

Mouse Listener

Mouse Moved Method

Slide26

Run Method

Mouse Listener

Mouse Moved Method

When done, Run continues.

Slide27

Run Method

Mouse Listener

Mouse Moved Method

Keeps Doing Its Thing

Slide28

Mouse Moved!

Run Method

Mouse Listener

Mouse Moved Method

Slide29

Calls Mouse Moved Method

Run Method

Mouse Listener

Mouse Moved Method

Slide30

When done, Run continues.

Run Method

Mouse Listener

Mouse Moved Method

Slide31

Mouse Tracker

Slide32

Mouse Tracker

Slide33

Variables 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.

Slide34

Often you need instance variables to pass information between the run method and the mouse event methods!

Instance Variables + Events

Slide35

Objects have a special value called

null

which means this variable is not associated with a value yet.

Null

Slide36

Debris Sweeper

Slide37

New Commands

addMouseListeners();getElementAt(x

,

y);remove(obj);New IdeasThe Listener ModelInstance Variablesnull

New Concepts

Slide38

mouseClicked(

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

Slide39

keyPressed(

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

Slide40

And Here We Are

Slide41

Catch Me If You Can?