Now it is time for you to write a class Flower that implements PlantInterface. The constructor should take parameters that indicate the x and y coordinates (as integers) of the base of the flower, a parameter of type Image representing the image of the flower, and a parameter of type Component that represents the canvas for drawing.
The constructor should set the initial height of the flower to be 80 and its width to be 64 (use constants that are doubles!). The initial growth rate should be 4. For each day that the plant grows (via the grow method), the height should increase by the growth rate (i.e., two days growth should result in twice as much change as one day). The width of the flower does not change as it grows. Rain has the effect of increasing the growth rate by inches/2 times the current growth rate. A frost kills the plant, resulting in setting the growth rate to 0, and the height to 2.
The draw method should look something like:
public void draw(Graphics g) { g.drawImage(plantImage,x,y,wd,ht,canvas); }where x and y represent the upper left hand corner of the image, while wd and ht are the desired width and height of the image. Note that all of these must be ints. If they are computed as doubles then they must be cast to ints. I strongly suggest that you also include a toString() method that indicates that it is a flower and what its height and width are. Test your program by using your flower class with the following driver class:
import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * @author kim * * Program to create and grow a flower */ public class StartIt extends JFrame implements ActionListener { // Size of the window private static final int CANVAS_HEIGHT = 600; private static final int CANVAS_WIDTH = 800; // Flower to be grown on canvas private Flower myFlower; // number of days flower since flower was created private int day = 0; // Timer to trigger animation private Timer gardenTimer; // Create a flower and a timer to trigger its growth public StartIt() { super("Gardens"); Image flowerImage = getCompletedImage("flower.jpg"); myFlower = new Flower(100,400,flowerImage,this); repaint(); gardenTimer = new Timer(1000, this); gardenTimer.start(); } /** * Retrieve an image from file "filename", return the * Image when it has loaded completely. * @param the name of the file containing the image * @return the loaded Image. */ private Image getCompletedImage(String fileName) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image myImage = toolkit.getImage(fileName); MediaTracker mediaTracker = new MediaTracker(this); mediaTracker.addImage(myImage, 0); try { mediaTracker.waitForID(0); } catch (InterruptedException ie) { System.out.println(ie); System.exit(1); } return myImage; } // When the timer ticks, perform the appropriate change to // the flower corresponding to the day number. public void actionPerformed(ActionEvent evt) { day++; switch (day) { case 3: rain(5); System.out.println("raining"); break; case 10: frost(); break; case 18: gardenTimer.stop(); System.out.println("stopped"); break; default: grow(); } repaint(); } // Change the flower to reflect the effects of the frost private void frost() { System.out.println("frost hits at time " + day); myFlower.frost(); } // Change the growth rater of the flower to reflect the // effects of the inches of rain, and then grow private void rain(int inches) { System.out.println("raining " + inches + " inches at time " + day); myFlower.rain(inches); myFlower.grow(); } // Grow by the amount expected in one day. public void grow() { System.out.println("growing at time " + day); myFlower.grow(); } @Override // paint the flower to show the changes public void paint(Graphics g) { super.paint(g); myFlower.draw(g); } // Create the window (JFrame) and show it. public static void main(String[] args) { StartIt sit = new StartIt(); sit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); sit.setSize(CANVAS_WIDTH, CANVAS_HEIGHT); sit.setVisible(true); } }
Make sure this program is in the same folder as your Flower class. You can compile them both by "cd"ing to that folder in a terminal window and then typing:
javac *.java java StartItMake sure that the results you get are correct.
If you are wondering about the
@Overrideline, you may want to consult this tutorial on Annotations.
Be sure to document your class with appropriate comments, including a general description at the top of the file and a description of what each method and constructor does. Comments for most methods can be copied from the interface comments. Be sure to use descriptive variable names and provide a comment on all variables and constants to make clear what they stand for. The code in your constructor and method bodies typically will not need comments unless there is something tricky going on that needs extra explanation. Do not add comments that simply repeats what is obvious in the code.