Click on the canvas to create a colored oval. Select an existing oval with the mouse
and drag it around the canvas; drop it into the black box in the lower right corner
to delete it from the canvas. Move the mouse out of the canvas to change the
color of the existing ovals
Code:
import java.awt.*;
import objectdraw.*;
/*
* @author Barbara Lerner modified by Lisa Ballesteros
* @version April, 2009
*
* A simple drawing program that allows the user to place dots,
* drag them around and delete them by dragging them to the trash.
*/
public class DrawingProgram2b extends WindowController {
// Dimensions of trash can
private static final int TRASH_WIDTH = 40;
private static final int TRASH_HEIGHT = 3*TRASH_WIDTH/2;
// 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;
private FilledRect trash;
// Used to generate random colors
private RandomIntGenerator colorGen = new RandomIntGenerator (0, 255);public void begin ( ) {
trash = new FilledRect(.9*canvas.getWidth(), canvas.getHeight()-2*TRASH_HEIGHT, TRASH_WIDTH, TRASH_HEIGHT, canvas);
}
/**
* 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;
if (selectedShape != null && selectedShape.overlaps(trash)) {
int place = shapes.findShapePosition(selectedShape);
shapes.removeShapeAtPosition(place);
selectedShape.removeFromCanvas();
}
selectedShape = null;
}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 modified by Lisa Ballesteros
* @version April, 2009
*/
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;
}
// Return the index of @param shapeToFind in the shape array.
// If it is not found, return -1;
public int findShapePosition (FilledOval shapeToFind) {
for (int i = 0; i < numShapes; i++){
if (shapes[i] == shapeToFind){
return i;
}
}
return -1;
}
/*
* Remove the shape @param shapeToRmv from the array
* and shift array contents down.
*/
public void removeShapeAtPosition (int position) {
if (position < numShapes) {
numShapes--;
for (int place = position; place < numShapes; place++) {
shapes[place] = shapes[place+1];
}
shapes[numShapes] = null;
}
}public void colorShapes (Color newColor){
for (int i = 0; i < numShapes; i++){
shapes[i].setColor(newColor);
}
}
}