Some Graphics



In object-oriented programming, we group together data and operations on that data into objects.

First we create the objects - lists, windows, circles, etc., and then we use them. Objects contain data, and they know how to do things (perform operations).

Example: Circle Object
Data: My center is (20, 20), my radius is 10, my color is blue
Operations: draw myself on a graphics window, move myself to a different location on the window, set the color of my interior or outline


We haven't used any graphics objects before. We will use John Zelle's graphics module to simplify the creation and use of objects such as circles, rectangles, lines, and graphic windows. To use this module,  save the graphics.py file in the same directory as your python program. (More information in the project 8 specification).

Example:
# import all the functions from graphics
from graphics import *

# create a graphics window
win = GraphWin("My First Graphics Window Title", 300, 300)   # window is 300x300 - top left corner is (0, 0) and
                                                                                                          # bottom right corner is (300, 300)

# create a Circle object with center coordinates (150, 150) and radius 40
cir = Circle(Point(150, 150), 40)

# set color of circle's interior to green
cir.setFill('green')

# draw the circle on the window
cir.draw(win)

# close window when mouse is clicked on it
win.getMouse()


Output:









Windows



We create a graphic window using a GraphWin() object. We can then draw graphical images on the window object. A program can create multiple graphic windows.

Operations for GraphWin() objects:

GraphWin(title, width, height) - constructs a new graphics window on the screen. All parameters are optional - the default size is 200x200.

setBackground(color) - set the window's background to the specified color (which is a string) - some options are 'red', 'cyan', 'green', 'blue', 'purple', 'yellow'

getMouse() - pause for a mouse click in the graphics window, and return the Point at which the mouse was clicked. We will use a call to this method to allow us to close the graphics window by clicking anywhere on the window.


Example: creating a graphics window

from graphics import *

win = GraphWin()
win2 = GraphWin("Second window", 300, 300)







Methods for the drawable objects Point, Line, Circle, Oval, Rectangle



setFill(color) - set the interior color of the object to the specified color

setOutline(color) - set the outline of the object to the specified color

setWidth(pixels) - set the width of the object's outline to the specified number of pixels

draw(graphicWindow) - draw the object on the specified graphics window

undraw() - undraws the objects

move(dx, dy) - moves the object dx units in the horizontal direction and dy units in the vertical direction





Point Objects



Points are often used to define the position of other objects, but can also be drawn on the window.

Example:
pt = Point(15, 55)  # x coord is 15, y coord is 55
pt.setOutline('purple')  # set outline color of point to purple
pt.draw(win)    # draw the point on the window win



Operations for Point objects:

Point(x, y) - construct a Point with the specified coordinates x and y

getX() - returns the x coordinate of the point

getY() - returns the y coordinate of the point






Circle Objects



A Circle is defined by its center coordinates (given as a Point) and its radius.

Example:
cir = Circle(Point(50, 100), 25)  # center = (50, 100), radius = 25

cir.setFill('yellow')   # set circle's interior to yello

cir.draw(win)   # draw the circle on window



Operations for Circle objects:

Circle(centerPoint, radius) - constructs a Circle with specified center and radius

getRadius() - returns the radius of the circle




Line Objects



Example:
win = GraphWin("Another window", 200, 200)

diag = Line(Point(0, 0), Point(200, 200))   # line from top left to bottom right of window
diag.setWidth(15)  # make the line wider
diag.setOutline('blue')  # make the line blue
diag.draw(win)  # draw line on window


Operations on Line objects:

Line(point1, point2) - construct a line from point1 to point2

setArrow(string) - set the arrow status of the line. Arrows can occur at the first point, the end point, or both - possible values of the argument are 'first', 'last', 'both', or 'none'. The default is 'none'.





Rectangle Objects




Example:
win = GraphWin()
rec1 = Rectangle(Point(3, 4), Point(8, 10)  # opposite corners at specified points
rec1.draw(win)


Operations on Rectangle objects:

Rectangle(point1, point2) - constructs a Rectangle with opposite corners point1 and point2





Exercise: Draw the following shapes on a window:

d



Exercise: Produce the following window: