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

• October 15th, 2013, 07:15 PM
Techstudent
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!Attachment 2383

Turtle Class:

Code :

```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:

Code :

```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:

Code :

```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:

Code :

```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 } //====================== }```
• October 15th, 2013, 07:20 PM
Norm
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.
• October 15th, 2013, 11:14 PM
Techstudent
Re: Why are my flowers stacked on top of each other?
I posted the code. Thank you so much!
• October 15th, 2013, 11:25 PM
GoodbyeWorld
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.
• October 16th, 2013, 05:41 AM
Norm
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.
• October 16th, 2013, 11:00 AM
aussiemcgr
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:
Code java:

```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?