Object-Oriented Graphics Package Implementation with Python Inheritance

python programming in context n.w
1 / 32
Embed
Share

Explore the concept of inheritance in Python programming to create a versatile object-oriented graphics package. Learn how to design and draw on a canvas using inheritance relationships. Implement a Canvas class with a turtle for drawing functionalities, demonstrating practical examples of object-oriented design principles.

  • Python
  • Inheritance
  • Graphics
  • Object-Oriented Design
  • Canvas

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Python Programming in Context Chapter 12

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

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

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

  5. Figure 12.1

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

  7. Figure 12.2

  8. Figure 12.3

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

  10. Figure 12.4

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

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

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

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

  15. Point X and Y location Inherits color and width Draw makes a dot

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

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

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

  19. Figure 12.5

  20. Figure 12.6

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

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

  23. Figure 12.7

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

  25. 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:

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

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

  28. Figure 12.8

  29. Figure 12.9

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

  31. Figure 12.10

  32. Figure 12.11

Related


More Related Content