1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics

Download 1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics

Post on 15-Jan-2016

214 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>*Python Programming: An Introduction to Computer ScienceChapter 3Objects and Graphics</p></li><li><p>*Object Oriented ProgrammingThe classic point of view of a programmer was that the program instructions were the active part, the data just sat there and was manipulated. A new paradigm became popular in the 80s: OOPThe idea is that programs should be considered as interactions of objects. An object was a structure that knew things (i.e. had data values) and did things (i.e. had operations to do on the data) (Theres quite a bit more to the paradigm than that, but that will do for now.)</p></li><li><p>*OOP (cont)Modern computer programs are built using an object-oriented approach.Most applications youre familiar with have Graphical User Interfaces (GUI) that provide windows, icons, buttons and menus. These were provided as classes to the programmer they did NOT have to write everything from scratch to make an interface. This is why people like OOP the details can be ignored, the objects do things for you!</p></li><li><p>*Graphics LibraryA commonly used graphics library for Python is named Tkinter. This is not a simple package to use!The author of this book (Zelle) wrote a library on top of Tkinter, to make it easier to use. Its called graphics.pyFirst check to see if you have it alreadyC:\Python32\Lib\site-packages\graphics.pyIf you dont, go to Zelles site and get itGraphics Module Reference:</p></li><li><p>Python Programming, 2/e*Graphics LibraryThis chapter uses the graphics.py library supplied with the supplemental materials.Two locations you can put the fileIn Pythons Lib\site-packages directory with other librariesIn the same folder as your graphics program</p></li><li><p>*Objects in GraphicsThe graphics library is set up as a collection of object templates Point, Circle, Rectangle, Line and so onThese templates are called classes. They are the pattern for what a particular object should be. Example: a Point class would have data about the position of the point (x, y) and functions that could do things with that information</p></li><li><p>*Objects in GraphicsYou call a function called a constructor to make a new object from the class. Constructor methods are almost always the same name as the name of the class. (Objects are also called instances.)Once you have an object constructed, it has a name. You can give the object commands by calling the methods (functions) that it knows about.</p></li><li><p>*Objects in GraphicsExample:p = Point(200, 300) # p is now an object from class Pointp.draw(win)p.move(0, 5)</p><p>Note the syntax: dot notation. The statement or expression starts with the object name, followed by a dot, then the name of the method and its arguments, if any.</p></li><li><p>Setup for using graphicsYou have to import the graphics libraryYou can use either import graphics or from graphics import * or from graphics import GraphWin, Point, CircleIf you use import graphics you then refer to all the functions from the library as win = graphics.GraphWin() If you use one of the other imports, you can omit the graphics.</p></li><li><p>GraphWin ObjectsAGraphWinobject represents a window on the screen where graphical images may be drawn. A program may define any number ofGraphWins.</p></li><li><p>Creating a Graphics Window to draw onTypically the first thing donewin = 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 callThe object that is returned from the constructor is stored in the variable win</p></li><li><p>Creating a Graphics Window to draw onGraphWin(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.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)</p></li><li><p>Coordinate systemThe graphic window uses a coordinate systemIt uses two numbers, x and y, to refer to a location on the windowThe 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.</p></li><li><p>Coordinate systemY 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.</p></li><li><p>What methods you can use?Suppose right now you have created a graphic window.</p><p>win = GraphWin()</p><p>What method can you use? </p></li><li><p>Coordinate systemsetCoords(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). All subsequent drawing will be done with respect to the altered coordinate systeme.g. win.setCoords(0, 0, 20,20)</p></li><li><p>Ending a graphics programPlease 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 lineswin.getMouse()win.close()</p></li><li><p>Ending a graphics program</p><p>This assumes you have a graphics window called win.It waits for the user to click somewhere in the window, then closes the window.</p></li><li><p>Ending a graphics programgetMouse():Pauses for the user to click a mouse in the window and returns where the mouse was clicked as aPointobject.close(): Closes the on-screen window.Again: win.getMouse(), win.close()</p></li><li><p>Point ClassPoint(x,y) : Constructs a point having the given coordinates.The two methods that can be used by the instance of Point classgetX()Returns thexcoordinate of a point.getY()Returns theycoordinate of a point.</p></li><li><p>Line classWhat is needed to specify a line? The easiest way is with 2 pointsline1 = 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</p></li><li><p>Line classWhen you are ready to display the line on the graphics window, you use the statementline.draw(win)This assumes that win is a GraphWin which has already been set up</p></li><li><p>Circle classWhat 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)</p></li><li><p>Circle classThis 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.</p></li><li><p>Rectangle classWhat 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.</p></li><li><p>Rectangle classrect = 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) tallJust like the Circle class, this can be colorized, drawn on the graphics window, etc.</p></li><li><p>Polygon classA 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.</p></li><li><p>Polygon classpoly = 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!</p></li><li><p>ColorsThe 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/37701These can be used with setFill() to color the inside of a shape And with setOutline() to color the line which draws the shape</p><p>Python Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/ePython Programming, 1/e*Python Programming, 1/e</p></li></ul>

Recommended

View more >