Computer Science 120
Introduction to Programming

Spring 2012, Siena College

MoreColorfulSpirograph Demo

A working demo of MoreColorfulSpirograph will appear below. Click inside the applet to interact with it.



MoreColorfulSpirograph BlueJ Project

Click here to download a BlueJ project for MoreColorfulSpirograph.


MoreColorfulSpirograph Source Code

The Java source code for MoreColorfulSpirograph is below. Click on a file name to download it.


MoreColorfulSpirograph.java

import objectdraw.*;
import java.awt.*;

/*
 * Example MoreColorfulSpirograph
 *
 * Another spirograph program.  This time it creates lines of
 * any randomly chosen color.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: MoreColorfulSpirograph.java 1517 2011-01-31 21:10:26Z terescoj $
 */

public class MoreColorfulSpirograph extends WindowController {

    // first coordinate of a line segment
    private Location linesStart;

    // color of next spriograph
    private Color currentColor;

    // generator for random color values
    private RandomIntGenerator randomIntensity = new RandomIntGenerator(0, 255);

    /*
     * Save the first coordinate of the line, and pick
     * a new color at random.
     */
    public void onMousePress(Location point) {

        linesStart = point;

        // Create a new random color, with values from
        // 0..255 for the Red, Green, and Blue parts
        currentColor = new Color(randomIntensity.nextValue(), 
                                 randomIntensity.nextValue(),
                                 randomIntensity.nextValue());

    }

    public void onMouseDrag(Location point) {
        new Line(linesStart, point, canvas).setColor(currentColor);
    }

    // now also clear the canvas when we reenter the window
    public void onMouseEnter(Location pt) {
        canvas.clear();
    }
}