Lab 1640 - Tendron

Description

In this lab, you will use recursion to generate a graphic image. Your code will draw a "plant" recursively, modifying the structure as the elements get smaller, going through smaller branches and finally to leaves. Every time the tendron program runs, it generates a new and different plant since growth occurs randomly.

The main program, Tendron.java, sets up the graphics world for the image. When asked to paint, it creates a new Cluster, passing it the graphics context to draw on. Then it calls the display function of the Cluster class, telling it to draw a cluster starting at the midpoint of the screen using 50 short segments.

A cluster is a group of seven tendrils. Each tendril is made of a series of short line segments, and as each segment is drawn, it may turn a bit left or right, giving a more life-like appearance.

At the end of each tendril, a new cluster is created. The new cluster will have fewer line segments, therefore each tendril will be shorter than before. Recursively, these tendrils will again spawn clusters until a minimum tendril length is reached. Here is the final result:

tendron

When drawn, the tendrils together look very plant-like for such a simple algorithm. Notice the effect is enhanced by using the tendril length to determine the line color used when drawing.

Assignment

You need to create two new classes: Cluster and Tendril.

The Cluster class has a constructor that squirrels away the Graphics context to be used later, and one public method, display. The display method creates seven tendrils by creating Tendril objects and then invoking their display methods.

The Tendril class has a constructor that squirrels away the Graphics context passed to it by Cluster. This class does the actual drawing through its only method, display. The display method draws the tendril using the drawLine(int, int, int, int, ) method of the Graphics class. The drawLine method and the setColor method are the only methods you need to draw your image.

For example, if you were passed the graphics context in your constructor and saved it as Graphics myGraphics, you could draw a red line from the point (10, 15) to the point (30, 75) with these two lines of code:

    myGraphics.setColor(Color.red);
    myGraphics.drawLine(10, 15, 30, 75);

In drawing the tendril, remember to wiggle the direction and to use an appropriate color for a life-like appearance. After each tendril is drawn, conditionally create a new, smaller cluster on the end, which will have its own seven tendrils, each of which may have a cluster at the end...


Copyright © 2010 by Asylum Computer Services LLC Return to CS Labs