Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

NestedSquaresDrag Demo

A working demo of NestedSquaresDrag will appear below. Click inside the applet to interact with it.



NestedSquaresDrag BlueJ Project

Click here to download a BlueJ project for NestedSquaresDrag.


NestedSquaresDrag Source Code

The Java source code for NestedSquaresDrag is below. Click on a file name to download it.


NestedSquares.java

import objectdraw.*;
import java.awt.*;

/*
 * Example NestedSquares: a recursive implementation of an object
 * consisting of nested squares, complete with contains and move
 * methods to support dragging.
 *
 * And remember, Don't Worry, Be Happy!
 *
 * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013
 *
 * $Id: NestedSquares.java 2227 2013-10-24 03:37:54Z terescoj $
 */

public class NestedSquares {

    private static final int SIZE_CHANGE = 4;
    
    // instance variables to keep track of the outermost
    // square and then NestedSquares inside
    private FramedRect outline;
    private NestedSquares rest;
    
    // a recursive implementation: we draw nested squares
    // by creating the outermost square, then calling
    // THIS CONSTRUCTOR to draw the rest, which are really
    // just another set of nested squares, just a little bit
    // smaller and a little bit down and to the right.
    public NestedSquares(double x, double y, int size, DrawingCanvas c) {
        
        // draw one        
        outline = new FramedRect(x, y, size, size, c);
        
        // draw more only if there are more to draw, otherwise
        // we remember rest as null (so we know the recursion
        // is over) -- if size is not greater than SIZE_CHANGE,
        // the next level would not exist, so we don't try to
        // draw it
        if (size > SIZE_CHANGE) {
            // draw the rest
            rest = new NestedSquares(x + SIZE_CHANGE/2, y+SIZE_CHANGE/2,
                size - SIZE_CHANGE, c);
        }
        else {
            // this will flag the end of the recursion
            rest = null;
        }
    }
    
    // contains is easy - we just check containment of the outline
    public boolean contains(Location point) {
        
        return outline.contains(point);
    }
    
    // move method will need to be recursive: we move the whole thing
    // by moving the outline, then moving the rest (using this method!)
    public void move(double dx, double dy) {
        
        outline.move(dx, dy);
        if (rest != null) {
            rest.move(dx, dy);
        }
    }
}

NestedSquaresDrag.java

import objectdraw.*;
import java.awt.*;

/*
 * Example NestedSquaresDrag: draw a nested squares object,
 * drag it around.
 *
 * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2013
 *
 * $Id: NestedSquaresDrag.java 2227 2013-10-24 03:37:54Z terescoj $
 */

public class NestedSquaresDrag extends WindowController {
   
    // standard dragging stuff: nothing here hints at recursion
    private NestedSquares theSquares;
    private boolean dragging;
    private Location lastMouse;
   
    public void begin() {
    
        theSquares = new NestedSquares(100, 100, 100, canvas);
    }
    
    public void onMousePress(Location point) {

        if (theSquares.contains(point)) {
            dragging = true;
            lastMouse = point;
        }
    }
    
    public void onMouseDrag(Location point) {
        
        if (dragging) {
            theSquares.move(point.getX() - lastMouse.getX(),
                            point.getY() - lastMouse.getY());
            lastMouse = point;
        }
    }
    
    public void onMouseRelease(Location point) {
        
        dragging = false;
    }
}