CSIS 3103 - Assignment 3: Fractals
Fall 2010


You can work in pairs on this assignment.

Goals:

Background:

Fractal patterns have been studied extensively since the mid-1970s. A fractal exhibits "self-similarity", which means that it looks roughly the same at any scale. Mathematically, a fractal is based on a a recursive formula. Perhaps the most famous fractal is the Mandelbrot Set, named after Benoit Mandelbrot (Nov 20, 1924 - Oct 14, 2010), the mathematician who defined it. Fractals often occur in nature, for example snow flake patterns, leaf patterns, etc.

Fractals also have been found to possess useful properties for practical applications. One example is a fractal pattern used for cell phone antennas:

  (Image link)

Problem Description:

We're going to develop a program that draws the fractal for this antenna design - called the Sierpinski carpet. The pattern begins with a background square that is divided into nine sub-squares. The center square is filled with a different color.  The following image has dashed lines to show the positions of the nine sub-squares:

The fractal pattern is repeated in the eight black squares that surround the white center square.

Your program must be capable of drawing these fractals with different levels. Below are several illustrations of the repeated pattern:

Level 2

Level 3

Level 4

Level 5

There are two classes that need to be implemented for this project - one to calculate and draw the fractal image and one with a main method to start execution of the program. The program should prompt the user for the number of levels to draw. Include appropriate error checking for user input.

The class to calculate and draw the fractal image will extend the JComponent class. This class must have a method

    public void paintComponent(Graphics g).

This method is automatically called every time a component needs to be redrawn in a window. The Graphics object g is able to draw simple obects, but the subclass Graphics2D is more useful, since it can draw shape objects, like Rectangle. Just do a typecast to get these features:

Graphics2D g2 = (Graphics2D) g;

See the Java API documentation for the appropriate methods and parameters.

Like many of the examples we've seen, the paintComponent acts as a wrapper method and calls another method which uses recursion to do the necessary calculations for drawing the squares by passing appropriate parameters.

The other class needs to make a window (JFrame ) and display the fractal image in it. The steps involved are:

  1. make a JFrame,
  2. set its initial size (make it square),
  3. tell it what to do when the window closes (exit the program),
  4. add a fractal component to it,
  5. set its background color
  6. make it visible.

The JFrame class has methods for all of these operation - check the API documentation. Be sure to include a statement similar to the following to exit the program when the window is closed:

frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE);

To set the background color of a JFrame called frame, use frame.getContentPane().setBackground(Color.BLACK);

You can choose any drawing colors.

The fractal pattern should automatically resize whenever the window is resized - including stretching the squares into rectangles if necessary.

An executable sample is available here: Fractal.jar

Submissions:

Submit just the two java files for your project (none of the other project files).

Extra credit:

Here is another example of a fractal antenna design published in U.S. Patent No. 7088965:

 

Implement another JComponent class to draw fractal patterns like this. Here the levels determine how many times the pattern repeats.