Lab7 Class
You will add code to the setup() method to create an array of various Shapes and code to the draw() method which will display all of these Shapes each frame.
- Declare an array variable of type Shape[]
- In setup()
- Allocate enough memory for your Shape array to store 4 Shapes.
- Initialize each element of the Shape array to be a new instance of either Rectangle or Circle.
- Choose x and y coordinates, and width, height, and radius values that will fit within our 200x200 window.
-
In draw()
- Loop through each element of the Shape array, and call Shape's draw() on each one.
shapes[i].draw();
- Loop through each element of the Shape array, and call Shape's draw() on each one.
Note here that we are invoking the abstract draw method on each Shape, but Java is selecting the specific implementation to call from our Rectangle or Circle class at runtime.
Testing
In the Lab7's draw method, add the following line right before you draw each shape:fill (i * 64);
Where i is the index of the Shape you are currently drawing.
The fill() method from PApplet chooses a color for each shape drawn (rect and ellipse in our case).
Depending on how you choose to initialize each shape, your window should look similar to:
![](./images/test.png)