Computer Science 252
Problem Solving with Java
Spring 2015, 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.
import objectdraw.*; import java.awt.*; import java.util.Random; /** * FILLS A CANVAS UP WITH RANDOM COLORED DOTS AT RANDOM POINTS * * @author DR. Sharon Gower Small * @version version 1.0 * * Updated by Jim Teresco, The College of Saint Rose, Fall 2013 * * $Id: RandomOvalsOnClick.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class RandomOvalsOnClick extends WindowController { private static final int OVAL_DIM = 10; private static final int OVALS_PER_CLICK = 1000; // Random number generator private Random randomGen = new Random(); /* * add a lot of randomly placed and colored ovals on each click */ public void onMouseClick(Location point) { for (int count = 0; count < OVALS_PER_CLICK; count++) { FilledOval randomOval = new FilledOval(randomGen.nextInt(canvas.getWidth() - OVAL_DIM), randomGen.nextInt(canvas.getHeight() - OVAL_DIM), OVAL_DIM,OVAL_DIM,canvas); randomOval.setColor(new Color(randomGen.nextInt(256), randomGen.nextInt(256), randomGen.nextInt(256))); } } }