import objectdraw.*; import java.util.Random; import java.awt.Color; /** * Program to grow a garden of broccoli. */ public class BroccoliGarden extends FrameWindowController { // dimensions of the window private static final int WINDOW_WIDTH = 1200; private static final int WINDOW_HEIGHT = 700; // color and position of the scene elements private static final Color GROUND_COLOR = new Color(80, 50, 50); private static final int GROUND_HEIGHT = 200; private static final Color SKY_COLOR = new Color(160, 230, 255); private static final int SUN_SIZE = 100; private static final Location SUN_LOC = new Location(100, 80); // range of broccoli tree stem lengths private static final double MAX_STEM_LENGTH = 120; private static final double MIN_STEM_LENGTH = 40; // random stem length generator private Random rand = new Random(); // the ground from which broccoli grows private FilledRect ground; // draw the scene public void begin() { resize(WINDOW_WIDTH, WINDOW_HEIGHT); ground = new FilledRect(0, canvas.getHeight() - GROUND_HEIGHT, canvas.getWidth(), GROUND_HEIGHT, canvas); ground.setColor(GROUND_COLOR); FilledRect sky = new FilledRect(0, 0, canvas.getWidth(), canvas.getHeight() - GROUND_HEIGHT, canvas); sky.setColor(SKY_COLOR); FilledOval sun = new FilledOval(SUN_LOC, SUN_SIZE, SUN_SIZE, canvas); sun.setColor(Color.YELLOW); } // plant some more broccoli public void onMousePress(Location point) { if (ground.contains(point)) { double nextLength = rand.nextDouble() * (MAX_STEM_LENGTH - MIN_STEM_LENGTH) + MIN_STEM_LENGTH; new GrowingBroccoli(point, nextLength, Math.PI / 2.0, canvas); } } }