DrawingProgram Applet


Your browser is ignoring the <APPLET> tag!


 

Code:

 

import java.awt.*;

import objectdraw.*;

/*

 * A simple drawing program that allows the user to place dots,

 * drag them around and delete them by dragging them to the trash.

 *

 * Author Barbara Lerner adapted by Lisa Ballesteros

 * @version December 3, 2007  (April 2009)

 */

public class DrawingProgram extends WindowController {

 

    // Size of shape to draw

    private static final int SIZE = 20;

 

    // The variable to hold the last shape created

    private FilledOval selectedShape;

   

    // All the shapes added so far

    private ShapeCollection shapes = new ShapeCollection();

 

    // Used to support the usual dragging algorithm

    private boolean dragging;

    private Location lastMouse;

   

    // Used to generate random colors

    private RandomIntGenerator colorGen = new RandomIntGenerator (0, 255);

 

    /**

     * When the user clicks the mouse, draw a small filled oval.

     * @param point the location where the user clicked

     */

    public void onMouseClick (Location point) {

        // Create a shape

        selectedShape = new FilledOval(point, SIZE, SIZE, canvas);

        Color shapeColor = new Color (colorGen.nextValue(), colorGen.nextValue(), colorGen.nextValue());

        selectedShape.setColor (shapeColor);

        shapes.addShape (selectedShape);

    }

 

    /*

     * Select the object pressed on and start a drag of that object

     * @param loc where the user pressed the mouse button down

     */

    public void onMousePress(Location loc) {

        // Select the new object and initialize the drag.

        selectedShape = shapes.getShapeAt (loc);

        if (selectedShape != null) {

            dragging = true;

            lastMouse = loc;

        }

    }

   

    /*

     * Drag the shape with the mouse.

     * @param loc where the mouse is

     */

    public void onMouseDrag(Location loc) {

        if (dragging) {

            // Move the shape and the selection border around the shape

            selectedShape.move(loc.getX() - lastMouse.getX(), loc.getY() - lastMouse.getY());

            lastMouse = loc;

        }

    }

 

    /*

     * Stop a drag.  If the user dragged a shape to the trash, remove the shape

     * @param point where the mouse is.

     */

    public void onMouseRelease(Location point) {

        dragging = false;

    }

 

    public void onMouseExit (Location point){

        Color shapeColor = new Color (colorGen.nextValue(), colorGen.nextValue(), colorGen.nextValue());

        shapes.colorShapes(shapeColor);

    }

}

 

import java.awt.*;

import objectdraw.*;

/**

 * A collection of shapes

 *

 * @author Barbara Lerner adapted by Lisa Ballesteros

 * @version December 3, 2007

 */

public class ShapeCollection

{

    // The maximum number of shapes in the collection

    private static final int MAX_SHAPES = 200;

   

    // The actual shapes

    private FilledOval[] shapes;

   

    // The number of shapes currently in the collection

    private int numShapes;

   

    /**

     * Create a new, empty collection

     */

    public ShapeCollection () {

        // Create the array to hold the shapes

        shapes = new FilledOval[MAX_SHAPES];

       

        // Remember that it is currently empty

        numShapes = 0;

    }

   

    /**

     * Add a shape to the collection.  If the collection is already full, this method

     * does nothing.

     * @param newShape the shape to add

     */

    public void addShape (FilledOval newShape) {

        if (numShapes < shapes.length) {

            shapes[numShapes] = newShape;

            numShapes++;

        }

    }

   

    /**

     * Find the topmost shape that contains the point.

     * @param point the coordinate we are looking for.

     * @return the topmost shape at the point.  If there is no shape there, return null.

     */

    public FilledOval getShapeAt (Location point) {

        // We search from the back of the array to the front because the shapes were placed

        // in the order in which they were created.  The topmost shape is at the end of

        // the array.

        for (int i = numShapes - 1; i >= 0; i--) {

            if (shapes[i].contains (point)) {

                return shapes[i];

            }

        }

       

        // No shape there.

        return null;

    }

 

   public void colorShapes (Color newColor){

       for (int i = 0; i < numShapes; i++){

           shapes[i].setColor(newColor);

        }

    }

}