more methods and graphics -...

102
More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 8 With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.

Upload: duonglien

Post on 26-Aug-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

More Methods and Graphics

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

Lecture 8

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

Page 2: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Announcements

● Hope you had a great 4th!● Assignment 2 due Wednesday July 10th, at 10AM

2

Page 3: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Plan for Today

● Review: Nested Loops, Infinite Loops, Intro to Graphics● Returning to Returns● More Graphics!● Stoplights

3

Page 4: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Plan for Today

● Review: Nested Loops, Infinite Loops, Intro to Graphics● Returning to Returns● More Graphics!● Stoplights

4

Page 5: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Nested For Loops

5

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

* * * * * * *

for (int i = 0; i < NUM_ROWS; i++) {for (int j = 0; j < NUM_COLS; j++) {

print(“*”);}println();

}

Page 6: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Infinite Loops

6

int answer = readInt(“What is 1 + 1?”);

while (answer != 2){

println(“Wrong answer! Try again”);}

Page 7: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Infinite Loops

7

while (true) {

// some code that never// stops repeating ...

}

int answer = readInt(“What is 1 + 1?”);

while (answer != 2){

println(“Wrong answer! Try again”);}

Page 8: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Intro to Graphics Programs

8

Program

Karel Program Console Program Graphics Program

Page 9: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Intro to Graphics Programs

9

Page 10: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Our First Graphics Program

10

import acm.program.*;

import acm.graphics.*; // Stanford graphical objects

import java.awt.*; // Java graphical objects

public class BlueSquare extends GraphicsProgram {

public void run(){

drawBlueSquare();}

private void drawBlueSquare(){GRect rect = new GRect(200, 200);rect.setFilled(true);rect.setColor(Color.BLUE);add(rect, 50, 50);

}}

Page 11: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Our First Graphics Program

11

import acm.program.*;

import acm.graphics.*; // Stanford graphical objects

import java.awt.*; // Java graphical objects

public class BlueSquare extends GraphicsProgram {

public void run(){

drawBlueSquare();}

private void drawBlueSquare(){GRect rect = new GRect(200, 200);rect.setFilled(true);rect.setColor(Color.BLUE);add(rect, 50, 50);

}}

Page 12: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: The Graphics Canvas

12

(0, 0)

* Note: The y coordinate gets bigger as we go down.This is opposite of how it acts in most math classes!

x

y

(40, 20)(120, 40)

(40, 120)

getWidth();

getHeight();

Page 13: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Review: Checkerboard

13

Page 14: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Plan for Today

● Review: Nested Loops, Infinite Loops, Intro to Graphics● Returning to Returns● More Graphics!● Stoplights

14

Page 15: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Returning to Returns

15

● Recall that methods can return data

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

double avg = (num1 + num2) / 2.0;return avg;

}

Page 16: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Returning to Returns

16

● Recall that methods can return data

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

double avg = (num1 + num2) / 2.0;return avg;

}

Page 17: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Returning to Returns

17

● Recall that methods can return data

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

double avg = (num1 + num2) / 2.0;return avg;

}

average

7.5

Page 18: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

● You can have multiple return statements in a method

18

Page 19: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

19

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

● You can have multiple return statements in a method

Page 20: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

20

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

Page 21: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

21

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

Page 22: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

22

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

Page 23: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

23

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

Page 24: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

24

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

Page 25: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

25

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

max memory

num1 num2

Page 26: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

26

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

max memory

num1 num2

5

Page 27: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

27

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

max memory

num1 num2

5 1

Page 28: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

28

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

max memory

num1 num2

5 1

Page 29: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

29

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

max memory

num1 num2

5 15

Page 30: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

30

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables

max memory

num1 num2

5 1

5

Page 31: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

31

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

No variables5

Page 32: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

32

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

5

larger

5

Page 33: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Multiple Return Statements

33

public void run() {

int larger = max(5, 1);

}

private int max(int num1, int num2) {

if (num1 >= num2) {

return num1;

}

return num2;

}

run memory

larger

5

Page 34: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Returning to our scheduled programming

34

private topic getTopic() {

if (isMonday()) {

return moreGraphics;} else if (isTues()) {

return animation;} else {

return randomExcitingTopic;}

}

Page 35: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Plan for Today

● Review: Nested Loops, Infinite Loops, Intro to Graphics● Returning to Returns● More Graphics!● Stoplights

35

Page 36: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

36

HW 3:

Page 37: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Working with Graphics

We make graphics programs by creating graphics objects and manipulating their properties.

37

Page 38: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Working with Graphics

We make graphics programs by creating graphics objects and manipulating their properties.

38

Page 39: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Graphical Objects

39

GRect GOval GLine GLabel

Hello

GImage

GArcGRoundRect GPolygon GCompound and more...

Page 40: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Graphical Objects

40

GRect GOval GLine GLabel

Hello

GImage

GArcGRoundRect GPolygon GCompound and more...

Page 41: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Graphical Objects

41

Page 42: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Working with Graphics

We make graphics programs by creating graphics objects and manipulating their properties.

42

Page 43: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Creating Graphics Objects

● To create a graphics object, we need to:○ declare a variable to hold that object, and ○ actually create the object using the new keyword.

● Initialization syntax:type name = new type(...);

● Example:GRect rect = new GRect(100, 200);

43

Page 44: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Creating Graphics Objects

● To create a graphics object, we need to:○ declare a variable to hold that object, and ○ actually create the object using the new keyword.

● Initialization syntax:type name = new type(...);

● Example:GRect rect = new GRect(100, 200);

44

This is called a “constructor”

Page 45: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Primitives vs. ObjectsPrimitive Variable Types

int

double

char

boolean

45

Object Variable Types

GRect

GOval

GLine

...

Page 46: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Primitives vs. ObjectsPrimitive Variable Types

int

double

char

boolean

46

Object Variable Types

GRect

GOval

GLine

...

Object variables:

Page 47: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Primitives vs. ObjectsPrimitive Variable Types

int

double

char

boolean

47

Object Variable Types

GRect

GOval

GLine

...

Object variables:1. Have UpperCamelCase types

Page 48: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Primitives vs. ObjectsPrimitive Variable Types

int

double

char

boolean

48

Object Variable Types

GRect

GOval

GLine

...

Object variables:1. Have UpperCamelCase types

2. Are constructed using new

Page 49: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Primitives vs. ObjectsPrimitive Variable Types

int

double

char

boolean

49

Object Variable Types

GRect

GOval

GLine

...

Object variables:1. Have UpperCamelCase types

2. Are constructed using new3. You can call methods on them

Page 50: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Working with Graphics

We make graphics programs by creating graphics objects and manipulating their properties.

50

Page 51: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Methods on Graphics Objects

● You can manipulate graphics objects by calling methods on those objects

● To call a method on an object, use the syntax:

object.method(parameters);

● Example:

rect.setColor(Color.BLUE);

51

Page 52: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Methods on Graphics Objects

● You can manipulate graphics objects by calling methods on those objects

● To call a method on an object, use the syntax:

object.method(parameters);

● Example:

rect.setColor(Color.BLUE);

52

receiver message

Page 53: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Methods on Graphics Objects

● You can manipulate graphics objects by calling methods on those objects

● To call a method on an object, use the syntax:

object.method(parameters);

● Example:

rect.setColor(Color.BLUE);

53

who?

Page 54: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Methods on Graphics Objects

● You can manipulate graphics objects by calling methods on those objects

● To call a method on an object, use the syntax:

object.method(parameters);

● Example:

rect.setColor(Color.BLUE);

54

who? what?

Page 55: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Methods on Graphics Objects

● You can manipulate graphics objects by calling methods on those objects

● To call a method on an object, use the syntax:

object.method(parameters);

● Example:

rect.setColor(Color.BLUE);

55

who? what? what specifically?

Page 56: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GObject MethodsThe following operations apply to all GObjects:

56Graphic courtesy of Eric Roberts

Page 57: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Colors● Specified as predefined Color constants:

Color.NAME, where NAME is one of:

rect.setColor(Color.MAGENTA);

57

Page 58: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Colors● Specified as predefined Color constants:

Color.NAME, where NAME is one of:

rect.setColor(Color.MAGENTA);

● Or, create one using Red-Green-Blue (RGB) values of 0-255new Color(red, green, blue);

ex:

rect.setColor(new Color(218, 165, 32)); // goldenrold 58

Page 59: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Graphical Objects

59

GRect GOval GLine GLabel

Hello

GImage

GArcGRoundRect GPolygon GCompound and more...

Page 60: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GRect

60

new GRect(x, y, width, height);Creates a rectangle with the given width and height, whose upper-left corner is at (x, y).

new GRect(width, height);Same as above, but defaults (x, y) to (0, 0).

Page 61: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GOval

61

new GOval(x, y, width, height);Creates an oval that fits within a rectangle with the given width and height, whose upper-left corner is at (x, y).

new GOval(width, height);Same as above, but defaults (x, y) to (0, 0).

Page 62: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GRect & GOval

62

Methods shared by the GRect and GOval classes:

object.setFilled(fill);If fill is true, fills in the interior of the object; if false, shows only the outline.

object.setFillColor(color);Sets the color used to fill the interior, which can be different from the border.

object.setSize(width, height);Sets the object’s size to be the given width and height.

Page 63: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GLine

63

new GLine(x0, y0, x1, y1);Creates a line extending from (x0, y0) to (x1, y1).

Page 64: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GLabel

64

new GLabel(“your text here”, x, y);Creates a label with the given text, whose baseline starts at (x, y). Not positioned according to the top-left corner!

new GLabel(“your text here”);Same as above, but defaults (x, y) to (0, 0).

Hi everyone!baseline (x, y)

Page 65: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GLabel

65

new GLabel(“your text here”, x, y);Creates a label with the given text, whose baseline starts at (x, y). Not positioned according to the top-left corner!

new GLabel(“your text here”);Same as above, but defaults (x, y) to (0, 0).

Hi everyone!baseline (x, y)

Page 66: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GLabel

66

new GLabel(“your text here”, x, y);Creates a label with the given text, whose baseline starts at (x, y). Not positioned according to the top-left corner!

new GLabel(“your text here”);Same as above, but defaults (x, y) to (0, 0).

Hi everyone!baseline (x, y)

height

Page 67: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GLabel

67

new GLabel(“your text here”, x, y);Creates a label with the given text, whose baseline starts at (x, y). Not positioned according to the top-left corner!

new GLabel(“your text here”);Same as above, but defaults (x, y) to (0, 0).

Hi everyone!baseline (x, y)

height ascent

descent

Page 68: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GLabel Methods

68

Methods specific to the GLabel class:

label.getDescent();Returns the height of the label below its baseline.

label.getAscent();Returns the height of the label above its baseline.

label.setFont(font);Sets the font used to display the label as specified by the font string.

The font is typically specified as a string in the form:“family-style-size”

family: is the name of a font familystyle: is either PLAIN, BOLD, ITALIC, or BOLDITALICsize: is an integer indicating the point size

Page 69: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GImage

69

new GImage(“filename”, x, y);Creates an image displaying the given file, whose upper-left corner is at (x, y).

new GImage(“filename”);Same as above, but defaults (x, y) to (0, 0).

Page 70: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GImage

70

new GImage(“filename”, x, y);Creates an image displaying the given file, whose upper-left corner is at (x, y).

new GImage(“filename”);Same as above, but defaults (x, y) to (0, 0).

* Passion flower right outside our lecture hall. Nature is amazing :)

Page 71: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GImage Methods

71

object.setSize(width, height);Sets the object’s size to be the given width and height.

Page 72: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

The Collage Model

72

(0, 0)

Page 73: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

The Collage Model

73

(0, 0)

add(myOval);

Page 74: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

The Collage Model

74

(0, 0)

Page 75: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

The Collage Model

75

(0, 0)

Page 76: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

The Collage Model

76

(0, 0)

Page 77: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GraphicsProgram MethodsGraphicsProgram contains these useful methods:

77

Page 78: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

GraphicsProgram MethodsGraphicsProgram contains these useful methods:

78

Page 79: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

79

How can we nicely center an object on our canvas?

Page 80: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

80

Page 81: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

81

(?, ?)

Page 82: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

82

(?, ?)

Page 83: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

83

(?, ?)

Page 84: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

84

(?, ?)

double x = getWidth() / 2.0 - W / 2.0;

Page 85: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

85

(?, ?)

double x = getWidth() / 2.0 - W / 2.0;

getHeight();

Page 86: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Centering

86

(?, ?)

getHeight();

double x = getWidth() / 2.0 - W / 2.0;double y = getHeight() / 2.0 - H / 2.0;

Page 87: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Plan for Today

● Review: Nested Loops, Infinite Loops, Intro to Graphics● Returning to Returns● More Graphics!● Stoplights

87

Page 88: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

88

How would you make a method for drawing stoplights of different locations & sizes?

Page 89: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

89

What information do we need in order to draw this?

Page 90: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

90

What’s the Pseudocode?

Make stoplight (needs what info??):

Page 91: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

91

What’s the Pseudocode?

Make stoplight (location, size):

Page 92: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

92

What’s the Pseudocode?

Make stoplight (location, size):Make backgroundMake red circleMake yellow circleMake green circle

Page 93: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

93

What’s the Pseudocode?

Make stoplight (location, size):Make backgroundMake red circleMake yellow circleMake green circle

Hmm, these are almost the same… what can we do?

Page 94: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

94

What’s the Pseudocode?

Make stoplight (location, size):Make backgroundMake colored circle (red)Make colored circle (yellow)Make colored circle (green)

A helper method!

Page 95: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

95

What’s the Pseudocode?

Make stoplight (location, size):Make backgroundMake colored circle (red)Make colored circle (yellow)Make colored circle (green)

Make colored circle (needs what info?):

Page 96: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

96

What’s the Pseudocode?

Make stoplight (location, size):Make backgroundMake colored circle (red)Make colored circle (yellow)Make colored circle (green)

Make colored circle (location, size, color):

Page 97: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Practice: Stoplights

97

What’s the Pseudocode?

Make stoplight (location, size):Make backgroundMake colored circle (red)Make colored circle (yellow)Make colored circle (green)

Make colored circle (location, size, color):Make circle of provided size and color

and add it at the provided location

Page 98: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Let’s Code It!

98

Page 99: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Extra Practice: Line ArtWrite a graphical program LineArt that draws a series of lines (see lecture code for solution):

● Outer square is at (10, 30) and size 200x200

● Each line is 10px apart in each direction

99

Page 100: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Extra: GCompoundA GCompound contains other GObjects. It’s useful when you want to do one operation on multiple GObjects at the same time (e.g., move all of them by the same amount).

GCompound compound = new GCompound();

compound.add(shape);

compound.add(shape);

...

compound.add(shape);

add(compound);

Example: you can make a GCompound to represent a car.100

Page 101: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Extra: GCompoundsetBackground(Color.YELLOW); GCompound car = new GCompound();

GRect body = new GRect(10, 30, 100, 50); body.setFilled(true); body.setFillColor(Color.BLUE); car.add(body);

GOval wheel1 = new GOval(20, 70, 20, 20); wheel1.setFilled(true); wheel1.setFillColor(Color.RED); car.add(wheel1);

GOval wheel2 = new GOval(80, 70, 20, 20); wheel2.setFilled(true); wheel2.setFillColor(Color.RED); car.add(wheel2);

GRect windshield = new GRect(80, 40, 30, 20); windshield.setFilled(true); windshield.setFillColor(Color.CYAN); car.add(windshield);

add(car); // adds whole GCompound (like a “sub-canvas”) to the canvas 101

Page 102: More Methods and Graphics - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/Lecture8/Lecture8.pdf · More Methods and Graphics CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht

Plan for Today

● Review: Nested Loops, Infinite Loops, Intro to Graphics● Returning to Returns● More Graphics!● Stoplights

Extra: Line Art practice & GCompound

Next time: Animation!

102