CSE 131 Module 10: Class Hierarchies

Studio


Studio 10

Review studio procedures before starting.

Warmup


The goal of this studio is to get acquainted with a design for the BrickBreaker game, and to investigate the use of class extensions to accomplish variation.

Go as far as you can with this, and feel free to experiment. Show your work to TAs as you go and interact with other groups if you have ideas to share or need help.


  1. Right-click Main and choose Run As ... Java Application. Play with the paddle a bit and observe its behavior.

  2. Open the Blob interface and examine the methods defined there. A class implements Blob so that it can describe the space it occupies in the game. There is no visualization associated with Blob. Instead, it is a model for the space occupied by an instance of a class in the game.
  3. As discussed in class, a BoundingBox is a simple approximation of the space occupied by an object. It is also a class that can easily implement Blob. Open BoundingBox and look at its methods.

  4. Look at the MovePublisher interface. It is implemented by an object that can notify other objects (subscribers) when that object moves.
  5. Look at the Mobile interface. It describes an object that is both a Blob and a MovePublisher. A MovePublisher can keep track of any number of MoveSubscribers. When it moves, it notifies each subscriber that it has moved by calling the subscriber's observeMove method.
    Should a MovePublisher use an array or a list to keep track of its subscribers?
  6. The simplest object that brings these concepts together is Piece, which represents an object that might move and that occupies space in the game.

    Open Piece and make sure you understand each of its methods. Ask questions if something is unclear. In particular, examine

    How would you modify setLocation so that it doesn't notify subscribers if the object's new location is the same as its old one?

  7. OK let's look at the code from a different viewpoint. When the code begins, execution starts in Main, which instantiates an instance of Main and a NIP panel in which objects will be visualized.
  8. A Controller is also instantiated, and it is responsible for setting up the visualization and handling the control aspects of the game.
  9. Main handles all of the mouse interactions, and calls Controller to carry out the work on behalf of mouse actions.
    What exactly happens when the mouse moves? Follow the code from Main into Controller to see how mouse motions cause the Paddle to change location.
  10. Modify Main so that mousePressed calls the appropriate method in the Controller to set the Paddle all the way to the left, to the right, or in the middle (your choice).
    Show this to your TA
    .
  11. While Piece models an object that takes space, there is no visualization placed directly in the Piece object. Instead, PieceViz combines a Piece along with a Graphic that represents the Piece in the game. The Graphic can be changed at any time by calling the setGraphic method.
  12. How does the PieceViz know that its Piece has moved, and where is action taken in PieceViz to adjust the visualization?
    If you are uncertain, look again at the move publisher and subscriber interfaces.
  13. Specific types of PieceViz classes can be established by extending PieceViz. Take a look at Boundary, which represents the walls and ceiling of the game. Its constructor uses super to establish its parent class, PieceViz, by providing it a Piece and a GraphicsPanel.
  14. The Controller sets up the display that you see by instantiating four Boundary objects, picking a different color for the floor than the ceiling and walls. It also sets up the Paddle.
  15. Change the Paddle instantiation to SlowPaddle and run the game. Because SlowPaddle extends Paddle, it can be used anywhere a Paddle is expected, but its behavior can extend the behavior of Paddle.
  16. Implement ShyPaddle as another extension of Paddle or of SlowPaddle so that it avoids the cursor (as shown in class).
    A child class can cause its parent's definition of a method foo to execute by calling it as super.foo(...).
    Test this by changing Controller so that it instantiates ShyPaddle in place of Paddle.
  17. Define a Brick class that extends PieceViz like Boundary does, but uses a different color.
  18. Modify Controller so that it places some number of Brick objects in the game. Make sure they show up in the display. If some are missing, it's likely you gave them coordinates that caused them to be hidden. Try to place the Bricks where you want them for the game.
  19. Take a look at KeepPaddleInBounds. How does it detect when the Paddle hits a wall, and what does it do about that? Modify its behavior so that when the paddle hits the wall, something different happens.
  20. How would you modify the game so that two Paddles could be deployed some distance apart from each other?
    Hint: Instead of having the Controller directly a single Paddle, consider having it change the location of a Piece. Each Paddle can be a MoveSubscriber to the Piece. When the Piece moves (in response to the mouse moving), each Paddle is notified and can change its location accordingly, using some offset from the Piece.

Submitting your work (read carefully)



Last modified 19:55:26 CST 18 November 2009 by Ron K. Cytron