The Corner Class
The Corner class encapsulates the w and d pixel values on the screen for one corner of a
triangle. As shown in the legend on the screen capture, the w (width) dimension goes left to
right and the d (down) dimension goes top to bottom. (DON’T MODIFY IT)
• The Main Class
The Main class set up the window to display the triangles fractal pattern. (DON’T
MODIFY IT)
Your task is to complete the Triangle class and the TriangleGUI class.
• The Triangle Class
The Triangle class encapsulates three Corner objects that define the w and d position of
each corner of the triangle that it represents. The Triangle class has a constructor that
creates a Triangle object with three Corner objects provided as parameters.
Task 1: The getNextLevel() method calculates the corners of the three lower level
triangle at the corners and returns an ArrayList of Triangle object that saves these
three lower level triangles. You must figure out how to calculate the three new corners using
2
the midpoints of the edges of the current triangle. There is a mid method in the Corner
class that may be useful. Be sure to generate the corners of each of the new triangles so that
it is oriented same as the current triangle.
Task 2: The size() method calculates the size (perimeter) of the triangle and returns an
int value. This value is used in each of your drawing method to determine if the current
triangle has gotten too small to continue drawing its sub-triangles. You must write the code
for this method also. (Base the size on its geometric perimeter.) There is a len method in the
Corner class that may be useful.
• The TriangleGUI Class
The TriangleGUI class provides methods for drawing triangles in fractal pattern and
paint the figure on a Canvas.
Task 3: Complete an iterative method to draw the triangles in fractal pattern.
Step 1: you should an ArrayList object that you can use to store the triangle objects at
each level. You add the initial triangle that you received as a parameter into the ArrayList
to start the iteration.
Step 2: Then use a while loop with a condition based on whether there is still another
triangle in the ArrayList (Hint: you can use the isEmpty() method to check if the ArrayList
is empty or not). In the body of the loop, remove the first triangle from the ArrayList.
Process it by drawing it (there is a draw() method in the Triangle class) and based on its
size determine if you are going to draw its next lower level triangles. If so, get each of the
three next lower level triangles from the Triangle getNextLevel() method and
add it into the ArrayList. When the body of the loop stops adding lower level triangles
into the ArrayList, the while loop will complete the drawing of the triangles down to that
level and exit. The Triangle class has a symbolic constant SMALLEST that you can use
to terminate drawing before the triangles get too small to see.
Task 4: Complete the recursive method to draw the triangles in fractal pattern. You do not
need to use an explicit data structure. Also, there must be NO iteration statements (while, for,
3
or do … while). You must use selection statements (if – else or switch) and recursive calls
only.
The process is similar except that the recursive drawTriangleRecursive method calls
it self. Process each triangle by drawing it and based on its size determine if you are going to
draw its next lower level triangles. If so, call drawTriangleRecursive three times –
once with each of the next lower level triangles obtained from the Triangle
getNextLevel() method. When your code returns from each recursive call to
drawTriangleRecursive, the context of the previous level of recursion will be
automatically restored.
Task 5: In the paint method, uncomment the drawTriangleRecursive() method or
drawTriangleIterative() method to check if your methods work correctly.