Computer Science 252
Problem Solving with Java

Fall 2013, The College of Saint Rose

SimplestRecScribbler Demo

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



SimplestRecScribbler BlueJ Project

Click here to download a BlueJ project for SimplestRecScribbler.


SimplestRecScribbler Source Code

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


SimplestRecScribbler.java

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

/*
 * This program is a first step toward a drawing program that will 
 * be much more functional than our simple Scribble example.
 * Here, we construct a recursive data structure to represent
 * a Scribble.
 *
 * Jim Teresco, CSC 252, The College of Saint Rose, Fall 2013
 * Based on similar example from Williams College CS 134.
 *
 * $Id: SimplestRecScribbler.java 2230 2013-10-27 02:26:10Z terescoj $
 */

public class SimplestRecScribbler extends WindowController {

    // the current scribble
    private Scribble currentScribble;

    // stores last point for drawing or dragging
    private Location lastPoint;

    // start a new (empty) scribble for drawing
    //   and remember point for drawing later
    public void onMousePress(Location point) {
        currentScribble = new Scribble(null, null);
        lastPoint = point;
    }

    // add new line to current scribble
    public void onMouseDrag(Location point) {
        currentScribble = new Scribble(new Line(lastPoint, point, canvas), currentScribble);
        lastPoint = point;
    }
}

Scribble.java

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

/*
 * A very basic recursive data structure to represent
 * a Scribble.
 *
 * Jim Teresco, CSC 252, The College of Saint Rose, Fall 2013
 * Based on similar example from Williams College CS 134.
 * 
 *
 * $Id: Scribble.java 2230 2013-10-27 02:26:10Z terescoj $
 */

public class Scribble {
    private Line first;         // an edge line of the scribble
    private Scribble rest;      // the rest of the scribble

    // Our constructor here doesn't actually do any of the
    // drawing - it takes a line that someone else created and
    // which is supposed to be part of the Scribble, and
    // a reference to another Scribble which represents
    // any previous Scribble.
    public Scribble(Line segment, Scribble theRest) {
        first = segment;
        rest = theRest;
    }
}