CS51 - Spring 2010 - Lecture 8

  • http://xkcd.com/394/

  • Exercise 9.11.2: Write a program that creates a ResizableBall centered around the point where the mouse is clicked. A ResizableBall is an animated ball that grows up to twice its original size and then shrinks down to half its size before beginning to grow again. It continues to grow and shrink until the program is stopped. The center of the ResizableBall should never move.

       - what type of class will ResizableBall be?
          - ActiveObject
       - What information does it need to know in when it's created (i.e. what does in the constructor)?
          - Location where it is centered (either as two doubles or a Location)
          - it's initial size
          - canvas. Any class that's going to be creating other objects that require canvas, needs to have canvas as a parameter
       - How do we make it grow and shrink indefinitely?
          - need to change the width and height
          - need to recenter the ball
          - need to handle the fact that sometimes we're growing and sometimes we're shrinking

  • quick recap of FallingLeaves demo
       - overall behavior
          - when I click, we get multiple leaves to fall (in this case, 10)
          - each time I click I get 10 more leaves
          - the leaves have:
             - different pictures
             - fall at different speeds
             - fall at different locations
             - and don't all fall together at the same time
       - how many classes?
          - FallingLeaf
          - Fall (extends WindowController)
          - Tree! Used to generate multiple FallingLeaf objects, controlling the timing and generating randomness                  - what did we need to specify for the constructor to create a FallingLeaf?
          - a random images (selected from 2)
          - a random speed
          - a different x   
       - Tree class
          - how can we generate 10 leaves with another class?
             - generate a "random" leaf
             - pause
             - generate another "random" leaf, etc.
          - what information does it need to know?
             - needs to be given some different leaf images
                - loading images via getImage is slow!
                - creating new VisibleImages is fast
                - only call getImage once, and then pass that Image as a parameter to be shown when creating a new VisibleImage
             - needs the screen width so it can generate appropriate random x coordinates
             - needs the screen height so it can pass the height information along to the FallingLeaf class
          - what's going to be in the run() method?
             - pick a random leaf picture
             - pick a random x
             - pick a random speed
             - create the FallingLeaf
             - do this some fixed number of times
          - look at Tree class in FallingLeaves code
          - look at Fall class in FallingLeaves code
             - very simple!

  • look at "code guide" on the class web page

  • look at LaundrySorter assignment
       - how do we drag objects?
          ...
          FilledRect swatch;
          ...

          - access that variable in onMousePress, onMouseDrag and onMouseRelease

  • show ShirtsAndPants demo
       - we've seen the TShirt class
       - we've added a Pants class that is similar to the TShirt class
       - What would we need to change in the code?
          - randomly pick a clothing type in the onMouseRelease method
       - What complication does this add for dragging?
          - we need to declare a variable like above, but what would the type be?
             - we have a TShirt object sometimes and a Pants object sometimes

  • interfaces
       - what defines the functionality of a class, i.e what the class does?
          - the public methods of that class
          - the private methods are only used internally within that class
       - interfaces allow us to define WHAT functionality a class must have, but not HOW the functionality needs to be implemented
       - What functionality (i.e. methods) do we want for either TShirt or Pants?
          - move
          - moveTo
          - contains
          - setColor
          - getColor
          - removeFromCanvas
       - look at Laundry interface in ShirtsAndPants code
          - interfaces are defined similarly to classes, with an interface header and internal method headers, but we do not provide an implementation for any of the methods
          
          public interface MyInterface{
             // public method headers followed by a semicolon
          }
       - To use an interface, a class "implements" that interface.
          - add "implements MyInterface in the class header
          - the class then must implement ALL of the methods defined in the interface
       - Interfaces are a contract for a class, that class must implement the methods
       - look at TShirt and Pants classes
          - they have "implements Laundry" in the class header
          - they define all of the methods required by the interface
          - notice if we remove one of the required methods (e.g. by commenting it out), then the compiler will complain because we have not fulfilled our contractual obligation
       - So, what good is an interface?
          - what do we know about any object that implements an interface?
             - we know that it has all of the methods declared
       - When you define an interface, just like defining a new class, you are defining a new type
          - for example, we can define a variable to be of that type:
             private Laundry clothing;
          - and then assign it any object that implements that interface
          - NOTE: an interface is not a class. An interface defines a new type and what functionality that type has, but does not provide any implementation. You cannot say "new Laundry(..)".
       - look at LaundrySorter class in ShirtsAndPants code
          - private Laundry item;
          - in begin()
             - item = new TShirt(...)
                - we can do this because it implements the Laundry interface
          - in onMouseRelease()
             - notice that we generate a random piece of clothing, either a TShirt or Pants and can assign either to our variable
             - for our variable item, we can use ANY functionality defined by the interface
                - moveTo
                - contains
                - setColor
                - removeFromCanvas
             - why does this work?
                - because we defined an interface, we know that all of these methods must be defined

  • DrawableInterface
       - Knowing what you know now, do you think it's coincidence that all of the things we've seen so far have shared methods, like
          - move
          - moveTo
          - setColor
          - getX
       - Most of the classes inherit from DrawableInterface
       - look at the DrawableInterface interface declaration in the "Complete Objectdraw Library documentation" on the course web page