Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

RubberBand Demo

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



RubberBand BlueJ Project

Click here to download a BlueJ project for RubberBand.


RubberBand Source Code

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


RubberBand.java

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

/*
 * Example RubberBand
 *
 * A simple program to draw a rubber band line.
 * This version creates a single line segment and
 * the changes the end point each time the mouse moves.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013
 * Original from Williams College, CSCI 134
 *
 * $Id: RubberBand.java 1501 2011-01-24 20:48:50Z terescoj $
 */

public class RubberBand extends WindowController {

    // the line being drawn
    private Line line;

    public void onMousePress(Location pressedPoint) {
        
        // when the mouse is depressed, we draw a "degenerate"
        // line from the press point to the press point (a point!)
        // and remember it in the "line" instance variable
        line = new Line(pressedPoint, pressedPoint, canvas);
    }

    public void onMouseDrag(Location point) {
        
        // then when we drag, we take replace the previous end
        // point of the line with the current mouse position -
        // the last "drag" point before the release will be
        // the final endpoint for the line
        line.setEnd(point);
    }
}