Overview/Questions What are objects, and how do they help us - - PDF document

overview questions
SMART_READER_LITE
LIVE PREVIEW

Overview/Questions What are objects, and how do they help us - - PDF document

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


slide-1
SLIDE 1

1

1

Aaron Stevens

9 February 2009

CS108 Lecture 11: Objects and Graphics

Objects and Classes Graphics Concepts

2

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.

slide-2
SLIDE 2

2

3

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.

4

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.

slide-3
SLIDE 3

3

5

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

6

Why Graphics?

But aren’t we learning about objects?

– Graphics is a great way to learn about object

  • rientation concepts.

– Graphics programming has a short feedback cycle – you see immediate results of any

  • perations.

– Graphics can be a lot of fun.

slide-4
SLIDE 4

4

7

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.

8

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.

slide-5
SLIDE 5

5

9

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

10

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>

slide-6
SLIDE 6

6

11

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!

12

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

slide-7
SLIDE 7

7

13

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)

14

Drawing other Shapes

In addition to Point the graphics library defines the following object types:

– Line – Circle – Rectangle – Oval – Text

slide-8
SLIDE 8

8

15

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)

16

Some Theory

Our example manipulated several different kinds

  • f 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.

slide-9
SLIDE 9

9

17

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…

18

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.

slide-10
SLIDE 10

10

19

Creating an instance of a class

Often, the constructor is used as the right side

  • f an assignment statement, so the general form

becomes:

<var> = <class-name>(<param1>, <param2>,…)

Example:

p = Point(50,50)

20

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)

slide-11
SLIDE 11

11

21

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)

22

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

slide-12
SLIDE 12

12

23

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()

24

Take-Away Points

– Pixels, coordinates, etc. – Objects – Classes – Constructor – Method

slide-13
SLIDE 13

13

25

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).