Python Programming in Context Chapter 12. Objectives To introduce the concept of inheritance To create a working object-oriented graphics package To provide.

Slides:



Advertisements
Similar presentations
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Advertisements

Chapter 14 Graph class design John Keyser’s Modifications of Slides by Bjarne Stroustrup
PYTHON TURTLE WORLD : CHAPTER 4 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.
Unified Modeling Language
Object Oriented Programming: Inheritance Chapter 9.
Waterfront House Reflections. On the white paper, draw 2 horizontal lines. Below these lines, draw ripples for water.
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
Inheritance: Section 9.1, 9.2 Victor Norman CS106.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
Python Programming in Context Chapter 9. Objectives To introduce functional programming style To practice writing recursive functions To introduce grammars.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Adobe Illustrator CS? Is a graphic design software - it is the industry's premier vector- drawing environment for creating graphics that scale across media.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
Inheritance CS 2302 SPRING Inheritance In this part we introduce a new relationship between classes: inheritance. This is the fundamental feature.
Python Programming in Context
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Python Programming in Context Chapter 13. Objectives To write an event driven program To understand and write callback functions To practice with lists.
If..else Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed.
Geometry: Area By Mrs. Hafer. What is area? Area tells the size of a shape or figure. It tells us the size of squares, rectangles, circles, triangles,
Learning outcomes ALL MUST be able to draw simple rectangles and diagonals. MOST SHOULD be able to record in table form a set of results to identify relationships.
Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming.
Chapter 7 Vocab Review. 1. Write the generic formula (proportion) for geometric mean (x) of two positive numbers a & b.
Distribute and Combine Like Terms Applications. What is the area of the following shape? 5 2x-3 1.
Some Graphics CS303E: Elements of Computers and Programming.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Classes & Inheritance Review
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)
Histograms, Frequency Polygons, and Ogives
Python – May 19 Review –What is the difference between: list, tuple, set, dictionary? –When is it appropriate to use each? Creating our own data types:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
BELL RINGER (THINK, PAIR, SHARE) 1. List as many properties as you can about the sides, angles, and diagonals of a square and a rectangle.
Python Programming in Context Chapter 11. Objectives To explore classes and objects further To design a large multiclass application To implement a graphical.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
7-1C Areas and Perimeters of Similar Polygons What is the relationship between the perimeter ratios of similar figures and their similarity ratio? What.
MENTAL MATH DRAW THAT SHAPE In this timed test you will have to DRAW some geometric figures. Some may be able to be drawn two or three different ways.
9.1 PERIMETER AND AREA OF PARALLELOGRAMS Objective: Students find the perimeter and area of parallelograms.
Class Inheritance Victor Norman CS104. The Problem What is the basic problem that inheritance tries to solve? Answer: Two types (classes) may have lots.
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program.
Adapted from slides by Marty Stepp and Stuart Reges
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
9.1 Lines and Angles.
User Defined Classes CS F.
CSc 110, Autumn 2016 Lecture 31: Encapsulation
Pre-AP® Computer Science Quiz Key
Pre-AP® Computer Science Quiz
Using the coords function (Coordinates)
1-5 Geometric Formulas Polygons
Adding a paddle (1) We now have a code, which will run forever and just have a ball bouncing around the screen until we shut down the program. To make.
Simple Graphics Package
Inheritance in Graphics
Conrad Huang Genentech Hall, N453A x6-0415
PreAP Computer Science Quiz
Millennium High School Agenda Calendar
Terry Scott University of Northern Colorado 2007 Prentice Hall
Using Modules.
2D Shapes Rectangle Circle Triangle Rectangle. What shape is the door? Rectangle.
Presentation transcript:

Python Programming in Context Chapter 12

Objectives To introduce the concept of inheritance To create a working object-oriented graphics package To provide another example of object- oriented design

Goal This is what we want to be able to do Draw on a canvas

Listing 12.1 def drawHouse(): myCanvas = Canvas(800,600) house = Rectangle(Point(-100,-100),Point(100,100)) house.setFill('blue') door = Rectangle(Point(-50,-100),Point(0,75)) door.setFill('brown') roof1 = Line(Point(-100,100),Point(0,200)) roof2 = Line(Point(0,200),Point(100,100)) roof1.setWidth(3) roof2.setWidth(3) myCanvas.draw(house) myCanvas.draw(door) myCanvas.draw(roof1) myCanvas.draw(roof2) sun = Circle(Point(-150,250),20) sun.setFill('yellow') myCanvas.draw(sun) for i in range(-150,150): # move the sun across the sky sun.move(1,0)

Figure 12.1

Inheritance Hierarchy IS-A Relationship – One object is a more specific instance of another HAS-A Relationship – One object uses another object Examples – A rectangle is-a polygon – A square is-a rectangle

Figure 12.2

Figure 12.3

Listing 12.2 myCanvas = Canvas(800,600) myLine = Line(Point(-100,- 100),Point(100,100)) myCanvas.draw(myLine)

Figure 12.4

Implementing the Canvas Class A canvas will have a width and a height A turtle will do all the drawing

Listing 12.3 class Canvas: def __init__(self,w,h): self.turtle = turtle.Turtle() self.screen = turtle.Screen() self.width = w self.height = h self.screen.setup(width=self.width,height=self.height) self.turtle.hideturtle() def draw(self,gObject): self.turtle.up() self.screen.tracer(0) gObject._draw(self.turtle) self.screen.tracer(1)

Geometric Object Line color Line width Draw – Geometric objects don’t really know how to draw themselves – Subclasses will know how to draw themselves – Call the _draw method from each object

Listing 12.4 class GeometricObject: def __init__(self): self.lineColor = 'black' self.lineWidth = 1 def getColor(self): return self.lineColor def getWidth(self): return self.lineWidth def setColor(self,color): self.lineColor = color def setWidth(self,width): self.lineWidth = width def _draw(self,someturtle): print("Error: You must define _draw in subclass")

Point X and Y location Inherits color and width Draw makes a “dot”

Listing 12.5 class Point(GeometricObject): def __init__(self, x,y): super().__init__() self.x = x self.y = y def getCoord(self): return (self.x,self.y) def getX(self): return self.x def getY(self): return self.y def _draw(self,aturtle): aturtle.goto(self.x,self.y) aturtle.dot(self.lineWidth,self.lineColor)

Line Has two points Color and width are inherited Draws a line between by placing turtle on one point and moving to the other

Listing 12.6 class Line(GeometricObject): def __init__(self, p1,p2): super().__init__() self.p1 = p1 self.p2 = p2 def getP1(self): return self.p1 def getP2(self): return self.p2 def _draw(self,aturtle): aturtle.color(self.getColor()) aturtle.width(self.getWidth()) aturtle.goto(self.p1.getCoord()) aturtle.down() aturtle.goto(self.p2.getCoord())

Figure 12.5

Figure 12.6

Redesign Current design works well The following example shows a problem – Changing the color of the line after the line has been drawn – New color will not be shown – Need to redraw the line – But, then it will be out of place with respect to all the other objects – Need to redraw ALL OF THEM – Need to keep track of all object on canvas in the order they were created

Listing 12.7 def test2(): myCanvas = Canvas(500,500) myLine = Line(Point(-100,-100),Point(100,100)) myOtherLine = Line(Point(-100,100),Point(100,-100)) myLine.setWidth(4) myOtherLine.setWidth(4) myCanvas.draw(myLine) myCanvas.draw(myOtherLine) myLine.setColor('red')

Figure 12.7

Listing 12.8 class Canvas: def __init__(self,w,h): self.width = w self.height = h self.visibleObjects = [] self.turtle = turtle.Turtle() self.screen = turtle.Screen() self.screen.setup(width=self.width,height=self.height) self.turtle.hideturtle() def drawAll(self): self.screen.reset() self.screen.tracer(0) for shape in self.visibleObjects: shape._draw(self.turtle) self.screen.tracer(1) self.turtle.hideturtle() def addShape(self,shape): self.visibleObjects.append(shape) def draw(self,gObject): gObject.setCanvas(self) gObject.setVisible(True) self.turtle.up() self.screen.tracer(0) gObject._draw(self.turtle) self.screen.tracer(1) self.addShape(gObject)

Listing 12.9 (Part 1) class GeometricObject: def __init__(self): self.lineColor = 'black' self.lineWidth = 1 self.visible = False self.myCanvas = None def setColor(self,color): self.lineColor = color if self.visible: self.myCanvas.drawAll() def setWidth(self,width): self.lineWidth = width if self.visible:

Listing 12.9 (Part 2) self.myCanvas.drawAll() def getColor(self): return self.lineColor def getWidth(self): return self.lineWidth def _draw(self): print ("Error: You must define _draw in subclass") def setVisible(self,vFlag): self.visible = vFlag def setCanvas(self,theCanvas): self.myCanvas = theCanvas

Polygon Polygons are defined by a list of points Draw the polygon by moving the turtle from point to point Connect the last point back to the first

Figure 12.8

Figure 12.9

Rectangle Needs two opposing diagonal points Can compute the other two points Draw as polygon

Figure 12.10

Figure 12.11