Start by snarfing the assignment code assign9_recart_cps006_spring10.
Recursion is "the process of solving large problems by breaking them down into smaller, more simple problems that have identical forms." Given solutions to those smaller problems, you must determine how to use that result to answer the more general problem. Additionally, you need to figure when the smaller problem has a trivial implementation (i.e., drawing one square or doing nothing) --- this case halts the recursion. Drawing pictures recursively is a good way to to visualize the recursive process and an elegant way to create complex and interesting pictures.
We will look at two different approaches to creating the artwork shown below.
The first task is to create a recursive method that is called
from the shape's paint
method. In this case, there is one
shape, with one set of instance variables, and how it is drawn is
recursively.
You will modify Target.java only. Notice that this class extends the Mover class, which has lots of useful methods. For example, if you want to determine the size of the canvas you can call the method getSize().
In Target, the recursive method you need to modify is called recurseDraw. In this method you need to draw one circle and then recursively draw it again with a smaller size and a different color on top of it. You also need to decide when to stop drawing (recursion needs a way out).
To draw the circle (remember pen.setColor and pen.fillOval), you will need to figure out the dimensions of the circle. It will be helpful to look at myNumRings , the total number of rings to draw, and numLeft, the number of rings left to draw. It may also be helpful to get the center of the canvas (again look at Mover.java for a helpful method to call).
You will need to change the color of each circle each time you draw it so you can distinguish between the drawings. You could start with one color and change the amount of red, green or blue by some amount each time (you will need to tie this to the one thing that changes with each recursive call).
Note that there is a method randomColor() in the method Mover. If you want to use it, then you will need to comment out the last line in the paintComponent method in the Canvas class.
// repaint();If you don't then your picture will actually be an animation with the rings rapidly changing colors. The reason is that the Canvas class works by repainting the screen over and over again and you are creating a new color each time the drawings are repainted.
Commenting out the repaint command should not affect the rest of this classwork.
You will do the same problem again with a different approach.
This approach is to actually create the complete shape
from several smaller shapes, which are themselves created from smaller
shapes. In this case, there are several shapes, with several different
instance variables, and how they are created is recursive. Then the
paint
method simply paints the current shape and then
tells the next shapes created to paint themselves.
You will modify Target2.java and create a picture that is similar to the previous problem, just created in a different way. In this case you will need to make changes in several places.
Notice the new state variable myNext, which is of type Target2. This is one place where we are using recursion. If a Target2 object is not the last circle to be drawn, then myNext will be set to a new Target2. If it is the last circle to be drawn, then myNext will be set to null.
Thus in the constructor, you will construct the object (that has already been done with the call to super), initialize myNumRings and initialize myNext (as a smaller Target2). Thus myNext will be initialized by using "new".
You also need to complete the paint method. You will need to paint the current circle (setColor and fillOval) and then paint the smaller circle (myNext, if it exists) by having it call its paint method.
Note that in this version you can use randomColor, in fact it is already being used for you. Each Target2 that is created is generated with a random color. You can use the getColor() method in the Mover class to access that color.
This classwork will continue as assignment 9 and be turned in as assignment 9.