Click on the very left edge of Applet to make a cool color effect. Then click and move it again.

Then click in very topmost region of the Applet window, so see another special effect on the next move after that.


/*  Written on November 19th, 2002 - Mark Jacobson  
    Compare the NestedSquare2 class to the NestedSquares 
    recursively defined class.    
    Chapter 13 of Big Java textbook - Pages 521-530         */

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

public class NestedSquare2Controller extends WindowController {
   
   private NestedSquare2 nestedSquares;
   
   public void begin() 
   {
      new Text("Click in Applet to 'moveTo' that location", 
                200, 10, canvas);

      nestedSquares = new NestedSquare2(100, 100, 100, canvas);
      new Text(nestedSquares.area() + " is the total area.", 
               10, 10, canvas);
   } 

   public void onMousePress (Location point) 
   {
      if (point.getX() < 20)
         nestedSquares.colorOddRed();
      else if (point.getY() < 20)
         nestedSquares.removeEveryOther();
      else
         nestedSquares.moveTo(point.getX(), point.getY());     
   }
} // end NestedSquare2Controller class

------------------------------------------------------------------------------

// Verified that the area() is correct by changing SHIFT constant to 10
// and using the bc calculator (on cowboy.cns.uni.edu) to see this:

// venkman@julia:~/java$ bc
//     100^2 + 80^2 + 60^2 + 40^2 + 20^2
//     22000                           <--- 22,000 = area() method result

import objectdraw.*;                        
import java.awt.*;
import java.util.ArrayList;

public class NestedSquare2 
{
   private static final double SHIFT = 2;
   private ArrayList squares;
    
    public NestedSquare2(double x, double y, double size, DrawingCanvas canvas) 
    {
        squares = new ArrayList();

        for (int i = (int) size; i >= 4; i -= 2*SHIFT)
        {
           squares.add(new FramedRect( x, y, size, size, canvas ));
           x = x + SHIFT;
           y = y + SHIFT;
           size = size - 2 * SHIFT;
        }
    }
    
    public void moveTo(double x, double y) 
    {
        FramedRect f;

        for (int i = 0; i < squares.size(); i++)
        {
           f = (FramedRect)squares.get(i);
           f.moveTo(x, y);
           x += SHIFT;
           y += SHIFT;            
        } 
    }

    public double area()
    {
       double totalArea = 0;
       for (int i = 0; i < squares.size(); i++)
       {
          FramedRect f = (FramedRect)squares.get(i);
          totalArea += f.getWidth() * f.getHeight();
       }
       return totalArea;
    }

    public void colorOddRed()
    {
       for (int i = 0; i < squares.size(); i += 2)
       {
           FramedRect r = (FramedRect)squares.get(i);
           r.setColor( Color.red );
       }
    }

    public void removeEveryOther()
    {
        for (int i = squares.size() - 1; i >= 0; i -= 2)
        {
           squares.remove(i);
        }
    }
}