CS 102 Homework Laboratory # 8
Scribbler

Objective: To gain more experience using recursion and recursive data structures.

Introduction

This lab is intended to give you more experience with recursion, as well as GUIs. You will implement a recursive data structure to manage a collection of Scribble objects.

General requirements

You're required to follow these naming conventions:

This week, you will be implementing a program we call "Scribbler." You have seen this program in class as a demo, but here's the demo again.




The image above will be a working version of the Scribbler if your web browser supports Java applets. You should experiment with this demonstration version after you finish reading the lab handout to make sure you understand exactly what your program is supposed to do.

Your finished Scribbler program will have three modes: Draw mode, Move mode, and Color mode. The program starts in Draw mode. Draw mode, Move mode, and Color mode are selected by pressing buttons, and the color used by Color mode is selected by choosing a color from a combo box component filled with ColorItem objects. The modes behave as follows:

Draw mode:
Drag to draw a new Scribble on the canvas.
Move mode:
Drag a Scribble by pressing the mouse on it and dragging.
Color mode:
Click on any Scribble to change its color to that selected in the color choice component.

At any time the user may select a new color, and that color is used for subsequent drawing in Draw mode as well as for changing the color in Color mode.

The program also has an "Erase Last" button that will erase the most recently drawn Scribble. Pressing the button repeatedly will erase Scribbles in the reverse order in which they were drawn. (With what we know so far, there's no easy way to erase an arbitrary scribble that the user points to. You're welcome to implement this if you know how.)

Getting started

To start,

Click this link (while holding the Shift key) to download the Scribble starter script to your home folder.

Now open a terminal window and type

source initScribbler
As a result, there will now be a CS102/lab8 folder containing the following files. (You can also download these files using your browser by clicking on the links below:)

How to Proceed

The starter for this lab is a working Scribbler (try it out by compiling everything except the scribble collection classes and interface), but it has only the Draw mode and a simplified Move mode that can move only the most recently drawn Scribble. You will need to add Color mode. More importantly you must implement a recursive data structure that manages a collection of Scribbles, to allow the various modes and the "Erase last" button to work correctly as in the demo.

There are a number of step-by-step approaches you could take to solve this problem, but it is important that you have a plan, and that you add functionality one step at a time. Here is one possible ordering of the tasks. We recommend that you develop and test your program incrementally - make sure you have a working implementation at each step before moving on to the next.

  1. Implement a simplified Color mode. This is similar to Move mode, except that you set the color of the most recently drawn Scribble, if it contains the mouse point. To do this, you will have to add a setColor method to the ScribbleIfc interface, and the Scribble and EmptyScribble classes. You'll also have to add a listener for the color mode button, and add code to the actionPerformed method that handles the button press. For now, you will only be able to recolor the most recently drawn Scribble. (This will change when you implement collections of Scribbles.)
  2. Implement a simplified Erase button. Here, you respond to the "Erase last" button's actionPerformed event by deleting the most recently drawn Scribble from the canvas. For now, you will only be able to erase the most recently drawn Scribble. A second button press will do nothing. As in step 1, you'll need to modify both the scribble data structure and the controller code.
  3. Now, in order to get the full functionality you see in the demo above, you must add the recursive data structure to represent a collection of Scribbles. This will allow the user to select any scribble for moving or coloring, and to erase all the scribbles from newest to oldest. The starter files include skeleton versions of the interface (ScribbleCollectionIfc) and classes (EmptyScribbleCollection and
    ScribbleCollection) you need. These are similar to the Scribble data structure itself, as well as the URL lists in the text.

    Since your Scribbler class needs to manage a collection of scribbles, which may be empty (an EmptyScribbleCollection) or not (a ScribbleCollection), it needs an instance variable with type ScribbleCollectionIfc. In your begin method, you should initialize it with an object created from EmptyScribbleCollection.

    To see how to add a new scribble to the collection, proceed by analogy with the way Scribbler adds a new line to the scribble (see onMouseDrag). Especially notice that there's no way to alter an existing scribble. To "add" a line to the scribble we have, we really create a new scribble, passing it the line and the old scribble.

    You will add new Scribbles to your ScribbleCollection in a similar way. Careful though: should you add a new Scribble to the collection in onMouseDrag? Or in onMouseRelease? Or where?

    How will the controller class use the constructors and methods defined for scribble collections?

Turning in your program

Before turning in your work, be sure to double check both its logical organization and your style of presentation. Make your code as clear as possible and include appropriate comments describing major sections of code and declarations. Remove the comments that no longer make sense because they were provided as guidance for you. Be sure that your code adheres to the naming conventions. Also, don't forget to write your name in all the files you are submitting.

Hand in your program in the usual way: make sure all your java files are in your-home-folder/CS102/lab8. If not, copy them there, and compile and run your program one last time in that folder to make sure that everything works properly.

To submit your files go to moodle in the CS102 course page and the Lab 8 section. There will be separate place to upload for each file. Three questions for you to answer are also provided below, which should be answered in the third activity provided in the moodle under lab 8.

  1. What was the important idea that you should have learned from this lab.
  2. Did the lab effectively teach you this idea.
  3. What questions do you still have about the material presented in this lab.

Good luck and have fun!

Grading Point Allocations

Value Feature
Design preparation (3 points total)
1.5 points Simplified color mode
1.5 points Erase mode
Style (5 points total)
2 points Descriptive comments
1 point Good names
1 point Good use of constants
1 point Appropriate formatting
Design (6 points total)
1 point Good use of boolean expressions, loops, conditionals
1 point Not doing more work than necessary
1 point Use of most appropriate methods
1 point Correct use of ColorItem class
2 points Appropriate recursive structure in ScribbleCollection
Correctness (6 points total)
1 point Switching among modes correctly
1 point Draw mode adds correctly to the ScribbleCollection
1 point Move mode works correctly
1 point Color mode works correctly
1 point Erase button works correctly
1 point Color selection works correctly

Three feedback questions (4 pts total)


This lab is based on materials developed by the department of Computer Science at Williams College.