Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
Broccoli Demo
A working demo of Broccoli will appear below. Click inside the applet to interact with it.
Broccoli BlueJ Project
Click here to download a BlueJ project for Broccoli.
Broccoli Source Code
The Java source code for Broccoli is below. Click on a file name to download it.
/**
* Program to create a recursive graphics which looks like broccoli.
* Written 11/4/99 by Kim Bruce
* Modified 4/20/00 by Andrea Danyluk
*
* Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
*
* $Id: Broccoli.java 2244 2013-11-06 04:54:22Z terescoj $
*/
import objectdraw.*;
import java.awt.*;
public class Broccoli extends WindowController {
private static final double STEM_X = 200,
STEM_Y = 350,
STEM_LENGTH = 80;
private Location lastLocation; // Where mouse was before last mouse event
private BroccoliBranch plant; // Broccoli object to be drawn and moved
/**
* Create the broccoli
*/
public void begin() {
plant = new BroccoliBranch(new Location(STEM_X,STEM_Y), STEM_LENGTH, Math.PI/2.0, canvas);
}
// Get ready to move broccoli
public void onMousePress(Location pt) {
lastLocation = pt;
}
// Drag the broccoli around
public void onMouseDrag(Location pt) {
plant.move(pt.getX()-lastLocation.getX(), pt.getY()-lastLocation.getY());
lastLocation = pt;
}
}
/*
* 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));
}
}
/*
* Class to recursively draw broccoli
*
* From 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 {
private static final double TOP_FRACT = 0.8; // How much broccoli shrinks each call
private static final double GROWTH_LIMIT = 20.0; // How little a branch must be before broccoli stops expanding
private BroccoliBranch left, center, right; // branches of broccoli
private AngLine stem; // stem of broccoli
private FilledOval flower; // Flower of broccoli plant
private static final int FLOWER_SIZE = 3; // The size (width and height) of a flower
private boolean bigEnough; // Object big enough to have branches?
/**
* Draw broccoli by recursively drawing branches (and flower)
*/
public BroccoliBranch(Location startLocation, double size, double direction, DrawingCanvas canvas) {
stem = new AngLine(startLocation,size,direction,canvas); // Draw stem and color green
stem.setColor(Color.green);
Location destLocation = stem.getDest(); // end of stem
bigEnough = size > GROWTH_LIMIT;
if (bigEnough) { // Big enough to keep growing
left = new BroccoliBranch(destLocation, size * TOP_FRACT,
direction + Math.PI/9.0, canvas);
center = new BroccoliBranch(destLocation, size * TOP_FRACT,
direction, canvas);
right = new BroccoliBranch(destLocation, size * TOP_FRACT,
direction - Math.PI/9.0, canvas);
}
else { // draw flower when small enough
flower = new FilledOval(destLocation,FLOWER_SIZE,FLOWER_SIZE,canvas);
flower.setColor(Color.yellow);
}
}
/**
* @param x,y amount to move broccoli
*/
public void move(double x, double y) {
stem.move(x,y); // move stem
if (bigEnough) { // move rest of broccoli - three branches
left.move(x,y);
center.move(x,y);
right.move(x,y);
}
else { // move flower
flower.move(x,y);
}
}
}
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();
}