The package graphics.py
is a simple object oriented graphics library designed to make it very easy for novice programmers to experiment with computer graphics in an object oriented fashion. It was written by John Zelle for use with the book "Python Programming: An Introduction to Computer Science" (Franklin, Beedle & Associates).
There are two kinds of objects in the library. The GraphWin
class implements a window where drawing can be done, and various GraphicsObject
s are provided that can be drawn into a GraphWin
. As a simple example, here is a complete program to draw a circle of radius 10 centered in a 200x200 window:
from graphics import * def main(): win = GraphWin("My Circle", 200, 200) c = Circle(Point(100,100), 10) c.draw(win) win.getMouse() # pause for click in window win.close() main()
GraphWin
represents a window on the screen where graphical images may be drawn. A program may define any number of GraphWin
s. A GraphWin
understands the following methods:
GraphWin(title, width, height)
plot(x, y, color)
(x,y)
in the window. Color is optional, black is the default.plotPixel(x, y, Color)
(x,y)
ignoring any coordinate transformations set up by setCoords
.setBackground(color)
close()
getMouse()
Point
object.checkMouse()
getMouse
, but does not pause for a user click. Returns the latest Point
where the mouse was clicked orNone
if the window as not been clicked since the previous call to checkMouse
or getMouse
. This is particularly useful for controlling simple animation loops.getWidth()
getHeight()
setCoords(xll, yll, xur, yur)
(xll, yll)
and the upper-right corner is (xur, yur)
. All subsequent drawing will be done with respect to the altered coordinate system (except for plotPixel
).Point
, Line
, Circle
, Oval
, Rectangle
, Polygon
, Text
. All objects are initially created unfilled with a black outline. All graphics objects support the following generic set of methods:
setFill(color)
setOutline(color)
setWidth(pixels)
Point
.)draw(GraphWin)
GraphWin
.undraw()
move(dx,dy)
dx
units in the x
direction and dy
units in the y
direction. If the object is currently drawn, the image is adjusted to the new position.clone()
Point(x,y)
getX()
x
coordinate of a point.getY()
y
coordinate of a point.Line(point1, point2)
point1
to point2
.setArrow(string)
arrowhead
status of a line. Arrows may be drawn at either the first point, the last point, or both. Possible values of string
are first
, last
, both
, and none
. The default setting is none
.getCenter()
getP1()
, getP2()
Circle(centerPoint, radius)
getCenter()
getRadius()
getP1()
, getP2()
Rectangle(point1, point2)
point1
and point2
.getCenter()
getP1()
, getP2()
Oval(point1, point2)
point1
and point2
.getCenter()
getP1()
, getP2()
Polygon(point1, point2, point3, ...)
getPoints()
Text(anchorPoint, string)
string
centered at anchorPoint
. The text is displayed horizontally.setText(string)
string
.getText()
string
.getAnchor()
setFace(family)
family
. Possible values are helvetica
, courier
, times roman
, and arial
.setSize(point)
point
size. Sizes from 5 to 36 points are legal.setStyle(style)
style
. Possible values are: normal
, bold
, italic
, and bold italic
.setTextColor(color)
color
. Note: setFill
has the same effect.Objects of Entry
are displayed as text entry boxes that can be edited by the user of the program. Entry
objects support the generic graphics methods move()
, draw(graphwin)
, undraw()
, setFill(color)
, and clone()
. The Entry
specific methods are given below.
Entry(centerPoint, width)
Entry
having the given centerPoint
and width
. The width
is specified in number of characters of text that can be displayed.getAnchor()
getText()
setText(string)
string
. Changes the font face to the given family. Possible values are helvetica
, courier
, times roman
, and arial
.setSize(point)
point
size. Sizes from 5 to 36 points are legal.setStyle(style)
style
. Possible values are: normal
, bold
, italic
, and bold italic
.setTextColor(color)
color
.The graphics module also provides minimal support for displaying and manipulating images in a GraphWin
. Most platforms will support at least PPM and GIF images. Display is done with an Image
object. Images support the generic methods move(dx,dy)
, draw(graphwin)
, undraw()
, and clone()
. Image-specific methods are given below.
Image(anchorPoint, filename)
Image
from contents of the given filename
centered at the given anchorPoint
. Can also be called with width
and height
parameters instead of filename
. In this case, a blank (transparent) image is created of the given width
and height
.getAnchor()
getWidth()
getHeight()
getPixel(x, y)
[red, green, blue]
of the RGB values of the pixel at position (x,y)
. Each value is a number in the range 0-255 indicating the intensity of the corresponding RGB color. These numbers can be turned into a color string using the color_rgb
function (see next section).
Note that pixel position is relative to the image itself, not the window where the image may be drawn. The upper-left corner of the image is always pixel (0,0)
.
setPixel(x, y, color)
(x,y)
to the given color
. Note: this is a slow operation.save(filename)
filename
. The type of the resulting file (e.g., GIF or PPM) is determined by the extension on the filename
. For example, img.save("myPic.ppm")
saves img
as a PPM file.Colors are indicated by strings. Most normal colors such asred
, purple
, green
, cyan
, etc. should be available. Many colors come in various shades, such as red1,red2
,red3
,and red4
, which are increasingly darker shades of red.
To see the full set of colors available to you:
$ python >>> from colorPicker import * >>> colorPicker()
The graphics module also provides a function for mixing your own colors numerically. The function color_rgb(red, green, blue)
will return a string representing a color that is a mixture of the intensities of red, green and blue specified. These should be ints
in the range 0-255. Thus color_rgb(255, 0, 0)
is a bright red, while color_rgb(130, 0, 130)
is a medium magenta.