Computer Science 252
Problem Solving with Java

Fall 2013, The College of Saint Rose

VanishingScribble Demo

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



VanishingScribble BlueJ Project

Click here to download a BlueJ project for VanishingScribble.


VanishingScribble Source Code

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


VanishingScribble.java

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

/*
 * Example VanishingScribble: draw using lines that either fall off the canvas
 * or fade away.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 *
 * $Id: VanishingScribble.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class VanishingScribble extends WindowController {

    // random generator to pick line types
    private Random whichType = new Random();
    
    // the current line type: 0=fading, 1=falling, 2=rising
    private int lineType;

    // location of last mouse position to draw scribble components
    private Location lastMouse;

    // pick a type of line (fading or falling) for this scribble
    // and remember starting point
    public void onMousePress(Location point) {

        lineType = whichType.nextInt(3);
        lastMouse = point;
    }

    // draw a segment of the appropriate type of line then update lastMouse
    public void onMouseDrag(Location point) {

        if (lineType == 0) {
            new FadingLine(lastMouse, point, canvas);
        }
        else if (lineType == 1) {
            new FallingLine(lastMouse, point, canvas);
        }
        else {
            new RisingLine(lastMouse, point, canvas);
        }
        lastMouse = point;
    }
}

FadingLine.java

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

/*
 * Fading line segments for the VanishingScribble example
 * 
 * A simple active object that controls a line that fades from black
 * through shades of grey, then is removed from the canvas when it becomes
 * white
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 *
 * $Id: FadingLine.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class FadingLine extends ActiveObject {

    // amount and speed of the fade
    private static final double FADE_BY = 2;
    private static final int DELAY_TIME = 33;
    private static final int INITIAL_DELAY = 1000;

    // the line controlled by this instance
    private Line line;

    // Draw a line and start it falling.
    public FadingLine(Location start, Location end, DrawingCanvas aCanvas) {

        // draw the line at its initial position
        line = new Line(start, end, aCanvas);

        // activate!
        start();
    }

    // change the line's color periodically so it fades to white
    public void run() {

        pause(INITIAL_DELAY);
        int hue = 0;
        while (hue < 255) {
            line.setColor(new Color(hue, hue, hue));
            pause(DELAY_TIME);
            hue += FADE_BY;
        }
        line.removeFromCanvas();
    }
}

FallingLine.java

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

/*
 * Falling line segments for the VanishingScribble example
 * 
 * A simple active object that controls a line that falls down the
 * canvas and disappears when it goes off the end.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 * 
 * $Id: FallingLine.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class FallingLine extends ActiveObject {

    // size and speed of falling lines
    private static final double INITIAL_Y_SPEED = 0.0001;
    private static final int DELAY_TIME = 33;
    private static final int INITIAL_DELAY = 100;

    // the line controlled by this instance
    private Line line;

    // how far to fall before stopping and disappearing?
    private double yMax;

    // Draw a ball and start it falling.
    public FallingLine(Location start, Location end, DrawingCanvas aCanvas) {

        // draw the line at its initial position
        line = new Line(start, end, aCanvas);

        // ask the canvas how big it is so we know when to stop
        yMax = aCanvas.getHeight();

        // activate!
        start();
    }

    // move the line repeatedly until it falls off screen
    public void run() {

        pause(INITIAL_DELAY);

        // start out with a slow fall, then accelerate
        double ySpeed = INITIAL_Y_SPEED;

        while ((line.getStart().getY() <= yMax) ||
        (line.getEnd().getY() <= yMax)) {
            line.move(0, ySpeed);
            ySpeed = ySpeed * 1.1;
            pause(DELAY_TIME);
        }
        line.removeFromCanvas();
    }
}

RisingLine.java

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

/*
 * Rising line segments for the VanishingScribble example
 * 
 * A simple active object that controls a line that falls up the
 * canvas and disappears when it goes off the end.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 *
 * $Id: RisingLine.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class RisingLine extends ActiveObject {

    // size and speed of falling lines
    private static final double INITIAL_Y_SPEED = 0.0001;
    private static final int DELAY_TIME = 33;
    private static final int INITIAL_DELAY = 100;

    // the line controlled by this instance
    private Line line;

    // Draw a ball and start it falling.
    public RisingLine(Location start, Location end, DrawingCanvas aCanvas) {

        // draw the line at its initial position
        line = new Line(start, end, aCanvas);

        // activate!
        start();
    }

    // move the line repeatedly until it falls off screen
    public void run() {

        pause(INITIAL_DELAY);

        // start out with a slow fall, then accelerate
        double ySpeed = -INITIAL_Y_SPEED;

        while ((line.getStart().getY() >= 0) ||
        (line.getEnd().getY() >= 0)) {
            line.move(0, ySpeed);
            ySpeed = ySpeed * 1.1;
            pause(DELAY_TIME);
        }
        line.removeFromCanvas();
    }
}