objects and graphics - stanford university€¦ · evaluates to true. a loop of the ... tuesday,...

33
Objects and Graphics

Upload: others

Post on 17-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Objects and Graphics

Page 2: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

One Last Thought on Loops...

Page 3: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Looping Forever

● while loops iterate as long as their condition evaluates to true.

● A loop of the form while (true) will loop forever (unless something stops it).

● You can immediately exit a loop by using the break statement.

while (true) { … if ( … ) break;}

Page 4: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Looping Forever

● while loops iterate as long as their condition evaluates to true.

● A loop of the form while (true) will loop forever (unless something stops it).

● You can immediately exit a loop by using the break statement.

while (true) { … if ( … ) break;}

Page 5: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

The “Loop-and-a-Half” Idiom

● Often you will need to● read a value from the user,● decide whether to continue, and if so● process the value.

● Technique: The loop-and-a-half idiom:

while (true) { /* … get a value from the user … */ if (condition) break;

/* … process the value … */}

Page 6: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Time-Out For Announcements!

Page 7: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Programming Assignment 2

● Second programming assignment is currently out and is due on Friday, January 31.

● Suggestion: Have PythagoreanTheorem, HailstoneSequence, and FindRange done by Friday.

● Assignment review hours this Sunday, January 26 from 5PM – 6PM in Hewlett 200.● Will not be recorded, but we'll post notes online.

Page 8: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Keith's Office Hours

● New OH location: Gates 300.● Same times as before:

● Tuesday, 10:15AM – 12:15PM● Wednesday, 4:30PM – 6:30PM

● Everyone is welcome!

Page 9: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

A Friendly Reminder: Honor Code

Page 10: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Your Emails

● As of last night, I've read 532 of your emails.

● I'll try to compile a list of some of the really wonderful stories from those emails for Friday.

● If you'd prefer that I not include your interesting story in the slides, please feel free to email me. I respect your privacy!

Page 11: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Casual CS Dinner

● Casual dinner for women studying computer science is tonight on the Gates fifth floor, starting at 6PM.

● I'll be shortening my normal office hours in Gates 300 tonight to 4:30PM – 6:00PM.

● Everyone is welcome!

Page 12: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Back to CS106A!

Page 13: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Object-Oriented Programming

Page 14: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

An object is an entitythat has state and behavior.

Page 15: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Has a fur color.Has an energy level.

Has a level of cuteness.Can be your friend.

Can sit.Can stay.Can bark.

Page 16: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

An object is an entitythat has state and behavior.

A class is a set of features and behaviorcommon to a group of objects.

An instance of a class is anobject that belongs to that class.

Page 17: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Programming with Graphics

Page 18: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

The GObject Hierarchy

The classes that represent graphical objects form a hierarchy, part of which looks like this:

GObject

GRect GOval GLineGLabel

Graphic courtesy of Eric Roberts

Page 19: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

public class HelloProgram extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello, world", 100, 75); label.setFont("SansSerif-36"); label.setColor(Color.RED); add(label); }}

HelloProgram

Sending Messages to a GLabel

hello, world

label

hello, worldhello, worldhello, world

Graphic courtesy of Eric Roberts

Page 20: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Objects and Variables

● Variables can be declared to hold objects.● The type of the variable is the name of

the class:● GLabel label;● GOval oval;

● Instances of a class can be created using the new keyword:● GLabel label = new GLabel("Y?", 0, 0);

Page 21: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Sending Messages

● To call a method on an object stored in a variable, use the syntax

object.method(parameters)

● For example: label.setFont("Comic Sans-32");

label.setColor(Color.ORANGE);

Page 22: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Graphics Coordinates

HelloProgram

hello, world

• Origin is upper left.

• x coordinates increase from left to right.

• y coordinates increase from top to bottom.

• Units are pixels (dots on the screen).

• GLabel coordinates are baseline of first character.+x

+y

Graphic courtesy of Eric Roberts

Page 23: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Graphics Coordinates

HelloProgram

hello, world

(100, 75)

• Origin is upper left.

• x coordinates increase from left to right.

• y coordinates increase from top to bottom.

• Units are pixels (dots on the screen).

• GLabel coordinates are baseline of first character.+x

+y

Graphic courtesy of Eric Roberts

Page 24: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Drawing Geometrical Objects

Graphic courtesy of Eric Roberts

Page 25: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Graphics Program

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

Graphic courtesy of Eric Roberts

+x

+y

(x, y)

(x + width, y + height)

Page 26: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.

Graphic courtesy of Eric Roberts

Graphics Program

+x

+y

(x, y)

(x + width, y + height)

Page 27: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.

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

Graphic courtesy of Eric Roberts

Graphics Program

+x

+y

(x0, y

0)

(x1, y

1)

Page 28: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.

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.

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

Graphic courtesy of Eric Roberts

Page 29: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

The Collage Model

Page 30: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

The Collage Model

Page 31: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Size of the Graphics Window

Methods provided by GraphicsProgram class getWidth()

Returns the width of the graphics window.

getHeight()

Returns the height of the graphics window.

Based on slides by Eric Roberts

Note: receiver of these calls is the GraphicsProgram itself, so we don’t need to specify a separate object as receiver.

Page 32: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Centering an Object

Graphics Program

getWidth();

W

getWidth() / 2.0;

W / 2.0

Page 33: Objects and Graphics - Stanford University€¦ · evaluates to true. A loop of the ... Tuesday, 10:15AM – 12:15PM ... If you'd prefer that I not include your interesting story

Centering an Object

Graphics Program

getWidth();

W

getWidth() / 2.0;

W / 2.0

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