Welcome to Dream.In.Code
Become a Java Expert!

Join 149,617 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




GUI problem

 
Reply to this topicStart new topic

GUI problem

QuietRiot
20 Sep, 2007 - 08:06 AM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 4


My Contributions
Hi, there. I'm new here and also new to programming so I was hoping you guys could help me out. I'm doing an assignment for a class where I have to make a GUI program that will simulate the growth of a flower. I have written the following code:

CODE

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Component;

public class Flower implements PlantInterface
{
     private int x;
     private int y;
     private Image flowerImage;
     private static final double INITIAL_HEIGHT = 80;
     private static final double INITIAL_WIDTH = 64;
     private int growthRate = 4;
     private int wd;
     private int ht;
     private Component canvas;
    
    /**
     * Constructor for objects of class Flower
     */
    public Flower(int x, int y, Image p, Component c)
    {
       this.x = x;
       this.y = y;
       flowerImage = p;
       canvas = c;
        
    }
    
    
    // modify the size of the plant to reflect growing "days" days.
    public void grow() {
        ht = ht + growthRate;
    }

    // modify the rate of growth of the plant to reflect "inches" of rain.
    public void rain(double inches) {
        ht += (growthRate) * (inches/2);
    }

    // modify the size of the plant to reflect the effects of a frost.
    public void frost() {
        ht = 2;
        growthRate = 0;
    }
    
    // display the plant with its current location and size.
    public void draw(Graphics g) {
        g.drawImage(flowerImage,x,y,wd,ht,canvas);
    }
    
    public String toString (String info) {
        info = "The flower's height is " + ht + " and its width is"
        + wd;
        return info;
    }

}


and we're supposed to test it with the following driver class:



CODE

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.*;
/**
*  
* 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);
   }
}



I'm having a problem getting the flower to actually show up in the JFrame (ie. nothing is there when you run StartIt). The text part of the program works as intended. It is printing the status of the flower each "day." Please have a look and let me know if you can figure this out.

Also, is my toString method in class Flower correct? It is simply supposed to indicate the flower's height and width, although I'm not sure what actually calls the toString in this program.

Thanks a lot.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: GUI Problem
20 Sep, 2007 - 01:07 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well, you certainly have an interesting project here. Never seen one like it before. Keep up the good work. As for your questions, I have your solutions. Expect any less from the coding ninjas of DIC? So here is the deal...it is pretty simple fix.

In your flower class you have your width and height variables. Did you notice that you never really set the width value? You don't set your height either, at first, but then increment it later. Unfortunately you don't touch your width. So your image is there, but it is 0 pixels in width. wink2.gif

Just to see what I mean, go to your flower class and initialize your wd variable to something like 100. Then run your app and voila! By the DIC gods you will see your image... unless they don't like you and in that case you won't see it. But, that should fix your disappearing graphic.

Next, your toString() function is a bit unconventional. Usually the toString() method doesn't take a parameter and returns the string representation of the object. You would use it any time you want to get the string of what it means to be the flower. You could also use it for things like storing in a List class etc. Your project doesn't currently use the feature but you could build it in with something like in your Grow() function. Check out a sample below...

CODE

// Grow by the amount expected in one day.
public void grow() {
      System.out.println("growing at time " + day);
      myFlower.grow();

      // Use our toString function to get the dimensions of the flower
      System.out.println("Grow message: " + myFlower.toString());
}


So in order to represent your object for instance you could simply use the toString() listed below...

CODE

// Return a string message showing the height and width of the flower.
public String toString() {
      return "The flower's height is " + ht + " and its width is " + wd;
}


So once you fix the width issue and the string issue, you will be on your way.

I did want to point out something else to you. During your growth, when it hits the frost it pretty much kills the height of the flower. I assume you want to grow again right? Well once you hit the frost, your ht variable no longer seems to grow (because your growthrate is now 0). Unless you meant to do that, you will want to set that back to 4. This is of course assuming that you are not demonic and want all flowers to die! If you want it to die, by all means, carry on.

Anyways, there you go. Enjoy!

At DIC we be coding ninjas! ph34r.gif

This post has been edited by Martyr2: 20 Sep, 2007 - 01:12 PM
User is offlineProfile CardPM
+Quote Post

QuietRiot
RE: GUI Problem
20 Sep, 2007 - 03:10 PM
Post #3

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 4


My Contributions
Thanks a lot Martyr2. I finally got it working. And about the frost, the prof told us to kill the flower lol. tongue.gif It isn't supposed to grow again once it's dead hehe. Thanks again.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month