90 likes | 249 Views
Lecture 8. Introducing Color and Text. Review. We create an object when we call its constructor. Objects can be created with or without a name p oint(30,50) p 1 = Point(30,50) Unless an object has a name you can’t give it commands unless you do it right away: point(30,50 ).draw(w)
E N D
Lecture 8 Introducing Color and Text
Review • We create an object when we call its constructor. • Objects can be created with or without a name • point(30,50) • p1 = Point(30,50) • Unless an object has a name you can’t give it commands unless you do it right away: • point(30,50).draw(w) • p1.draw(w)
Graphics objects can doa lot of things • p. 152 lists all the operations that can be performed by any Point, Line, Circle, Oval, Rectangle, Polygon and Text. • The subsequent pages give you extra operations that particular kinds of objects can do. • E.g. if c is a Circle • p = c.getCenter() returns a clone of c’s center. • p is a Point object. How do you get its coordinates?
Parameters • You can create the parameters before you call the constructor or while you call the constructor. • p1 = Point(100,100) c = Circle(p1,15) • c = Circle(Point(100,100), 15)
Mouse clicks • p = win.getMouse() • Returns a Point whose x and y values you can recover by p.getX() and p.getY() • Uses • Closing a window when the user is ready to do so • Getting other information from the user
Class problem • Let the user click twice on the screen and draw a line segment between the two points. • Algorithm: • Create a GraphWin object • Prompt the user to click in the window • Get the first point • Prompt the user to click again in the window • Get the second point • Draw a line between the two points • Wait for a mouse click to close the window
Collecting a list of mouse clicks • We can use a list of points to make a polygon • That list is then passed as a parameter to the Polygon constructor. • Those points can be gotten from mouse clicks • tree.py • tree1.py
Color using rgb values • You can actually mix your own colors rather than relying on color names like “red”, “green” etc. • We use the function color_rgb(red,green,blue) where the red, green and blue are integer values somewhere between 0 and 255. • color_rgb(0,255,0) is very bright green • color_rgb(130,0,130) is a medium magenta
Text • Text(anchorPoint, string) • Creates a string which is centered at anchorPoint • t1 = Text(Point(100,20), “Enter 2 points”) • t1.draw(w) • Or if you don’t want to name the point • Text(Point(100,20), “Enter 2 points”).draw(w) • See other methods on p. 154