Computer Science 252
Problem Solving with Java

Fall 2013, The College of Saint Rose

BroccoliBetter Demo

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



BroccoliBetter BlueJ Project

Click here to download a BlueJ project for BroccoliBetter.


BroccoliBetter Source Code

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


BroccoliBetter.java

/**
 * Program to create a recursive graphics which looks like broccoli.
 * Written 11/4/99 by Kim Bruce
 * Modified 3/12/02 by Andrea Danyluk
 * 
 * Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
 * 
 * $Id: BroccoliBetter.java 2244 2013-11-06 04:54:22Z terescoj $
 */

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

public class BroccoliBetter extends WindowController {

    private Location lastCoords;  // Where mouse was before last move
    private BroccoliBranch plant; // Broccoli object to be drawn & moved
    private boolean broccoliPressed; // whether the brocolli has been pressed

    /*
     * Create the broccoli
     */
    public void begin() {
        plant = new BroccoliBranch(new Location(200,350), 80.0,
            Math.PI/2.0, canvas);
    }

    // Get ready to move broccoli
    public void onMousePress(Location pt) {

        lastCoords = pt;
        broccoliPressed = plant.contains(pt);
    }

    // Drag the broccoli around
    public void onMouseDrag(Location pt) {

        if (broccoliPressed) {
            plant.move(pt.getX()-lastCoords.getX(), pt.getY()-lastCoords.getY());
            lastCoords = pt;
        }
    }

}

AngLine.java

/*
 * AngLine.java     Jonathan Kallay 7/14/99
 * (c) 1999 Williams College
 */
import java.awt.*;
import objectdraw.*;

/**
 * AngLine is an implementation of a drawable line object.
 *
 * @author Jonathan Kallay
 * @version 1.0 last update 8/25/99
 * 
 * Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
 * 
 * $Id: AngLine.java 2244 2013-11-06 04:54:22Z terescoj $
 */
public class AngLine extends Line implements LineInterface {
    private double length;
    private double angle;

    /**
     * Creates a new AngLine object.
     * @param start the starting point of the line.
     * @param dest the destination point of the line.
     * @param c the canvas in which the line is created.
     */
    public AngLine(Location start, double length, double radianAngle, DrawingCanvas canvas){
        super(start,new Location(start.getX() + length*Math.cos(radianAngle),
                start.getY() - length*Math.sin(radianAngle)),canvas);
        this.length = length;
        angle = radianAngle;
    }

    public Location getDest() {
        return new Location(start.getX() + length*Math.cos(angle),
            start.getY() - length*Math.sin(angle));
    }

}


BroccoliBranch.java

/**
 * Class to recursively draw broccoli
 * 
 * Williams College CSC 134
 * Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
 * 
 * $Id: BroccoliBranch.java 2244 2013-11-06 04:54:22Z terescoj $

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

public class BroccoliBranch implements BroccoliPart {
    // dark green color for broccoli
    private static final Color BROCCOLI_COLOR = new Color(0,135,0);

    // Below this size draw flower
    private static final double MINSIZE = 25.0;

    // How much broccoli shrinks each call
    private final static double TOP_FRACT = 0.8;

    // stem of broccoli
    private AngLine stem;

    // branches of broccoli
    private BroccoliPart left, center, right;

    /**
     * Draw broccoli by recursively drawing branches (and flower)
     */
    public BroccoliBranch(Location startCoords, double size,
    double direction, DrawingCanvas canvas)
    {
        // Draw stem and color green
        stem = new AngLine(startCoords, size, direction, canvas);
        stem.setColor(BROCCOLI_COLOR);

        Location destCoords = stem.getEnd();    // end of stem

        if (size > MINSIZE) {
            left = new BroccoliBranch(destCoords, size * TOP_FRACT,
                direction + Math.PI/9.0, canvas);
            center = new BroccoliBranch(destCoords, size * TOP_FRACT,
                direction, canvas);
            right = new BroccoliBranch(destCoords, size * TOP_FRACT,
                direction - Math.PI/9.0, canvas);
        } 
        else {
            left = new Flower(destCoords, size * TOP_FRACT,
                direction + Math.PI/9.0, canvas);
            center = new Flower(destCoords, size * TOP_FRACT,
                direction, canvas);
            right = new Flower(destCoords, size * TOP_FRACT,
                direction - Math.PI/9.0, canvas);
        }

    }

    /**
     * @param x,y amount to move broccoli
     */
    public void move(double x, double y) {

        stem.move(x,y);                 // move stem

        left.move(x,y);                 // move other parts
        center.move(x,y);
        right.move(x,y);
    }

    /**
     * @param point location to be checked for containment in broccoli
     */
    public boolean contains(Location point) {

        return (stem.contains(point) || left.contains(point) ||
            center.contains(point) || right.contains(point));
    }

}

BroccoliPart.java


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

/*
 * BrocolliPart is an interface for pieces of broccoli.
 * 
 * Williams College CSC 134
 * Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
 * 
 * $Id: BroccoliPart.java 2244 2013-11-06 04:54:22Z terescoj $

 */
public interface BroccoliPart {
    
    // amount to move the broccoli part
    public void move(double x, double y);

    // whether a point is contained in the broccoli part
    public boolean contains(Location point);
}

Flower.java

/**
 * Class to draw a broccoli flower
 * 
 * Williams College CSC 134
 * 
 * Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
 * 
 * $Id: Flower.java 2244 2013-11-06 04:54:22Z terescoj $
 */
import objectdraw.*;
import java.awt.*;

public class Flower implements BroccoliPart {

    private static final Color BROCCOLI_COLOR = new Color(0,135,0);
    private static final int BUD_SIZE = 3;  // diameter of bud

    private AngLine stem;                 // stem of broccoli

    private FilledOval bud;       // flower of broccoli plant

    /**
     * Draw flower
     */
    public Flower(Location startCoords, double size,
    double direction, DrawingCanvas canvas) {
        // Draw stem and color green
        stem = new AngLine(startCoords, size, direction, canvas);
        stem.setColor(BROCCOLI_COLOR);

        Location destCoords = stem.getEnd();    // end of stem

        bud = new FilledOval(destCoords,BUD_SIZE,BUD_SIZE,canvas);
        bud.setColor(Color.yellow);
    }

    /**
     * @param x,y amount to move flower
     */
    public void move(double x, double y) {
        
        // move stem
        stem.move(x,y);

        // move bud
        bud.move(x,y);
    }

    /**
     * @param point location to be checked for containment in flower
     */
    public boolean contains (Location point) {
        
        return stem.contains(point) || bud.contains(point);
    }

}

LineInterface.java

import objectdraw.*;
/*
 * Interface for objects that have an endpoint
 * 
 * Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
 * 
 * $Id: LineInterface.java 2244 2013-11-06 04:54:22Z terescoj $
 */

public interface LineInterface {

    // Return endpoin   t
    public Location getDest();
}