CIT 594 Assignment 2: Recursive Drawings
Spring 2009, David Matuszek


General Idea:

Write six (or more) methods to make recursive drawings. The first drawing should replicate the example below.

In order to allow you to concentrate on the recursion, I am providing starter code that implements a simple GUI. Feel free to modify this code however you like. (I especially recommend putting more meaningful names on the buttons.)


Draw a square (figure 1). Then at each corner of the square, draw a smaller square (figure 2). Then at the corners of each of those squares, draw even smaller squares (figure 3). Continue this process as many times as desired.

Figure 1
Figure 2
Figure 3

How to do it:

The general technique is to write a method that takes these parameters:

The initial call to this method will be with x and y representing the center of the drawing area and depth representing the maximum depth of the recursion. The method usually does something like the following:

void draw(Graphics g, int x, int y, int size, int depth) {
    if depth == 0, return
    draw some figure, centered at x, y, with the given size
    at various points around the figure {
        draw(..., a smaller size, depth-1)

The "at various points" indicates several recursive calls, possibly in a loop, but more likely just a list of calls. Each call should be with a reduced depth, where the base case is a depth of zero (which doesn't do anything).

This is a formula that works well, and you should probably use it to create the above drawings. But once you understand the formula, feel free to try variations on it. For example, you might omit the depth parameter, and use as a base a sufficiently small size. Also feel free to modify the GUI as needed.

Here are some things you might try:

Javadoc your new methods (even if they're private). No JUnit tests in this assignment, since it's all GUI output.

Additional points:

These are not intended as additional requirements. If you get an interesting pattern (probably accidentally!) by violating some of these, feel free to keep it.


Due Date:

Thursday, January 29, before midnight.