Computer Science 120
Introduction to Programming

Spring 2011, Siena College

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

Before you know it, the snow will melt and the flowers will bloom. But for now, we'll rely on you to write a program to display 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.

Lab Preparation

To make our limited lab time as productive as possible, please do the following before coming to your lab meeting:

  1. Read the lab description carefully.
  2. Prepare a written design for your program, similar to the ones you have developed for previous labs.

    As part of the design, you should also draw the layout for one of the flowers your program will generate, including the graphical objects that it will include and labelling important dimensions and locations.

  3. Do not start coding before lab. 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 come to lab 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, but as you drag the mouse around, it should grow. 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.

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 constructor should expect three parameters: the Location where the Flower's stem should be planted, a double specifying the maximum height of the Flower, and the canvas. The Flower class should define the following methods used by the controller to implement the functionality described above:

  1. 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.
  2. public boolean flowerContains(Location point): returns true if the petals or center of the flower contains the point.
  3. 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 two bonus points may be awarded 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 hw4, (ii) print a copy of your program and hand it to your lab instructor, and (iii) demonstrate the execution of your program for your instructor.