Computer Science 120
Introduction to Programming

Fall 2011, Siena College

CrazyColorfulSpirograph Demo

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



CrazyColorfulSpirograph BlueJ Project

Click here to download a BlueJ project for CrazyColorfulSpirograph.


CrazyColorfulSpirograph Source Code

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


CrazyColorfulSpirograph.java

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

/*
 * Example CrazyColorfulSpirograph
 *
 * Yet another spirograph program.  This time it creates lines of
 * any randomly chosen color, and new colors for each line, not for
 * each spirograph
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: CrazyColorfulSpirograph.java 1501 2011-01-24 20:48:50Z terescoj $
 */

public class CrazyColorfulSpirograph 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, but don't worry
     * about colors yet for this one.
     */
    public void onMousePress(Location point) {
  
        linesStart = point;
    }

    public void onMouseDrag(Location 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());
        new Line(linesStart, point, canvas).setColor(currentColor);
    }

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