Computer Science 202
Introduction to Programming
Fall 2013, The College of Saint Rose
RandomOvalsOnClick Demo
A working demo of RandomOvalsOnClick will appear below. Click inside the applet to interact with it.
RandomOvalsOnClick BlueJ Project
Click here to download a BlueJ project for RandomOvalsOnClick.
RandomOvalsOnClick Source Code
The Java source code for RandomOvalsOnClick is below. Click on a file name to download it.
/** * Use a loop to draw lots and lots of randomly colored and positioned circles. * * @author Jim Teresco * * Developed in class, The College of Saint Rose * CSC 252, Fall 2013 * * $Id: RandomOvalsOnClick.java 2223 2013-10-22 19:30:12Z terescoj $ */ import objectdraw.*; import java.awt.*; public class RandomOvalsOnClick extends WindowController { // how big? how many? private static final double OVAL_SIZE = 10; private static final int OVALS_PER_CLICK = 100; // random generation stuff private RandomDoubleGenerator xGen; private RandomDoubleGenerator yGen; private RandomIntGenerator colorGen = new RandomIntGenerator(0,255); public void begin() { xGen = new RandomDoubleGenerator(0, canvas.getWidth() - OVAL_SIZE); yGen = new RandomDoubleGenerator(0, canvas.getHeight() - OVAL_SIZE); } public void onMouseClick(Location point) { // option 1: count up /* // count the number of ovals we've drawn so far int counter = 0; while (counter < OVALS_PER_CLICK) { // if there's another oval to draw, draw it new FilledOval(xGen.nextValue(), yGen.nextValue(), OVAL_SIZE, OVAL_SIZE, canvas) .setColor(new Color(colorGen.nextValue(), colorGen.nextValue(),colorGen.nextValue())); // remember that we've drawn another counter++; } */ // option 2: count down // count the number of ovals yet to draw int counter = OVALS_PER_CLICK; while (counter > 0) { new FilledOval(xGen.nextValue(), yGen.nextValue(), OVAL_SIZE, OVAL_SIZE, canvas) .setColor(new Color(colorGen.nextValue(), colorGen.nextValue(),colorGen.nextValue())); counter--; } } }