Graphics Shapes Setup for using graphics • You have to import the graphics library • You can use either “import graphics” or “from graphics import *” or “from graphics import GraphWin, Point, Circle” • If you use “import graphics” you then refer to all the functions from the library as “win = graphics.GraphWin()” or “p = graphics.Point(5,5)” • If you use one of the other imports, you can omit the “graphics.” Creating a Graphics Window to draw on • Typically the first thing done • win = GraphWin() • This is a call to a constructor to make a GraphWin object. It has no arguments but it still needs the parentheses to make Python see it as a function call • The object that is returned from the constructor is stored in the variable win • There are optional arguments: first argument is the title of the window (in the title bar), the 2nd and 3rd arguments are the width and height in pixels (a pixel is the smallest location on the screen which can be turned ‘on’ or ‘off’) Coordinate system • The graphic window uses a coordinate system like the Cartesian plane • It uses two numbers, x and y, to refer to a location on the window • The origin of the system by default, is the upper left corner of the graphics window. Note that this makes the y coordinates behave differently than you are used to in algebra. • Y coordinates start at 0 at the TOP of the window and increase as the location moves DOWN the window. • X coordinates behave normally, 0 at the left and increases as you move right on the window. • Yes, there is a way to change this if you want. Line class • What is needed to specify a line? The easiest way is with 2 points • line1 = Line(Point(100, 25), Point(17, 92)) • This creates an object called line1, which is actually a line segment drawn from one point to the other point given • When you are ready to display the line on the graphics window, you use the statement line.draw(win) This assumes that win is a GraphWin which has already been set up Circle class • What information does the computer need to draw a circle? A center point and a radius (distance from center to edge of circle) • my_eye = Circle(Point(100, 150), 25) • This creates an object called my_eye which is a circle. It has two arguments; first one is a Point object, second one is a number (can be integer or float) • This does NOT make the circle appear on the graphics window, you have to call the draw method my_eye.draw(win) • You can do other things with the Circle object before you draw it, like setFill (to change color), setWidth (to make the line thicker), etc. Rectangle class • What is needed to specify a rectangle? There are several ways, but the easiest is to give two Points. One is considered the upper left corner and the other is the lower right corner. Which one is which? Does not matter, the library figures it out. • rect = Rectangle(Point(100, 300), Point(300, 250)) • This will draw a rectangle that is 200 pixels wide and 50 pixels tall (300-100) wide, (300-250) tall • Just like the Circle class, this can be colorized, drawn on the graphics window, etc. Polygon class • A polygon is a generic shape. Its sides can be any length at any angle. How would you specify a shape like that? By giving all the points where the sides connect to each other. poly = Polygon(Point(100, 300), Point(300, 25), Point(70, 92)) • This will draw a triangle. It draws a line segment from the first point to the second, from the second to the third and so on, and then a line segment from the last point to the first. • Obviously the order of the points in the arguments matters! Colors • The colors available in this library are the ones available in the Tkinter library. There is a list of the color names with their colors here http://wiki.tcl.tk/37701 • These can be used with setFill() to color the inside of a shape • And with setOutline() to color the line which draws the shape Ending a graphics program • Please do these things! If you forget them, you can eventually lock up your IDE and sometimes even your operating system! • At the end of the graphics part of the program, you want two lines win.getMouse() win.close() • This assumes you have a graphics window called win. • It waits for the user to click somewhere in the window, then closes the window.