CS51 - Spring 2010 - Lecture 9

  • http://xkcd.com/303/

  • Exercise 10.5.2
    public interface Alphabet{
       public void x( Color aColor );
       public double y( int number );
       public boolean z( Location point );
    }

    Class A implements Alphabet:
    methods: public void x( Color aColor );
       public double y( int number );
       public boolean z( Location point );
       public String w();

    Class B implements Alphabet:
    methods: public void x( Color aColor );
       public double y( int number );
       public boolean z( Location point );
       public String w();
       public void v (boolean b);

    a. private Alphabet letter = new A();
    b. private A letter = new A();
    c. private A letter = new Alphabet();
    d. private Alphabet letter = new B();
    e. B letter = new B();
    letter.v(true);
    f. Alphabet letter = new B();
    letter.v(true);
    g. Alphabet letter = new A();
    letter.w();
    h. A letter = new A();
    letter.w();
    i. Alphabet letter = new A();
    letter.x( Color.RED );

  • printing to the console
       - traditional programs often don't output results to a new window
       - instead, the interface with the user through the console (or command-line)
       - doing this is straightforward, but we can only print text
       - to print text to the console, use
          System.out.println(...)
       - what type of method is this?
          - static method (note we don't have to instantiate an object)
       - The simplest, is just to output a string:
          System.out.println("Here I am!");
       - You can also print variable values:
          double num = 10.0;
          System.out.println(num);
       - And can combine the two:
          double num = 10.0;
          System.out.println("The number is: " + num);
       - Note that what is really happening is that the double is getting converted to it's String representation
       - You can also print out all objects
          Location loc = new Location(10, 10);
          System.out.println("My location is: " + loc);

          - again, we're getting a String representation of the object
          - Note, however, that not all classes have a good String representation and so you may not get much information. For example

          FramedRect rect = new FramedRect(...);
          System.out.println(rect);
       - This can be very useful for debugging!

  • basic debugging
       - simplest way, System.out.println()
       - debug functional components!!!
          - if you program one method, test out that one method by itself and make sure it works
             - you can even test parts of a method by commenting things out to make sure a sub-part is working
          - when you've tested all of the methods individually in a class, then test the entire class
          - save you a lot of time debugging
          - don't just code everything up and then try and debug the whole thing!

  • responding to keyboard input
       - KeyListener interface
          - public void keyPressed(KeyEvent e);
          - public void keyTyped(KeyEvent e);
          - public void keyReleasted(KeyEvent e);
       - Similar to our onMousePress, etc methods but for the keyboard
       - When are these methods called?
          - a press is any time a key goes down
          - a release is any time a key come up
          - a type is a higher level operation and only happens when an actual character is type
       - show KeyDemo code
          - import java.awt.event.*;
          - "implements KeyListener"
          - must define all of the methods required by the interface!
             - you may not use all of them, but you still need to define them
             - leave them empty (i.e. no code in the body of the method) if you're not going to use them
          - System.out.println(...) prints information to the console
          - the KeyEvent variable passed in has information about the key that was pressed
             - each key on the keyboard has it's own unique int code, obtained from the getKeyCode() method
                - if you ever want to get this code, you can just run this program and see what that number is
             - you can also get the character press from the getKeyChar() method, though for somethings, like "shift" or the arrow keys, you'll get a '?'
          - in the begin method, you need to do a few things to tell the world that this class wants to be told about keyboard events
             - when any key events happen, in either canvas or our window, we want to know about it

                canvas.addKeyListener(this);
                this.addKeyListener(this);
             
                - this adds us to the set of listeners for these objects
                - what do you think is the expected parameter type for addKeyListener?
                   - KeyListener (which is why we needed to implement the interface)
             - also need to do the following:
                setFocusable(true);
       - show KeyDemo demo
          - notice that all three methods are called when I push down on a key (for most characters)
          - if I press "shift" and release I don't get a key typed, but I get a pressed and released
          - if I hold a key down, I get repeated calls
          - most of the time, we'll just want to use keyPressed

  • show BouncingBasketBall demo
       - dealing with audio clips
          - similar to images, two steps:
             - Define a variable: AudioClip bounce;
             - load the audio clip: bounce = getAudio("hit.au");
             - play the audio clip: bounce.play()
          - as with images, just load (call getAudio(...)) once!
       - two classes:
          - one looks a lot like our other basketball programs however, now, we're using images and we pick up the ball when we move over an image
          - second, is a class that dribbles the ball and extends ActiveObject
             - what does this class do?
                - all it does is dribble the ball
                - How do you dribble a ball?
                   - move it down then up and repeat
             - what information does it need to know to dribble the ball?
                - the VisibleImage of the ball
                - the AudioClip for the bounce sound
             - what other functionality might we need?
                - need to know when to stop dribbling the ball
                - how can we communicate this?
                   - need to create a method
                   - method sets a variable telling the dribbler that we're done bouncing the ball

  • Object oriented (OO) design
       - decompose your problem into objects
       - designing objects
          - designing the public part of the class
             - what information do I need to know to create the object? (this needs to go in to the constructor)
             - what functionality does the object need to support? (these are the methods)
             - how will the different object communicate?
                - after thinking about this, you may have to revisit your design
          - the implementation details
             - What state will it need to keep track of?
                - constants
                - instance variables
       - why OO design?
          - encapsulation: details that are not important to external users are hidden within a class
          - abstraction: allow us to think about higher-level things rather than the low-level details (I want to move this object, I don't want to worry about how that happens)
             - how do computers work?
                - because of encapsulation, you don't need to know, but you can still use them
          - complexity: by breaking it into objects, you only need to worry about one small piece at a time

  • Frogger
       - read the entire description before doing anything!
       - one of the most challenging labs, so we're giving you an extra day
       - Let's roughly map out the classes/design for Frogger
          - What are the classes?
             - Frogger extends WindowController
             - Frog
             - Vehicle extends ActiveObject
             - Lanes (the road)
          - Frog class:
             - What is the functionality/behavior?
                - move (hopToward)
                - be killed
                - be reincarnated
             - What state will it need to keep track of?
                - whether it's alive
                - VisibleImage
             - What information does it need to be constructed?
                - image
                - starting location
                - how much to jump each time
             - Look at Frog.java starter code
          - Vehicle class
             - What is the functionality/behavior?
                - active object
                - run method that moves the car along
                - kill the frog
                   - the Frog and Vehicle classes will need to communicate
                      - this is similar to the Box and the Ball
                   - each vehicle will need to know about the frog
                   - what questions/actions will it then need to make?
                      - do I overlap with the frog (need to add this to our Frog class)
                      - if so, then kill the frog
             - What state will it need to keep track of?
                - image
                - speed
             - What information does it need to be constructed?
                - image
                - speed (to be consistent with other cars)
                - location

          - Lanes class
             - What is the functionality/behavior?
                - generate the vehicles
                   - similar to the Tree class in the FallingLeaves example
                   - this time, though, just keep generating them at a constant rate
                - active object
             - What state will it need to keep track of?
                - speed of the cars in its lane (note they'll all be the same speed)
                - location
                - image
             - What information does it need to be constructed?
                - location
                - screen width
                - speed
                - image
                - ?
          - Frogger class:
             - What is the functionality/behavior?
                - create the background
                - create the frog
                - create the lanes
                - respond to mouse events appropriately
       - design process is iterative and sometimes done at different levels of detail
          - once we have the high-level definitions of classes, we can go back and start to add more detail. For example, define what methods we need

  • GUIs
       - Graphical User Interface
       - things like buttons, drop down menus, pull down menus, text fields, scroll bars
       - Three basic groups of things for doing GUIs in Java:
          - GUI components: these are the actual things (like buttons, text fields, sliders, etc.) that we want to put in our program
          - Containers: containers actually house the GUI components. Unlike the other objects we've seen so far, GUI components are NOT displayed when they're created, but need to be added to a container.
          - Layout managers: Unlike adding things to DrawingCanvas, we don't add GUI objects with positional information. Instead, all containers have a layout manager associated with them that describe how the GUI components that are added to the container will be positioned.
             - Why do you think we do this instead of using exact positioning?

  • WindowController
       - WindowController is a Container and so if you extend WindowController, then you can add GUI components to it
       - The layout manager for WindowController is called BorderLayout
          - you can add components to the North, South, East, West and Center
          - when you add a component, it fills up the entire space
       - For WindowController, the canvas object sits in the Center, so in general, don't add anything there
          - extend Controller if you don't need the canvas and want to add components to the center

  • show ComboBoxDrawingProgram demo