CS108 Lecture 11: Objects and Graphics Objects and Classes Graphics Concepts Aaron Stevens 9 February 2009 1 Overview/Questions – What are objects, and how do they help us program more effectively? – An overview of computer graphics concepts. – Review: using a third-party library in Python. – How to create and use objects. 2 1
Computing Models Data Processing Model View data as passive entity which we manipulate using active operations (arithmetic, string library, etc.) Object Orientation Define objects which model elements from the real world. Build complex programs by manipulating relatively simple objects. 3 Object Orientation Object – Sort of an active data type, which supports both data and operations. – Objects know stuff (they contain data) and also do stuff (they have operations) Method – In OO parlance, objects interact by sending each other messages , also known as methods . – A method is a request for an object to perform one of its operations. 4 2
Object Orientation Example Consider a banking application, which would use bank account objects. A bank account has some data – account balance, interest rate and some operations – withdraw, deposit 5 Why Graphics? But aren’t we learning about objects? – Graphics is a great way to learn about object orientation concepts. – Graphics programming has a short feedback cycle – you see immediate results of any operations. – Graphics can be a lot of fun. 6 3
Some Graphics Concepts Graphical User Interface Type of program where input/output is done via mouse clicks, windows, dialogs, menus, etc. – We’ll cover GUIs in detail later in the course. Vector Graphics A geometric/algorithmic drawing scheme, which makes drawing quite easy. 7 Some Graphics Concepts Graphics Window A collection of pixels on the screen which we can turn on/off/change colors to create images. Pixels The smallest addressable unit of light on a computer monitor. Pixel is a concatenation of picture element. 8 4
Getting Started We are using Zelle’s graphics library, which is available here: http://mcsp.wartburg.edu/zelle/python/graphics.py The API reference pages are here: http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html 9 Getting Started Put the graphics.py file in the same folder as your examples, or in the Python system folder (e.g. c:/Python25/Lib/ on windows or /Library/Frameworks/Python.framework/Versions/2.5/ lib/python2.5/site-packages/ on Mac. Recall that we can import a library using: import <library> This form will save us some typing later: from <library> import <name> 10 5
Experiment First, Theory Later Try this: >>> from graphics import * >>> win = GraphWin() This statement creates a new window on the screen. The GraphWin is an object, and we have assigned it to the variable win . The window (by default) has a size of 200 pixels tall by 200 pixels wide. That’s 40,000 pixels! 11 Coordinate System We’ll use a library of objects which know how to draw themselves on a GraphWin . In geometry, a point is a location in space, defined by its x and y coordinates. In computer graphics, the origin (0,0) is at the top-left corner of the screen (window). – x-values increase from left to right – y-values increase from top to bottom 12 6
Drawing a Point A Point is an object with x and y coordinates. Like all objects in the graphics library, it knows how to draw itself on a GraphWin . >>> p = Point(50,50) >>> p.draw(win) >>> p2 = Point(100,75) >>> p2.draw(win) 13 Drawing other Shapes In addition to Point the graphics library defines the following object types: – Line – Circle – Rectangle – Oval – Text 14 7
Drawing other Shapes >>> from graphics import * >>> win = GraphWin() >>> p = Point(50,50) >>> p.draw(win) >>> win = GraphWin() >>> center = Point(100,100) >>> circ = Circle(center, 30) >>> circ.setFill("red") >>> circ.draw(win) >>> line = Line(Point(150,50),Point(150,150)) >>> line.draw(win) >>> text = Text(Point(100,20), "Witty remark.") >>> text.draw(win) 15 Some Theory Our example manipulated several different kinds of objects: Point, Line, Circle, Rectangle, Oval, Text These are examples of classes . Every object is an instance of a class , and the class describes the properties (data and behaviors) the object will have. 16 8
Classes and Objects An analogy using cars: Consider a 2002 Honda Civic LX 4-door 5-speed, w/AC. What does this describe, really? Can you drive it? What color is it? What condition is it in? – This actually describes an engineering specification from which an entire class of identical cars could be created. Now consider my wife’s car… 2002 Honda Civic LX 4-door 5-speed, w/AC, green exterior, gray interior, 40500 miles, VIN 1HXYZ123… some minor interior/exterior toddler damage. This describes a single instance of a vehicle -- made to the specification of a 2002 Honda Civic… 17 Creating an instance of a class To create an instance of a class, we must use a special operation called a constructor. The general form for a constructor is: <class-name>(<param1>, <param2>,…) <class-name> is the name of the class of which to create an object instance (i.e. Point ). The number and type of parameters depend on the class. 18 9
Creating an instance of a class Often, the constructor is used as the right side of an assignment statement, so the general form becomes: <var> = <class-name>(<param1>, <param2>,…) Example: p = Point(50,50) 19 Calling Methods To perform an operation on an object, we invoke (call) a method on that object. To invoke a method we use the dot notation: <object>.<method-name>(<param1>, <param2>,…) Example: p.draw(win) 20 10
Calling Methods To invoke a method we use the dot notation: <object>.<method-name>(<param1>, <param2>,…) More Examples: >>> circ.draw(win) >>> circ.setFill("green") >>> circ.move(10,10) 21 A Note About Calling Methods Methods are very finicky about their parameters. When calling a method, be sure to: – Provide the correct number of parameters. – Provide the correct types of parameters. – Provide the correct order of parameters. The API reference page shows you the list of parameters required/expected for a given method. http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html 22 11
Calling Methods Some methods do not take any parameters. These either return data, or else change the state of the object: More Examples: >>> p.getX() >>> p.getY() >>> circ.undraw() 23 Take-Away Points – Pixels, coordinates, etc. – Objects – Classes – Constructor – Method 24 12
Student To Dos – HW04 due Tuesday 2/10 – Quiz 2 is on Friday 2/13 Covers lectures 5-9 (repetition, accumulator pattern, strings, files) – Reading: chapter 5 (graphics) – Download the graphics.py module from http://mcsp.wartburg.edu/zelle/python/graphics.py, and save it in your code folder (with your examples). 25 13
Recommend
More recommend