Lecture 9 - Inheritance


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab9
$ cd ~/cs170/labs/lab9

Inheritance

You have now seen the How and the Why of inheritance. Today, you will see the when. Let's talk about how objects are related, and how we use that information to determine attributes and inheritance.


Lab Activity 1
Decorators

One of the biggest uses of inheritance in productions environments is to create what are known as "Decorators" for elements that are being created. We can use "abstract" decorators to describe different ways that we can decorate certain elements.

In this silly, directed example, you are going to define decorators for shape objects that are currently defined in the current_shapes.py file.

Details

In the current_shapes.py file, you will see three different shapes: Square, Circle, and Triangle. You should create at least two classes:   FlashDecorator (which cycles the color of the objects between at least two colors) and GrowShrinkDecorator (which grows the object to a specified size, then shrinks it to zero).

Make the Square, Circle, and Triangle inherit the decorators. Modify the bodies of the classes so that they get decorated when they get drawn in the current main loop.

Hint

  • Your Decorator classes should both have update methods, which update the attributes necessary for each attribute.

  • Your FlashDecorator class needs at least two attributes: color and a color list. You might need additional attributes to help facilitate cycling the colors.

  • Your GrowAndShrinkDecorator class needs at least two attributes: the current size and the maximum size. You may need additional attributes to facilitate growing and shrinking.

  • You need to update the update method of your shapes to call the decorator class update methods. That should be it, as far as getting the decorators to work.

 

Challenge

My decorators are kind of boring. Create your own, more interesting decorator. It does not necessarily have to follow the same format as the above decorators, but it does need to provide some modification to the shapes drawn.


Lab Assignment 9
Photo Frame

Alright, now for a more fun example of inheritance. We can essentially create our own version of some classes that are currently defined in Python. So, you are going to override the built in tk.Canvas object, so that you can create a digital photo frame.

Details

Create a class called PhotoFrame that inherits the tk.Canvas class. You are going to need to add at least one attribute to the class, which will be the image you want displayed at the center of the canvas. You will definitely need to create a couple of methods, to make sure you cannot draw on top of the image. Take a look at the methods on the canvas, to see which methods you need to change the behavior of. Notice that this is not overriding the create_oval method of the Canvas, just providing a more simple version that can be used to create ovals.

  def photo_create_oval(self, x1, y1, x2, y2, color):
      super().create_oval(x1, y1, x2, y2, fill=color)

Find a .gif file (either make one in GIMP, or download one from the Internet), and load the image into your canvas. Decorate your frame using some of the built in object creations you just wrote.

Hint

  • You will need to use the tk.PhotoImage class in order to load your image onto the canvas. This is a pretty straight forward process:

          my_image = tk.PhotoImage(file="image.gif")
          canvas.create_image(0, 0, image=my_image)
          
  • The only methods you should need to create in your PhotoFrame class should be the methods for creating the objects (create_oval, create_line, etc.).

  • For every create method except for draw line, you will want to check to make sure the bounding box of the shape does not overlap the image being drawn to the canvas. Luckily, you have done this before in Pong, and the check is very similar.

    The line intersection is a little bit trickier. You need to see if the line specified intersects any of the lines of the rectangle surrounding the image.

 

Challenge

Even with all the changes you made to the canvas class here, it still looks kind of rough as far as digitial photo frames go. Usually digital photo frames rotate through a set of images, instead of having a single static image. Update your PhotoFrame class so that after some amount of time, the image displayed to the screen changes to some other image.


Submission

When you have finished, create a tar file of your lab9 directory. To create a tar file, execute the following commands:

cd ~/cs170/labs
tar czvf lab9.tgz lab9/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 9. Make sure you include a header listing the authors of the file.


In-class Notes


Last modified: Fri Jan 31 09:11:31 EST 2014