Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

CrazyColorfulSpiralLines Demo

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



CrazyColorfulSpiralLines BlueJ Project

Click here to download a BlueJ project for CrazyColorfulSpiralLines.


CrazyColorfulSpiralLines Source Code

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


CrazyColorfulSpiralLines.java

import objectdraw.*;
import java.awt.*;
import java.util.Random;

/*
 * Example CrazyColorfulSpiralLines
 *
 * Yet another spiral lines program.  This time it creates lines of
 * any randomly chosen color, and new colors for each line, not for
 * each spiral
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Modified for CSC 252, The College of Saint Rose, Fall 2013, Spring 2014, Spring 2015
 * Based on example from Williams College CSCI 134
 *
 * $Id: CrazyColorfulSpiralLines.java 2523 2015-01-16 21:36:54Z terescoj $
 */

public class CrazyColorfulSpiralLines extends WindowController {

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

    // generator for random color values
    private Random randomIntensity = new Random();

    /*
     * 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. Note that this can (and should)
        // be a local variable in this version of the program since we
        // create and use the color here, and have no need to remember
        // it for any successive calls to this or any other method.
        Color currentColor = new Color(randomIntensity.nextInt(256), 
                randomIntensity.nextInt(256),
                randomIntensity.nextInt(256));
        new Line(linesStart, point, canvas).setColor(currentColor);
    }

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