MIT 6.030: 
Introduction to Interactive Programming

Laboratory 5: Scibble

http://www-cs101.ai.mit.edu/courses/fall99/psets/scribble/



Finger Exercises

Q: Define a class that implements java.awt.event.MouseListener and extends the mouseClicked(MouseEvent) method by printing the coordinates of the point on which the mouse had clicked. You may also want to make use of the class java.awt.event.MouseAdapter. (Bonus: also print the components of the previous mouse click.)
A:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import cs101.io.Console;

public class DisplayClicks extends MouseAdapter
{
    mouseClicked(MouseEvent e)
    {
        Console.out.println("Mouse clicked at (" + e.getX() + ", " +
                            e.getY() + ").");
    }
}
Bonus:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import cs101.io.Console;

public class DisplayClicks extends MouseAdapter
{
    private int lastX;
    private int lastY;
    private boolean firstClick = true;
    mouseClicked(MouseEvent e)
    {
        if (!firstClick)
        {
            Console.out.println("Last click at (" + this.lastX + ", " +
                                this.lastY + ").");
            firstClick = false;
        }

        Console.out.println("Mouse clicked at (" + e.getX() + ", " +
                            e.getY() + ").");
        lastX = e.getX();
        lastY = e.getY();
    }
}
Q: Now define a class that extends java.awt.Canvas and sends its mouse events to your MouseListener.
A:
import java.awt.Canvas;

public class ClickyCanvas extends Canvas
{
    public ClickyCanvas()
    {
        this.addMouseListener(new DisplayClicks());
    }
}
Q: Define a class that implements java.awt.event.WindowListener and extends the windowClosing() and extends the windowClosing() method by printing "Nah, nah, you can't kill me!" (Alternatly, you can do the potentially more useful thing and (1) call the object's dispose() method and (2) call System.exit(0).) What class do you think would be useful when implementing WindowListener?
A:
import java.awt.event.WindowAdapter;
import cs101.io.Console;

public class IndestructibleWindowListener extends WindowAdapter
{
    public windowClosing(WindowEvent e)
    {
        Console.println("Nah, nah, you can't kill me!");
    }
}
Bonus:
import java.awt.event.WindowAdapter;
import cs101.io.Console;

public class DestructibleWindowListener extends WindowAdapter
{
    public windowClosing(WindowEvent e)
    {
        e.getWindow().dispose();
        System.exit(0);
    }
}
Q: What methods does a Graphics have that allow you to make things appear on it? (One such method is drawOval).
A: draw3DRect, drawBytes, drawChars, drawImage, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString, fill3DRect, fillOval, fillPolygon, fillRect, and fillRoundRect.
Q: Define a class that extends java.awt.Canvas and looks like a (black and white) Japanese flag, i.e., it has a circle at (100,100). A:
import java.awt.Graphics;
import java.awt.Canvas;

public class JapaneseFlag extends Canvas
{
    public void paint(Graphics g)
    {
        g.fillOval(75, 75, 50, 50);
    }
}
Bonus
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.Color;

public class JapaneseFlag extends Canvas
{
    public paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillOval(75, 75, 50, 50);
    }
}
Extra Bonus
import java.awt.Graphics;
import java.awt.Canvas;
import java.awt.Color;

public class JapaneseFlag extends Canvas implements MouseListener
{
    private Color currentColor = Color.black;

    public paint(Graphics g)
    {
        g.setColor(currentColor);
        g.fillOval(75, 75, 50, 50);
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) 
    {
        this.currentColor = Color.red;
        this.repaint();
    }
    public void mouseExited(MouseEvent e) 
    {
        this.currentColor = Color.black;
        this.repaint();
    }
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

}

Development Plan

  1. Create a Main class which will instantiate the WhiteBoard and SmartCanvas.
  2. Create and attach a mouse listener which prints out coordinates of mouse pressed events.
  3. Augment the mouse listener so that it also prints out mouse dragged events.
  4. Print out pairs of coordinates with last position and new position.
  5. Add lines to the repisitory upon each mouse dragged event.
  6. Do extensive testing to determine if scibbling behavior is what I want.
  7. Augment the listener to handle action events, add it as a listener to the clear button, and enable line clearing.
  8. Augment the listener to handle item events, add it as a listener to the color list, and enable color selection
  9. Final testing and evaluation

Laboratory

This course is a part of Lynn Andrea Stein's Rethinking CS101 project at the MIT AI Lab and the Department of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology.
 

Questions or comments:

cs101-webmaster@ai.mit.edu>