Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

ColorfulSpiralLines Demo

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



ColorfulSpiralLines BlueJ Project

Click here to download a BlueJ project for ColorfulSpiralLines.


ColorfulSpiralLines Source Code

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


ColorfulSpiralLines.java

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

/*
 * Example ColorfulSpiralLines: adding some randomly chosen
 * colors to our Spiral line drawings.
 *
 * 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: ColorfulSpiralLines.java 2523 2015-01-16 21:36:54Z terescoj $
 */

public class ColorfulSpiralLines extends WindowController {

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

    // color of next sprial
    private Color currentColor;

    // random number generator -- created here since there is no
    // need to construct one each time the onMousePress method
    // is called
    private Random pickAColor = new Random();

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

        // since we only need colorNumber within this method, it
        // is declared as a local variable.
        // once we use it here to choose currentColor, we no
        // longer need to remember its value
        int colorNumber = pickAColor.nextInt(4);

        switch (colorNumber) {
            case 0:
            currentColor = Color.red;
            break;
            case 1:
            currentColor = Color.blue;
            break;
            case 2:
            currentColor = Color.magenta;
            break;
            default:
            currentColor = Color.green;
            break;
        }
    }

    /*
     * As the user is dragging draw a line from
     * initial point where mouse pressed to current point.
     */
    public void onMouseDrag(Location point) {
        new Line(linesStart, point, canvas).setColor(currentColor);
    }
}