CS62 - Spring 2010 - Lecture 4

  • Exercise 3.15 - An m x n matrix could be implemented using a single Vector with mn locations. Assuming that this choice was made, implement the get method. What are the advantages and disadvantages of this implementation over the Vector of Vectors approach?

  • show MyGraphicsDemo code
       - we'll need to import a number of different packages
          - java.awt.*
          - java.awt.geom.*
          - javax.swing.*
       - use constants!

  • extends JFrame (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html)
       - A Frame is a top-level window with a title and a border
       - new JFrame(String title)
       - setSize(int width, int height) // sets the size of the frame (width
       - setVisible(boolean visible)
          - makes the frame visible (or hidden)
          - similar to show() or hide()

  • constructor
       - What does super(title) do?
       - graphic objects are not displayed when they are created. They must be drawn in the paint() method using the Graphics object.
       - note if we comment out the paint method, and run the program, nothing happens
       - For most of the graphics objects there is both a float version and a double version
          - Line2D.Double
          - Line2D.Float
          - The only differences is whether the coordinates are specified as floats or doubles
       - Graphic objects in java are layed out based on an x and y coordinate system starting at the top left corner of the window
          - positive x is right
          - positive y is down
       - See the JavaDoc online for details for these classes
          - Line2D.Double
          - Ellipse2D.Double
          - Rectangle.Double

  • paint(Graphics g) method
       - the key method for drawing graphics
          - invoked implicitly whenever another object calls this frame‚Äôs repaint() method or whenever the screen manager requires it, e.g. when a portion of this frame is revealed by the movement of another frame
          - This is why in CS51 using the objectdraw library, all of our objects had the additional "canvas" parameter. The drawing was being done in the paint() method for canvas.
       - The Graphics object passed in gives us a handle on an object to actually display on the screen
          - First thing to do is typecast the Graphics object as a Graphics2D object
          - Until we "draw" an object using the Graphics2D class, it won't be displayed
             - draw - to draw any Shape object we've created (like a line, rectangle or ellipse)
             - drawString - For putting text on the screen
             - fill - fills the interior of the shape
          - Attributes like line color, fill color and font are NOT set at the Shape object level, but are set on the Graphics2D object before it is used to draw the Shape object


  • interfaces
       - how do we define an interface and what goes in them?
          public interface InterfaceName{
             public void method1();
             public int method2(int number);
             public ArrayList<String> anotherMethod(String myString);
          }

          - only provide the headers of methods
       - how are they used?
          - to "implement" an interface, we add "implements InterfaceName" after the class name in the class header
          - we must then implement ALL of the methods described in the interface, or our code will not compile
       - why are they used?
          - another form of inheritance
             - why is inheritance useful?
                - allows us to reuse code
                - allows us to group together objects that perform similar operations and use them in similar contexts
          - allows us to group objects together with similar functionality
             - public void example(InterfaceName obj){...}
             - we know that we can call any methods defined in the interface from that object
          - we can say, the only thing that's important to me is that the class implement method x, y and z


  • Handling mouse events
       - Two commonly used interfaces:
          - MouseListener (http://java.sun.com/j2se/1.3/docs/api/java/awt/event/MouseListener.html)
             - for taking actions based on mouse presses, releases, clicks, enters and exits
          - MouseMotionListener (http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/MouseMotionListener.html)
             - for taking actions based on mouse moves or drags
             - moving/dragging is continuous in the real world. How does this translate to method calls?

       
  • show MouseEvents code
       - beginning code in main is the generic code to setup a frame
       - private classes
          - you can only have ONE public class in a file
          - however, you can have private helper classes
             - we'll see these frequently in this class
             - again, useful for compartmentalizing functionality
       - the MouseEventsHelper class implements both the MouseListener interface and MouseMotionListener class
          - notice that if you comment out one of the methods, the compiler will complain, because you have not satisfied your contractual obligation of the interface
       - each method is passed a MouseEvent object giving some information about the mouse event that triggered/called the method
          - we'll use the getX() and getY() method to get the x and y coordinates of the event

  • show MouseEvents demo

  • show assignment 1 solution
       - we'll provide you with most of the code, you just have to fill in some of the details
       
  • Coin class
       - extends Ellipse2D.Double
       - again, what does the call to "super" do?
       - moveTo method allows us to tell allow other classes/methods to move the coin around
          - setFrame readjuts the object to an x, y location and a new width and height
          - how will the coin be moved with respect to the x, y coordinates input to the moveTo method?

  • CoinSquare class
       - extends Rectangle2D.Double
       - besides being a graphical class, they're also a container for coins
          - setCoin(Coin coin)

  • both the Coin class and the CoinSquare class inherit the method "boolean contains(int x, int y)" which tells you whether or not the object (Coin or CoinSquare) have the coordinates x, y within their shape

  • GraphicsCoinStrip class

  • import
       - I've told you a package is a collection of classes. java.awt is a package. What do you think "import java.awt.*" means?
       - why do we need import statements if these packages/classes are built in to java already?
          - they make our life easier within the code. We could write:
             - javax.swing.JFrame instead of JFrame
             - java.awt.Color.RED instead of Color.RED
             - java.util.ArrayList instead of ArrayList
                - java.util.ArrayList<Integer> list = new java.util.ArrayList<Integer>();
          - We can use the simpler name, instead of the fully qualified name (with package, etc)
       - An alternative to importing all of the classes in a package is to just import the one class we need:
          - import java.swing.JFrame;


  • Printing objects
       - we've seen System.out.println(...)
       - PrintWriter out;
          out.println(...)
       - For Strings, Integers, Doubles, etc. it's straightforward to see what is printed
       - What about other objects, for example what about an ArrayList?
       - String toString()
          - Every class has a toString() method that is called in these situations
          - If one isn't explicitly defined, where do you think it comes from?
             - Remember, all classes implicitly inherit from Object
       - Let's look at our Vector class again
          - What do you think will happen if we try and print a Vector from this class?
             - The default for Object is just to print out the address in memory of the Object (not very useful!)
          - We can override the method by adding our own toString method:
             public String toString(){
                ...
             }