Computer Science 120
Introduction to Programming

Spring 2012, Siena College

Lab 4: Spring Flowers
Due: the start of your next lab session

Your task this week is to write a program that grows and displays some spring flowers.

When you press the mouse on the canvas, a small plant is drawn. As you drag the mouse, the plant grows slowly. When it reaches its full height, a festive spring flower appears. If you then click on that flower's petals before beginning to work on the next flower, its color will change.

You may work alone or with a partner on this lab.

Design Document

To get started, you will again develop a written design for this program. Before you write anything down, read this entire lab description carefully. Then develop a design that includes:

Do not start coding before completing this design and having it approved by your instructor. It is often more productive to design a program away from the computer, and you should get into the habit of working in this way. If you arrive at the keyboard with a written idea of how to approach the problem, you will be able to make progress much more quickly than you would otherwise.

To help you get started, we have provided a starter BlueJ project with the class header and some method stubs for the Flower class and a complete version of a WindowController extension named Spring that will manipulate the flowers on the screen using the constructor and methods of the Flower class.

Problem Description

The canvas should initially start empty. When you click on the canvas, a flower should start growing with its base centered at that point. Initially it will be just a sprout, the height of which you may choose to be either a constant value or a percentage of the ultimate height of the flower. As you drag the mouse, the stem should grow (upward, obviously). When it reaches its full height, the stem should stop growing and petals should appear. The color for the petals should be chosen randomly. The flower should not grow any more, but if you click on the petals after it has bloomed, the flower will change color. When you are happy with the flower's color, you can grow another one by clicking somewhere else in the window. When the mouse moves out of and then back into the window, the flowers should be removed from the canvas. Only the most-recently-created flower needs to respond to mouse clicks by changing its color.

If the mouse is released before the stem reaches its full height, the stem's growth stops and this plant will never produce a flower.

A working solution for this program will appear below. Click inside the applet to interact with it.

Your program will be divided into two classes: a window controller called Spring and a Flower class. A complete Spring class, which you should not modify, is provided in the starter folder. Your task is to implement the Flower class so that it works with our Spring. In particular, the Flower class needs to provide:

  1. A constructor that takes three parameters: the Location where the Flower's stem should be planted, a double specifying the maximum height of the Flower, and the canvas.
  2. A mutator method public void changeColor(): sets the color of the flower petals to a random color. This may be completely random, or you may restrict to colors that you think look nice.
  3. An accessor method public boolean flowerContains(Location point): returns true if the petals or center of the flower contains the point.
  4. A mutator method public void grow(): make the flower grow a bit if it has not already reached its full size and sprouted petals. Growing by about 1 for each mouse drag event is an appropriate rate of growth.

Your flowers should be at least as realistic as those in the demo solution. But feel free to use more graphics primitives in your Flower class to make the flowers look more attractive. Up to four bonus points are available for especially nice flowers.

Submitting Your Work

Before the start of your next lab session, submit your Java program for grading. There are three things you need to do to complete the submission: (i) place a copy of your Java program into your csis120/hw folder under hw7, (ii) print a copy of your program and hand it to your instructor, and (iii) demonstrate the execution of your program for your instructor.

Don't forget to check your programs for compliance with the Style Guide for CSIS 120 Programs


This assignment is worth 100 points, which are distributed as follows:

> FeatureValueScore
Initial Design
Sufficient detail to convey the basic design of Flower 8
Includes picture of flower layout with dimensions 4
Style, Design, and Efficiency
Appropriate comments 6
Good variable names 4
Appropriate variable declarations 6
Good use of constants 8
Appropriate formatting 2
Does not generate new objects unnecessarily 4
Good overall design and methods 6
Initial stem drawn properly 8
Stem grows while mouse is dragged 12
Growth stops at specified height 8
Flower appears when height is achieved 12
Color changes when most recent petals clicked 8
Canvas cleared on mouse enter 4
Extra Credit
Nicer flowers 4
Total 100