Using the Zelle Graphics Library

GraphWin | Point | Line | Rectangle | Circle | Oval | Polygon | Text | Colors
Introduction

This graphics library allows you to draw colorful pictures on the computer. To use the graphics library, first import it:

	 from graphics import *

Here is a short example showing how to create a new graphics window object and then create and draw a circle within this window:

	 win = GraphWin("Graphics!", 500, 500)   # creates new GraphWin object, 500x500 pixels in size
	 win.setCoords(0, 0, 500, 500)           # places the origin in lower left corner
	 win.setBackground("blue")               # set the background color to blue

	 cp = Point(50,50)                       # creates a Point at 50,50
	 circ = Circle(cp, 20)                   # creates a Circle centered at point cp with radius of 20
	 circ.setFill("red")                     # fills the Circle with the color red
	 circ.draw(win)                          # draws the Circle in the window

GraphWin

Use the GraphWin to create a canvas on which to draw other objects.

        GraphWin(title, width, height)
  
        # Sets the coordinate system of the window so that the lower
        # left corner is at (xll, yll) and the upper right corner is
        # at (xur, yur)
        setCoords(xll, yll, xur, yur)   
        close()
        getHeight()
        getWidth()
        getMouse() # wait for mouse click and return Point obj representing click location
        setBackground(color)   # color could be something like "red"
Point

The Point is a building block for all of the other more complicated graphical objects, such as Lines, Circles, and Rectangles.

     Point(x, y)

     getX()     # the int value of the Point's x-coordinate
     getY()     # the int value of the Point's y-coordinate

     ----------------------- Methods common to all Graphics Object classes:

     draw(graphwin):    # Draw the object in a graphics window
     move(dx, dy):      # move object dx units in x and dy units in y direction
     setFill(color):    # Set interior color to the given color
     setOutline(color): # Set outline color to the given color
     setWidth(width):   # Set line weight to width
     undraw():          # Undraw the object (i.e. hide it) 

  Example:

  p1 = Point(300, 450)
  p1.draw(mywin)
  x1 = p1.getX()
  x2 = p1.getY()
  p2 = p1.clone()
  p2.move(100,20)
  p2.draw(mywin)

Line

A Line is defined by two points.

     Line(p1, p2)   # p1 and p2 are the end Points of the line

     clone()
     getCenter()    # returns a Point object corresponding to the Line's center
     getP1()        # get one end-point Point object
     getP2()        # get the other Point object

     draw(graphwin)
     move(dx, dy)   
     setFill(color)
     setOutline(color)
     setWidth(width) 

  Example:

  longline = Line(Point(0, 250), Point(500, 250))
  longline.setWidth(5)
  longline.draw(mywin)

Rectangle

A Rectangle is defined by two points at opposite corners.

     Rectangle( p1, p2)  # p1 and p2 are points for upper left and lower right

     clone()
     getCenter()  # returns a Point object corresponding to the center
     getP1()      # returns the upper left corner Point
     getP2()      # returns the lower right corner Point

     draw(graphwin)
     move(dx, dy)	 
     setFill(color)
     setOutline(color)
     setWidth(width)   
     undraw()

  Example:

  square = Rectangle(Point(100, 100), Point(200, 200))
  square.draw(mywin)

Circle

A Circle is defined by its center point and raidus.

     Circle(p1, radius)       # p1 is a Point at the center of the circle, radius is an int

     clone()
     getCenter()              # returns the Point object describing the Circle's center
     getRadius()              # returns the int value of the Circle's radius

     draw(graphwin)
     move(dx, dy)   
     setFill(color)   
     setOutline(color)
     setWidth(width)  
     undraw()

  Example:

  center = Point(100, 100)
  radius = 35
  sun = Circle(center, radius)
  sun.setFill("yellow")
  sun.setOutline("yellow")
  sun.draw(mywin)

Oval

An Oval is defined by two points that set the opposite corners of a bounding box.

     Oval(p1, p2)    # p1 and p2 are Points of opposite corners of bounding rectangle 

     clone()
     getCenter()
     getP1()
     getP2()

     draw(graphwin)
     move(dx, dy)
     setFill(color)
     setOutline(color)
     setWidth(width)

Polygon

A Polygon is a many sided shape defined by a list of points. The Polygon is drawn by connecting each point in succession and finally connecting the last point to the first.

     Polygon([point1, point2, ..., pointn])

     getPoints()  # return a list of Points in the polygon
     clone()

     draw(graphwin)
     move(dx, dy)
     setFill(color)
     setOutline(color)
     setWidth(width)
     undraw()

  Example (a triangle):

  tri = Polygon([Point(10,20), Point(10,60), Point(30,40)])
  tri.setOutline("pink")
  tri.setFill("blue")
  tri.draw(mywin)

Text

A Text object is used to create a text box on the graphical window.

     Text(p, text)  # p is center point and text is string
    
     clone()
     getAnchor()         # returns clone of anchor point
     getText()           # get the text of this Text Object
     setText(text)       # change text of given object
     setTextColor(color)
     setFace(family)     # set font face, ex. "arial" "courier"
     setSize(size)       # set font size (5-36 are legal)    
     setStyle(style)     # set font style  ex.  "bold" "italic"

     draw(graphwin)
     move(dx, dy)
     setFill(color)
     setOutline(color)
     setWidth(width)
     undraw()


  Example:

  message = Text(Point(300, 100), "Hello World!")
  message.setSize(24)
  message.setTextColor("red")
  message.draw(mywin)

Defining your own Colors

You can use the color_rgb() function to create colors based on different amounts of red, green, and blue. Here's an example of how it's used:

p = Point(100,200)
c = Circle(p, 50)
mycolor = color_rgb(100,200,0)
c.setFill(mycolor)

In the above example, the arguments to color_rgb() are integers representing the amount of red, green, and blue. They can by any integer from 0-255. So the above color is a bright green (red=100, green=200, blue=0). Using color_rgb(50,50,50) would give a dark grey, and color_rgb(0,255,255) would be cyan (red off, green and blue full on).