GraphWin Objects

A GraphWin object represents a window on the screen where graphical images may be drawn. A program may define any number of GraphWins. A GraphWin understands the following methods:
GraphWin(title, width, height)
Constructs a new graphics window for drawing on the screen. The parameters are optional; the default title is “Graphics Window,” and the default size is 200 x 200 pixels.


Example: win = GraphWin("Investment Growth", 640, 480)

plot(x, y, color)
Draws the pixel at $(x,y)$ in the window. Color is optional; black is the default.


Example: win.plot(35, 128, "blue")

plotPixel(x, y, color)
Draws the pixel at the “raw” position $(x,y)$, ignoring any coordinate transformations set up by setCoords.


Example: win.plotPixel(35, 128, "blue")

setBackground(color)
Sets the window background to the given color. The default background color depends on your system. See Section 4.8.5 for information on specifying colors.


Example: win.setBackground("white")

close()
Closes the on-screen window.


Example: win.close()

getMouse()
Pauses for the user to click a mouse in the window and returns where the mouse was clicked as a Point object.


Example: clickPoint = win.getMouse()

checkMouse()
Similar to getMouse, but does not pause for a user click. Returns the last point where the mouse was clicked or None if the window has not been clicked since the previous call to checkMouse or getMouse. This is particularly useful for controlling animation loops (see Chapter 8).


Example: clickPoint = win.checkMouse()
Note: clickPoint may be None.

getKey()
Pauses for the user to type a key on the keyboard and returns a string representing the key that was pressed.


Example: keyString = win.getKey()

checkKey()
Similar to getKey, but does not pause for the user to press a key. Returns the last key that was pressed or "" if no key was pressed since the previous call to checkKey or getKey. This is particularly useful for controlling simple animation loops (see Chapter 8).


Example: keyString = win.checkKey()
Note: keyString may be the empty string ""

setCoords(xll, yll, xur, yur)
Sets the coordinate system of the window. The lower-left corner is $(xll, yll)$ and the upper-right corner is $(xur, yur)$. Currently drawn objects are redrawn and subsequent drawing is done with respect to the new coordinate system (except for plotPixel).


Example: win.setCoords(0, 0, 200, 100)