import java.awt.Color; import graphics.*; public class DrawApp { private static int nCircles = 50; // main method public static void main(String[] args) { // Java provides a default constructor (no parameter version) // IF there is NO constructor at all // ERROR: non-default constructor exists in CircularStroke //CircularStroke csDefault = new CircularStroke(); //canvas.resize(400, 400); // create a Strokes array of size nCircles // use IS-A relationship // e.g.) CircularStroke IS-A Stroke, SquareStroke IS-A Stroke Stroke[] strokes = new Stroke[nCircles]; //strokes[0].draw(); int cx; int cy; int size = 10; Color c = Color.BLACK; boolean f = true; char choice = canvas.promptChar("Choose stroke type: C or S"); canvas.waitForUserClick(); for (int i = 0; i < nCircles; ++i) { //canvas.clear(); // sample mouse position cx = canvas.getMouseX(); cy = canvas.getMouseY(); // polymorphism: variable stroke can point to object of different types // any behavior you expect on the stroke must be at least // declared in the Stroke class (at least method signature) Stroke stroke = null; // polymorphism // variable scope: only available/accessible within the closest enclosing { } if (choice == 'C' || choice == 'c') { stroke = new CircularStroke(cx, cy, size, f); } else if (choice == 'S' || choice == 's'){ stroke = new SquareStroke(cx, cy, size, f); } else { // for any default/error handling, the code is here } /****** HUGE concept: DYNAMIC BINDING *****/ stroke.draw(); strokes[i] = stroke; canvas.sleep(0.08); } canvas.waitForUserClick(); canvas.clear(); for (Stroke stroke : strokes) { stroke.draw(); } } }