1 / 9

Lecture 8

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)

fonda
Télécharger la présentation

Lecture 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8 Introducing Color and Text

  2. 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)

  3. 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?

  4. 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)

  5. 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

  6. 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

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

  8. 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

  9. 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

More Related