Events Where are we? Stamp Tool Catch Me If You Can Weve Gotten - - PowerPoint PPT Presentation

events where are we stamp tool catch me if you can we ve
SMART_READER_LITE
LIVE PREVIEW

Events Where are we? Stamp Tool Catch Me If You Can Weve Gotten - - PowerPoint PPT Presentation

Events Where are we? Stamp Tool Catch Me If You Can Weve Gotten Ahead of Ourselves Source: The Hobbit Start at the Beginning Source: The Hobbit Learning Goals 1. Write a program that can respond to mouse events 2. Use an instance


slide-1
SLIDE 1

Events

slide-2
SLIDE 2

Where are we?

slide-3
SLIDE 3

Stamp Tool

slide-4
SLIDE 4

Catch Me If You Can

slide-5
SLIDE 5

Source: The Hobbit

We’ve Gotten Ahead of Ourselves

slide-6
SLIDE 6

Source: The Hobbit

Start at the Beginning

slide-7
SLIDE 7

Learning Goals

1. Write a program that can respond to mouse events

  • 2. Use an instance variable in your program
slide-8
SLIDE 8
  • 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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Hole Puncher

slide-11
SLIDE 11

Now With Dancing Children

slide-12
SLIDE 12

Normal Program

Run Method

slide-13
SLIDE 13

New Listener Characters

Mouse Clicked Method Mouse Listener

slide-14
SLIDE 14

Program Starts Running

Run Method Mouse Clicked Method

slide-15
SLIDE 15

Add Mouse Listener

Run Method Mouse Listener Mouse Clicked Method addMouseListeners();

slide-16
SLIDE 16

Program Runs as Usual

Run Method Mouse Listener Mouse Clicked Method

slide-17
SLIDE 17

Mouse Clicked!

Run Method Mouse Listener Mouse Clicked Method

slide-18
SLIDE 18

Calls Mouse Clicked Method

Run Method Mouse Listener Mouse Clicked Method

slide-19
SLIDE 19

Run Method Mouse Listener Mouse Clicked Method

When done, Run continues.

slide-20
SLIDE 20

Run Method Mouse Listener Mouse Clicked Method

Keeps Doing Its Thing…

slide-21
SLIDE 21

Mouse Moved!

Run Method Mouse Listener Mouse Clicked Method

slide-22
SLIDE 22

Calls Mouse Clicked Method

Run Method Mouse Listener Mouse Clicked Method

slide-23
SLIDE 23

When done, Run continues.

Run Method Mouse Listener Mouse Clicked Method

slide-24
SLIDE 24

Mouse Tracker

slide-25
SLIDE 25
  • 1. Variables exist until their inner-most code block ends.
  • 2. If a variable is defined outside all methods, its inner-most

code block is the entire program!

  • 3. 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.

slide-26
SLIDE 26

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

Instance Variables + Events

slide-27
SLIDE 27

Objects have a special value called null which means this variable is not associated with a value yet.

Null

slide-28
SLIDE 28

Debris Sweeper

slide-29
SLIDE 29

getElementAt(x, y);

  • getElementAt(x, y) will return any

GObject at the (x, y) coordinates. It will return null if there is no object at those coordinates.

slide-30
SLIDE 30

And Here We Are…

slide-31
SLIDE 31

Stamp Tool

slide-32
SLIDE 32

New Commands

  • addMouseListeners();
  • getElementAt(x, y);

New Ideas

  • The Listener Model
  • Instance Variables
  • null

New Concepts

slide-33
SLIDE 33

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

slide-34
SLIDE 34

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. The run method should call addKeyListeners
  • 2. Write definitions of any listener methods needed

Using portions of slides by Eric Roberts

Responding to Keyboard Events

slide-35
SLIDE 35

Warm Up: Making Tracks

slide-36
SLIDE 36

Catch Me If You Can?