Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Why are my flowers stacked on top of each other?

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Why are my flowers stacked on top of each other?

    Hello All,

    I am trying to write a program that draws 6 flowers next to each other. I can't figure out why my flowers are stacked on top of each other. Any advice would be greatly appreciated. Thank you!tester.zip

    Turtle Class:

    import java.awt.*;
     
    public class Turtle extends Turtlet
    {
    private static TurtleWorld theWorld = null;
     
     
    /** Write words without changing the Turtle's state. */
     
    public void say (String message)
    { super.say (message);
    theWorld.repaint();
    } //======================
     
     
    /** Make a circle of the given radius, Turtle at center. */
     
    public void swingAround (double radius)
    { super.swingAround (radius);
    theWorld.repaint();
    } //======================
     
     
    /** Fill a circle of the given radius, Turtle at center. */
     
    public void fillCircle (double radius)
    { super.fillCircle (radius);
    theWorld.repaint();
    } //======================
     
     
    /** Rotate left by left degrees; MOVE for forward steps.*/
     
    public Turtlet move (double left, double forward)
    { return super.move (left, forward);
    } //======================
     
     
    /** Rotate left by left degrees; PAINT for forward steps. */
     
    public Turtlet paint (double left, double forward)
    { super.paint (left, forward);
    theWorld.repaint();
    return this;
    } //======================
     
     
    /** Fill a rectangle of the given width and height, Turtle at center. */
     
    public void fillBox (double width, double height)
    { super.fillBox (width, height);
    theWorld.repaint();
    } //======================
     
     
    /** Create a turtle at the center of the default JFrame. */
     
    public Turtle()
    { this (false, 760, 600); // special case of the constructor below
    } //======================
     
     
    /** If makeNewWorld is true, make an additional JFrame of the given
    * width and height. Create a turtle at the center of the JFrame. */
     
    public Turtle (boolean makeNewWorld, int width, int height)
    { super (makePage (makeNewWorld, width, height),
    width / 2, height / 2);
    } //======================
     
     
    /** Only done as a separate method because super() has to be
    * the first statement in the any constructor. */
     
    private static Graphics makePage (boolean makeNewWorld, int w, int h)
    { if (theWorld == null || makeNewWorld)
    theWorld = new TurtleWorld (w, h);
    return theWorld.getPage();
    } //======================
    }
    //###################################################################
     
     
    /** A TurtleWorld is a JFrame on which an Image object is drawn each time
    * the JFrame is repainted. Each Turtle draws on that Image object. */
     
    class TurtleWorld extends javax.swing.JFrame
    {
    private static final int EDGE = 3, TOP = 30; // around the JFrame
    private Image itsPicture;
    private Graphics itsPage;
     
    public TurtleWorld (int width, int height)
    { super ("Turtle Drawings"); // set the title for the frame
    setDefaultCloseOperation (EXIT_ON_CLOSE); // no WindowListener
    setSize (width + 2 * EDGE, height + TOP + EDGE);
    toFront(); // put this frame in front of the BlueJ window
    setVisible (true); // cause a call to paint
    begin (width, height);
    } //======================
     
     
    public void begin (int width, int height)
    { itsPicture = new java.awt.image.BufferedImage (width, height,
    java.awt.image.BufferedImage.TYPE_INT_RGB);
    itsPage = itsPicture.getGraphics();
    itsPage.setColor (Color.white);
    itsPage.fillRect (0, 0, width, height);
    itsPage.setColor (Color.black);
    } //======================
     
     
    public Graphics getPage()
    { return itsPage; // itsPicture.getGraphics(); => NO COLORS
    } //======================
     
     
    public void paint (Graphics g)
    { if (itsPicture != null)
    g.drawImage (itsPicture, EDGE, TOP, this);
    } //======================
    }

    Turtlet Class:

    import java.awt.Color;
     
    public class Turtlet extends FlowerMaker
     
    { public static final double DEGREE = Math.PI / 180;
    public static final Color RED = Color.red, BLUE = Color.blue,
    BLACK = Color.black, GRAY = Color.gray,
    YELLOW = Color.yellow, PINK = Color.pink,
    ORANGE = Color.orange, GREEN = Color.green,
    MAGENTA = Color.magenta, WHITE = Color.white;
    private static java.awt.Graphics thePage;
    //////////////////////////////////
    private double heading = 0; // heading initially east
    private double xcor, ycor; // current position of Turtle
     
     
    /** Set the drawing Color to the given value. Made an instance method
    * only so that it cannot be called until thePage is assigned, although
    * the drawing color is a property of thePage, not of one Turtle. */
     
    public void switchTo (Color given)
    { thePage.setColor (given);
    } //======================
     
     
    /** Write words without changing the Turtle's state. */
     
    public void say (String message)
    { thePage.drawString (message, round (xcor), round (ycor));
    } //======================
     
     
    /** Supply the nearest int value to methods requiring ints. */
     
    private int round (double x)
    { return (int) (x + 0.5);
    } //======================
     
     
    /** Make a circle of the given radius, Turtle at center. */
     
    public void swingAround (double radius)
    { if (radius > 0.0)
    { int rad = round (2 * radius);
    thePage.drawOval (round (xcor - radius),
    round (ycor - radius), rad, rad);
    }
    } //======================
     
     
    /** Fill a circle of the given radius, Turtle at center. */
     
    public void fillCircle (double radius)
    { if (radius > 0.0)
    { int rad = round (2 * radius);
    thePage.fillOval (round (xcor - radius),
    round (ycor - radius), rad, rad);
    }
    } //======================
     
     
    // the Turtle class, completed
     
    /** Rotate left by left degrees; MOVE for forward steps. */
     
    public Turtlet move (double left, double forward)
    { heading += left * DEGREE;
    xcor += forward * Math.cos (heading);
    ycor -= forward * Math.sin (heading);
    return this;
    } //======================
     
     
    /** Rotate left by left degrees; PAINT for forward steps. */
     
    public Turtlet paint (double left, double forward)
    { heading += left * DEGREE;
    double x = xcor + forward * Math.cos (heading);
    double y = ycor - forward * Math.sin (heading);
    thePage.drawLine (round (xcor), round (ycor),
    round (x), round (y));
    xcor = x;
    ycor = y;
    return this;
    } //======================
     
     
    /** Fill a rectangle of the given width and height, Turtle at center. */
     
    public void fillBox (double width, double height)
    { if (width > 0.0 && height > 0.0)
    { thePage.fillRect (round (xcor - width / 2),
    round (ycor - height / 2),
    round (width), round (height));
    }
    } //======================
     
     
    /** Pause the animation for wait milliseconds. Made a class method
    * so that an applet can pause an animation "between turtles". */
     
    public static void sleep (int wait)
    { try
    { Thread.sleep (wait);
    }catch (InterruptedException ex)
    {}
    } //======================
     
     
    /** Create a Turtlet on a given Component at a given starting position.
    * All Turtlets must be created from within the Component's paint()
    * method or from a method called by it. All Turtles live in
    * the same world at any given time, so changing the page is analogous
    * to spaceshipping the entire Turtle population to a new world. */
     
    public Turtlet (java.awt.Graphics page, double xstart, double ystart)
    { if (page == null)
    throw new NullPointerException ("You did not give a "
    + "world where turtles can live!");
    thePage = page;
    xcor = xstart;
    ycor = ystart;
    } //======================
    }

    FlowerMaker class:

    public class FlowerMaker
    {
    // Draw two flowers each 60 pixels tall.
    // Start and end facing east at the base of the left flower.
    public void drawTwoFlowers()
    {
    Turtle florist;
    florist = new Turtle();
     
    florist.drawFlower();
    florist.move (90, 61);
    florist.drawFlower();
    florist.move (90, -59);
    } //======================
    // Start facing east at the base of the flower, right side,
    // with the current drawing color being BLACK (for the stem).
    // End facing south at the base of the flower, center.
    public void drawFlower()
    {
    Turtle florist;
    florist = new Turtle();
     
    florist.paint (90, 50); // right side of stem
    florist.paint (90, 2);
    florist.paint (90, 50); // left side of stem
    florist.paint (90, 1);
    florist.paint (90, 10); // one-fourth of the way up the stem
    florist.paint (-45, 8); // draw the twig for the right leaf
    florist.drawLeaf();
    florist.paint (45, 10); // one-half of the way up the stem
    florist.paint (45, 8); // draw the twig for the left leaf
    florist.drawLeaf();
    florist.paint (-45, 30); // to top of stem, in the center
    florist.switchTo (Turtle.RED);
    florist.fillCircle (15); // draw the flower petals
    florist.switchTo (Turtle.BLACK);
    florist.move (180, 50); // return to the base of the flower
    } //======================
    public void drawLeaf()
    {
    Turtle florist;
    florist = new Turtle();
     
    florist.switchTo (Turtle.GREEN);
    florist.fillCircle (3);
    florist.move (0, 3);
    florist.fillCircle (2);
    florist.move (0, 2);
    florist.fillCircle (1);
    florist.move (0, -13);
    florist.switchTo (Turtle.BLACK);
    } //======================
    }

    Garden App:

    public class GardenApp
    {
    // Draw 6 flowers all in a row, with a word title.
    public static void main (String[ ] args)
    {
    Turtle florist;
    florist = new Turtle();
    florist.drawTwoFlowers(); // the central two
    florist.sleep (300);
    florist.move (0, 120);
    florist.drawTwoFlowers(); // the two right of center
    florist.sleep (300);
    florist.move (0, -240);
    florist.drawTwoFlowers(); // the two left of center
    florist.sleep (300);
    florist.move (40, 130);
    florist.switchTo (Turtle.BLUE);
    florist.say ("My flower garden"); // above the flowers
    } //======================
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why are my flowers stacked on top of each other?

    Please post the code here on the forum. Please be sure to wrap it in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Why are my flowers stacked on top of each other?

    I posted the code. Thank you so much!

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Why are my flowers stacked on top of each other?

    public Turtlet move (double left, double forward)
    { return super.move (left, forward);
    }


    I don't know why you are overriding the method in the subclass if all it does is call the method from the parent class. It already gets it from inheritance.


    Also, can't be sure, but, from what you've said, the problem is likely something going wrong in your move() methods. Note, I don't think the thing I posted above is the issue, just that it seems unnecessary.

    As for the issue, it's likely a mathematical error. Put in () around operations and see if anything changes.

  5. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    Techstudent (October 16th, 2013)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why are my flowers stacked on top of each other?

    The posted code has lost all its formatting. Nested statements should be indented. All the statements should not start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Techstudent (October 16th, 2013)

  8. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Why are my flowers stacked on top of each other?

    Don't know if this is the problem, but is this on purpose or a mistake:
    florist.paint (90, 50); // right side of stem
    florist.paint (90, 2);
    florist.paint (90, 50); // left side of stem
    Notice how you have painted the right side and the left side in the same place? Could that be reason?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Techstudent (October 16th, 2013)

Similar Threads

  1. top 10 high score table
    By wacka in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 15th, 2013, 09:03 AM
  2. Seperating a top down program into classes
    By Jpopto in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2013, 02:14 AM
  3. Can anybody help me fix this, what I am supposed to do is at the top
    By peplo1214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2012, 07:05 AM
  4. Output in applet keeps painting over the top of itself
    By SashaThompson in forum Java Applets
    Replies: 1
    Last Post: October 9th, 2011, 11:21 AM

Tags for this Thread