Contents

  1. ./rectangle.py

./rectangle.py

# Draw a rectangle using the graphics API
# by CSCI111

from graphics import *

# Create a window
win = GraphWin("My Rectangle", 200, 200)

# Create an instance of a rectangle that is blue and 50x100
# pixels in the upper left of the window

upperLeftPoint = Point(0, 0)
lowerRightPoint = Point(50, 100)

myRectangle = Rectangle(upperLeftPoint, lowerRightPoint)
myRectangle.setFill("blue")

# draw the rectangle
myRectangle.draw(win)

# pause so we can see the rectangle shift
win.getMouse()

# shift the instance of the rectangle to the right 10 pixels
myRectangle.move(10, 0)

# find out the x- and y-coordinates of the 
# upper-left corner of the rectangle
newUpperLeftPoint = myRectangle.getP1()
newUpperLeftX = newUpperLeftPoint.getX()
newUpperLeftY = newUpperLeftPoint.getY()

# When you run the program, note the output

# this one prints out the _Point_ object
print("The new upper left point is", newUpperLeftPoint)

# these print out the integers for the x and y coordinates, respectively
print("The new upper left point's x is", newUpperLeftX)
print("The new upper left point's y is", newUpperLeftY)

# this one prints out the point's coordinates in my own desired format.
print("The new upper left point's coordinates are (", newUpperLeftX, ",", newUpperLeftY, ")") 


# pause so that the window doesn't close before we want it to close
win.getMouse()

Generated by GNU Enscript 1.6.5.90.