80
Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech, Brahm Capoor and others.

Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Mouse Events and Instance Variables

CS106A, Summer 2019Sarai Gould && Laura Cruz-Albrecht

Lecture 10

With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech, Brahm Capoor and others.

Page 2: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Announcements

● HW2 was due at 10AM today● HW 3 goes out today after lecture

○ Due Thursday July 18 at 10AM○ Can optionally be done in pairs; check out the Pair Programming

link on website

2

Page 3: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

3

HW 3:

Page 4: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

4

Page 5: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

5

Page 6: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Review: Animation Loop

6

public void run() {

// setup

Make variables. Add graphics to canvas.

while (condition) {

// update world

Update graphics.

// pause

pause(milliseconds);

}

}

Page 7: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Review: Animation Loop

7

public void run() {

// setup

GRect square = makeSquare();

while (true) {

// update world

square.move(1, 0);

// pause

pause(PAUSE_TIME);

}

}

Page 8: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Review: RandomGenerator

8

// this variable can generate random values

RandomGenerator rgen = RandomGenerator.getInstance();

// make a random number between 1 and 6 inclusive

int diceRoll = rgen.nextInt(1, 6);

// also: nextDouble, nextBoolean, nextColor, etc

Page 9: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

9

Page 10: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

getElementAt● The method:

GObject getElementAt(double x, double y);

returns which object is at the given location on the canvas.

10

Page 11: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

getElementAt● The method:

GObject getElementAt(double x, double y);

returns which object is at the given location on the canvas.

● The return type is GObject, since we don't know what specific type (GRect, GOval, etc.) is really there.

11

Page 12: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

getElementAt● The method:

GObject getElementAt(double x, double y);

returns which object is at the given location on the canvas.

● The return type is GObject, since we don't know what specific type (GRect, GOval, etc.) is really there.

● If no object is present, the special value null is returned.

12

Page 13: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Sand Art Revisited

/*

* Given a grain of sand, returns whether that sand has

* collided with any other objects on screen.

*/

private boolean hasHitSomethingElse(GOval sand) {

double checkX = sand.getX() + sand.getWidth() / 2.0;

double checkY = sand.getY() + sand.getHeight();

GObject collidingObject = getElementAt(checkX, checkY);

return collidingObject != null;

}

13

sand

Has sand hit something else?

Page 14: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

getElementAt

/*

* Given a grain of sand, returns whether that sand has

* collided with any other objects on screen.

*/

private boolean hasHitSomethingElse(GOval sand) {

double checkX = sand.getX() + sand.getWidth() / 2.0;

double checkY = sand.getY() + sand.getHeight();

GObject collidingObject = getElementAt(checkX, checkY);

return collidingObject != null;

}

14

sand

(checkX, checkY)

Has sand hit something else?

Page 15: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

getElementAt

/*

* Given a grain of sand, returns whether that sand has

* collided with any other objects on screen.

*/

private boolean hasHitSomethingElse(GOval sand) {

double checkX = sand.getX() + sand.getWidth() / 2.0;

double checkY = sand.getY() + sand.getHeight();

GObject collidingObject = getElementAt(checkX, checkY);

return collidingObject != null;

}

15

sand

(checkX, checkY)

Has sand hit something else?

Page 16: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Nullnull is a special variable value that objects can have that means “nothing”. Primitives cannot be null.

16

Page 17: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Nullnull is a special variable value that objects can have that means “nothing”. Primitives cannot be null.

If a method returns an object, it can return null to signify “nothing”. (just say return null;)

17

Page 18: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Nullnull is a special variable value that objects can have that means “nothing”. Primitives cannot be null.

If a method returns an object, it can return null to signify “nothing”. (just say return null;)

// may be a GObject, or null if nothing at (x, y)

GObject maybeAnObject = getElementAt(x, y);

18

Page 19: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Nullnull is a special variable value that objects can have that means “nothing”. Primitives cannot be null.

If a method returns an object, it can return null to signify “nothing”. (just say return null;)

// may be a GObject, or null if nothing at (x, y)

GObject maybeAnObject = getElementAt(x, y);

Objects have the value null before being initialized.

GOval circle; // initially null

19

Page 20: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Null

You can check if something is null using == and !=

20

// may be a GObject, or null if nothing at (x, y)

GObject maybeAnObject = getElementAt(x, y);

if (maybeAnObject != null) {

// do something with maybeAnObject

} else {

// null – nothing at that location

}

Page 21: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Null

Calling methods on an object that is null will crash your program!

21

// may be a GObject, or null if nothing at (x, y)

GObject maybeAnObject = getElementAt(x, y);

if (maybeAnObject != null) {

int x = maybeAnObject.getX(); // OK

} else {

int x = maybeAnObject.getX(); // CRASH!

}

Page 22: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Null

Calling methods on an object that is null will crash your program!⇒ Throws a NullPointerException

22

Page 23: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

23

Page 24: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Events

● An event is some external stimulus that your program can respond to.

● event-driven programming: A programming paradigm (common in graphical programs) where your code is executed in response to user events.

24

Page 25: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Events

● Common events include: ○ Mouse motion / clicking.○ Keyboard buttons pressed.○ Timers expiring.○ Network data available.

25

Page 26: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Events

● Common events include: ○ Mouse motion / clicking.○ Keyboard buttons pressed.○ Timers expiring.○ Network data available.

26

Page 27: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Eventspublic void run() {

// Java runs this when program launches

}

27

Page 28: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Eventspublic void run() {

// Java runs this when program launches

}

28

To respond to events, your program must write methods to

handle those events.

Page 29: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Eventspublic void run() {

// Java runs this when program launches

}

public void mouseClicked(MouseEvent event) {

// Java runs this when mouse is clicked

}

29

To respond to events, your program must write methods to

handle those events.

Page 30: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Eventspublic void run() {

// Java runs this when program launches

}

public void mouseClicked(MouseEvent event) {

// Java runs this when mouse is clicked

}

public void mouseMoved(MouseEvent event) {

// Java runs this when mouse is moved

}30

To respond to events, your program must write methods to

handle those events.

Page 31: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

31

public void mouseClicked(MouseEvent e) {

ore code ...

}

Page 32: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

32

public void mouseClicked(MouseEvent e) {

}

Public so other programs can call it

Page 33: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

33

public void mouseClicked(MouseEvent e) {

}

Doesn’t return anything

Page 34: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

Anatomy of a Mouse Method

34

public void mouseClicked(MouseEvent e) {

}

It must have one of the mouse event names

Page 35: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

35

public void mouseClicked(MouseEvent e) {

}

A collection of information about the mouse event that just occurred

Page 36: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

36

public void mouseClicked(MouseEvent e) {

double mouseX = e.getX();

double mouseY = e.getY();

}

Get information about the event

Page 37: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

37

public void mouseClicked(MouseEvent e) {

double mouseX = e.getX(); // mouse X-coord

double mouseY = e.getY(); // mouse Y-coord

}

Page 38: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Anatomy of a Mouse Method

38

public void mouseClicked(MouseEvent e) {

double mouseX = e.getX(); // mouse X-coord

double mouseY = e.getY(); // mouse Y-coord

// more code ...

}

Page 39: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Hole Puncher

39

Page 40: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Hole Puncher. . .

import java.awt.event.*; // NEW

public class HolePuncher extends GraphicsProgram {

// Adds a “hole punch” where the user clicks

public void mouseClicked(MouseEvent e) {

// Get information about the event

double x = e.getX();

double y = e.getY();

// Add hole punch (GOval) at the mouse location

addHole(x, y);

}

private void addHole(double centerX, double centerY) { … }

} 40

Page 41: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Hole Puncher. . .

import java.awt.event.*; // NEW

public class HolePuncher extends GraphicsProgram {

// Adds a “hole punch” where the user clicks

public void mouseClicked(MouseEvent e) {

// Get information about the event

double x = e.getX();

double y = e.getY();

// Add hole punch (GOval) at the mouse location

addHole(x, y);

}

private void addHole(double centerX, double centerY) { … }

} 41

Page 42: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Hole Puncher. . .

import java.awt.event.*; // NEW

public class HolePuncher extends GraphicsProgram {

// Adds a “hole punch” where the user clicks

public void mouseClicked(MouseEvent e) {

// Get information about the event

double x = e.getX();

double y = e.getY();

// Add hole punch (GOval) at the mouse location

addHole(x, y);

}

private void addHole(double centerX, double centerY) { … }

} 42

Page 43: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Types of Mouse Events● There are many different types of mouse events!● Each takes the form:

public void eventMethodName(MouseEvent e) { ...

43

Page 44: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Doodler

44

Page 45: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Doodlerprivate static final int SIZE = 10;

...

public void mouseDragged(MouseEvent event) {

double mouseX = event.getX();

double mouseY = event.getY();

double rectX = mouseX – SIZE / 2.0;

double rectY = mouseY – SIZE / 2.0;

GRect rect = new GRect(rectX, rectY, SIZE, SIZE);

rect.setFilled(true);

rect.setColor(Color.MAGENTA);

add(rect);

}45

Page 46: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Doodlerprivate static final int SIZE = 10;

...

public void mouseDragged(MouseEvent event) {

double mouseX = event.getX();

double mouseY = event.getY();

double rectX = mouseX – SIZE / 2.0;

double rectY = mouseY – SIZE / 2.0;

GRect rect = new GRect(rectX, rectY, SIZE, SIZE);

rect.setFilled(true);

rect.setColor(Color.MAGENTA);

add(rect);

}46

Page 47: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Doodlerprivate static final int SIZE = 10;

...

public void mouseDragged(MouseEvent event) {

double mouseX = event.getX();

double mouseY = event.getY();

double rectX = mouseX – SIZE / 2.0;

double rectY = mouseY – SIZE / 2.0;

GRect rect = new GRect(rectX, rectY, SIZE, SIZE);

rect.setFilled(true);

rect.setColor(Color.MAGENTA);

add(rect);

}47

Page 48: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: Doodlerprivate static final int SIZE = 10;

...

public void mouseDragged(MouseEvent event) {

double mouseX = event.getX();

double mouseY = event.getY();

double rectX = mouseX – SIZE / 2.0;

double rectY = mouseY – SIZE / 2.0;

GRect rect = new GRect(rectX, rectY, SIZE, SIZE);

rect.setFilled(true);

rect.setColor(Color.MAGENTA);

add(rect);

}48

Page 49: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

49

1. User performs some action, like moving / clicking the mouse.

Page 50: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

50

1. User performs some action, like moving / clicking the mouse.

click!

Page 51: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

51

1. User performs some action, like moving / clicking the mouse.2. This causes an event to occur!

click!

Page 52: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

52

1. User performs some action, like moving / clicking the mouse.2. This causes an event to occur!

Event!

click!

Page 53: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

53

1. User performs some action, like moving / clicking the mouse.2. This causes an event to occur!3. Java executes a particular method to handle the event.

Event!

click!

Page 54: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

54

1. User performs some action, like moving / clicking the mouse.2. This causes an event to occur!3. Java executes a particular method to handle the event.

Event!

public void mouseClicked(...) {

...

}

click!

Page 55: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

55

1. User performs some action, like moving / clicking the mouse.2. This causes an event to occur!3. Java executes a particular method to handle the event.4. That method’s code updates the screen appearance in some way

Event!

public void mouseClicked(...) {

...

}

click!

Page 56: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Recap: Events

56

1. User performs some action, like moving / clicking the mouse.2. This causes an event to occur!3. Java executes a particular method to handle the event.4. That method’s code updates the screen appearance in some way

Event!

public void mouseClicked(...) {

...

}

Page 57: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Revisiting Doodler

public void mouseDragged(MouseEvent event) {

double mouseX = event.getX();

double mouseY = event.getY();

double rectX = mouseX – SIZE / 2.0;

double rectY = mouseY – SIZE / 2.0;

GRect rect = new GRect(rectX, rectY, SIZE, SIZE);

rect.setFilled(true);

rect.setColor(Color.MAGENTA);

add(rect);

}

57

Page 58: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Revisiting Doodler

public void mouseDragged(MouseEvent event) {

double mouseX = event.getX();

double mouseY = event.getY();

double rectX = mouseX – SIZE / 2.0;

double rectY = mouseY – SIZE / 2.0;

GRect rect = new GRect(rectX, rectY, SIZE, SIZE);

rect.setFilled(true);

rect.setColor(Color.MAGENTA);

add(rect);

}

58

What if we wanted the same GRect to track the mouse, instead of making a new one each time?

Page 59: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

MouseTracker

59

Page 60: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

A Problem...

public void mouseMoved(MouseEvent e) {

double mouseX = e.getX();

double mouseY = e.getY();

// more code ...

}

60

Page 61: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

A Problem...

public void mouseMoved(MouseEvent e) {

double mouseX = e.getX();

double mouseY = e.getY();

// more code ...

}

61

You don’t call this method, so you can’t specify its parameters

Page 62: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

A Problem...

public void mouseMoved(MouseEvent e) {

double mouseX = e.getX();

double mouseY = e.getY();

// more code ...

}

62

So, how can we give mouseMoved access to a

single GRect we want to track?You don’t call this method, so you

can’t specify its parameters

Page 63: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

63

Page 64: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Instance Variables1. Variables exist until their inner-most control block ends.

64

Page 65: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Instance Variables1. 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!

65

Page 66: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Instance Variables1. 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.

66

Page 67: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Instance Variables1. 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.

private type name; // declared outside any method!

67

Page 68: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Instance Variables1. 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.

private type name; // declared outside any method!

68

private GRect square;

public void run() {

square = new GRect(...);

GRect localSquare = new GRect(...);

}

Page 69: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Example: MouseTracker

69

Page 70: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Instance Variables + Events

/* Instance variable for the square to be tracked */

private GRect square;

public void run() {

square = makeSquare();

add(square);

}

public void mouseMoved(MouseEvent e) {

double x = e.getX() - SQUARE_SIZE / 2.0;

double y = e.getY() - SQUARE_SIZE / 2.0;

square.setLocation(x, y);

} 70

Use instance variables if you need to pass information between the run method and the mouse event methods.

Page 71: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

The Importance of Style● It is considered extremely poor style to use instance variables

unnecessarily:

Do not use instance variables where local variables, parameters, and return values suffice.

71

Page 72: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

The Importance of Style● It is considered extremely poor style to use instance variables

unnecessarily:

Do not use instance variables where local variables, parameters, and return values suffice.

● Use local variables for temporary information.

72

Page 73: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

The Importance of Style● It is considered extremely poor style to use instance variables

unnecessarily:

Do not use instance variables where local variables, parameters, and return values suffice.

● Use local variables for temporary information.● Use parameters to communicate data into a method.

73

Page 74: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

The Importance of Style● It is considered extremely poor style to use instance variables

unnecessarily:

Do not use instance variables where local variables, parameters, and return values suffice.

● Use local variables for temporary information.● Use parameters to communicate data into a method. ● Use return values to communicate data out of a method.

74

Page 75: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

75

Page 76: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Putting it all together

76

Page 77: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Whack-a-MoleLet’s use instance variables and events to make Whack-A-Mole!● A mole should appear every second at a random location, and stop

once the user has gotten at least 10 points.● If the user clicks a mole, remove it and increase their score by 1● There should be a GLabel in the left corner showing their score

77

Page 78: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Let’s Code It!

78

Page 79: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Exception● If the user clicks an area with no mole, the program crashes

○ A program crash in Java is called an exception○ When you get an exception, Eclipse shows red error text○ The error text shows the line number where the error occurred○ Why did this error happen?○ How can we avoid it?

79

Page 80: Mouse Events and Instance Variables - Stanford University€¦ · Mouse Events and Instance Variables CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 10 With inspiration

Plan for Today

● Review: Animation & Randomness● getElementAt & Null● Event-driven Programming● Instance Variables● Whack-a-Mole

Next Time: Tracing & Memory

80