ShapeDriver Class
You will add code to the setup() method to create an array of various Shapes and call the draw() method which will display all of these Shapes each frame.
- Declare an instance variable which is an array of type Shape
- In setup()
- Allocate enough space in your Shape array to store 4 Shapes.
- Initialize the elements of a shape array to contain two cirlces and two rectangles.
- Choose x and y coordinates, and width, height, and radius values that will fit within our 200x200 window.
- For the fifth argument in the constructor which expects an object of type PApplet, have a look at the ShapeDriver class declaration. The ShapeDriver
extends from PApplet and you should pass the current instance as the fifth argument to the constructor. The call to the Rectangle constructor
for example could look like this from the ShapeDriver class
new Rectangle(120,400,250,300,this)
-
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.