objects and graphics - stanford university€¦ · evaluates to true. a loop of the ... tuesday,...
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/24.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/26.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/27.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/29.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/30.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/31.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/32.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022071001/5fbd64d430c08e34c3305014/html5/thumbnails/33.jpg)
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;