Computer Science 252
Problem Solving with Java

Fall 2013, The College of Saint Rose

RecWoodPile Demo

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



RecWoodPile BlueJ Project

Click here to download a BlueJ project for RecWoodPile.


RecWoodPile Source Code

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


RecWoodPile.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import objectdraw.*;

/*
 * Example of a program that allows a user to stack and
 * un-stack logs of wood.
 *
 * Jim Teresco, CSC 252, The College of Saint Rose, Fall 2013
 * Based on similar example from Williams College CS 134.
 *
 * $Id: RecWoodPile.java 2230 2013-10-27 02:26:10Z terescoj $
 */

public class RecWoodPile extends WindowController implements ActionListener {

    // dimensions of wood to be stacked
    private static final int WOOD_WIDTH = 100;
    private static final int WOOD_HT = 10;

    // the stacking displacement
    private static final int STACKING_DISP = 12;

    // the stack of wood
    private Stack theStack;

    // buttons that allow a user to select a mode (stacking or unstacking)
    private JButton stackEm, unStackEm;

    // the x and y locations of the next item of wood to be stacked
    private int x = 100;
    private int y = 340;

    // an image of wood
    private Image wood;

    public void begin() {
        // create the user interface components

        Container contentPane = getContentPane();
        stackEm = new JButton("Stack 'em");
        unStackEm = new JButton("Unstack 'em");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(stackEm);
        buttonPanel.add(unStackEm);

        contentPane.add(buttonPanel, BorderLayout.SOUTH);

        // add listeners
        stackEm.addActionListener(this);
        unStackEm.addActionListener(this);

        // the stack is initially empty
        theStack = new Stack(null, null);

        wood = getImage("wood.gif");
        
        contentPane.validate();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == stackEm) {
            // put a new piece of wood on the stack
            VisibleImage theWood = new VisibleImage(wood, x, y, canvas);
            theWood.setWidth(WOOD_WIDTH);
            theWood.setHeight(WOOD_HT);
            theStack = new Stack(theWood, theStack);
            y = y - STACKING_DISP;
        } else if (!theStack.isEmpty()) {
            VisibleImage theWood = theStack.removeMostRecent();
            theWood.removeFromCanvas();
            y = y + STACKING_DISP;
        }
    }
}

Stack.java

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

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

public class Stack {
    private VisibleImage first;   // the wood on the top of the stack
    private Stack rest;       // the rest of the stack

    public Stack(VisibleImage someWood, Stack theRest) {
        first = someWood;
        rest = theRest;
    }

    // pre:
    // post: returns true iff the stack is empty
    public boolean isEmpty() {
        return (first == null);
    }

    // pre: the stack is not empty
    // post: the first item is removed and is returned.
    public VisibleImage removeMostRecent()  {
        VisibleImage theFirst = first;

        first = rest.getFirst();
        rest = rest.getRest();

        return theFirst;
    }

    // pre:
    // post: returns the first item; returns null if the stack is empty
    public VisibleImage getFirst() {
        return first;
    }

    // pre:
    // pre: returns a stack containing all but the first item;
    //    returns null if the stack is empty
    public Stack getRest() {
        return rest;
    }
}