Computer Science 120
Introduction to Programming

Spring 2012, Siena College

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.


RandomOvalsOnClick.java

import objectdraw.*;

import java.awt.*;



/**

 * FILLS A CANVAS UP WITH RANDOM COLORED DOTS AT RANDOM POINTS

 * 

 * @author DR. Sharon Gower Small

 * @version version 1.0

 */



public class RandomOvalsOnClick extends WindowController

{

    private static final int OVAL_DIM = 10;



    //RANDOM INT GENERATORS FOR THE X Y LOCATION

    private RandomIntGenerator rigX = new RandomIntGenerator(0,canvas.getWidth());

    private RandomIntGenerator rigY = new RandomIntGenerator(0,canvas.getHeight());



    //RANDOM INT GENERATORS FOR THE COLOR

    private RandomIntGenerator colorNums = new RandomIntGenerator(0,255);



    public void begin(){



        //INITIALIZE X.Y RANDOM INT GENERATORS

        rigX = new RandomIntGenerator(0,canvas.getWidth());

        rigY = new RandomIntGenerator(0,canvas.getHeight());



        

    }

    

    /*

     * WILL ADD 1000 OVALS TO THE CANVAS WITH EVERY MOUSE CLICK

     */

    public void onMouseClick(Location point){

        int count = 0;

        while (count < 1000){



            FilledOval randomOval = new FilledOval(rigX.nextValue(), rigY.nextValue(),OVAL_DIM,OVAL_DIM,canvas);

            count++;

            randomOval.setColor(new Color(colorNums.nextValue(),colorNums.nextValue(),colorNums.nextValue()));



        }



        

    }



}