This site requires JavaScript, please enable it in your browser!
Greenfoot back
dbutts1@stumail.com
dbutts1@stumail.com wrote ...

2017/12/15

Greenfoot 6.34 Bubbles size and direction

dbutts1@stumail.com dbutts1@stumail.com

2017/12/15

#
I am trying to just put 18 bubbles of size 190 decreasing by 10 in the center of the world. They are supposed to have the direction changed by +20 degrees. I have used a while loop that looks like while ( i < bubbles.lenght) and while (i <=18) as starting points and I run into the same problem. I think I am some how getting an infinite loop, and that is why I get the following message. I get the" Exception in thread"AWT-EventQue-0" java.lang.OutOfMemoryError:Java heap space" message I also want to know why inside the loop for the addObjects I am prompted to use semicolons between parameters instead of commas. I do not get any compiling errors with the following code, but I get the message The world can't be completed. One of the classes may have an error, check them to initialize the scenario. Part of my problem may be my understanding of the vocabulary as I am very new to this. I usually use the getWidth()/2, getHetght()/2 to place objects in the center of the world but am getting the not a statement when I put in the /2 part. Here is my code form the Subclass of world. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A bit of empty space for bubbles to float in. * * @author Michael Kölling * @version 1.0 */ public class Space extends World { /** * Create Space. Make it black. */ public Space() { super(900, 600, 1); getBackground().setColor(Color.BLACK); getBackground().fill(); setup(); } private void setup() { Bubble bubble = new Bubble; int i = 0; int size = 190; int direction = 0; while( i <+18) { bubble = new Bubble(); addObject(new Bubble()+size(190)-10);direction(0+20);getWidth(); getHeight(); i=i+1; } } } I would appreciate any help anyone can give.
Super_Hippo Super_Hippo

2017/12/15

#
Please use the code tags so the code is properly displayed. You have a bubble array, but you add (or trying to add) a new bubble object to the world. You also don't save the array of the bubbles. So you can remove the array. The addObject method requires three parameters which need to be separated by a comma. An object, the x and the y coordinate. You can not add something like 'size(190)' to the bubble object despite the fact that I don't see any 'size' method which wouldn't make any sense either. Same with the direction. You end the method with the ), so the compiler wants a semicolon. You may want to add a new bubble constructor which requires an int as the parameter, the size. And in this constructor, you construct the bubble image size based on that passed variable. Then the 'setup' method could be this:
for (int i=0; i<18; i++)
{
    addObject(new Bubble(190-10*i), getWidth()/2, getHeight()/2);
}
Or, with a while loop:
int i=0;
while (i<18)
{
    addObject(new Bubble(190-10*i), getWidth()/2, getHeight()/2);
    i++;
}
dbutts1@stumail.com dbutts1@stumail.com

2017/12/15

#
Thank You I will keep working on it. Can you please tell me where the code tags are to attach to my code. Is it simply clicking on the word "code" that is below this , and then cut and paste my code in the box that pops up? Again Thank You
Super_Hippo Super_Hippo

2017/12/15

#
Either that or you simply write
[code]your code inside here[/ code] <-- no space inside
dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A bit of empty space for bubbles to float in.
 * 
 * @author Michael Kölling
 * @version 1.0
 */
public class Space extends World
{


    
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
      
       super(900, 600, 1);
       getBackground().setColor(Color.BLACK);
       getBackground().fill();
       
       setup();
       
    }
    

    //private void setup()
    //{ 
        
       // Bubble bubble = new Bubble();
       // int i = 0;
        
        

       // while( i < 18)
       // {
            
           // addObject(new Bubble(190-10*i),getWidth()/2, getHeight()/2);
           // i=i+1;

        
     
    public int setup(int size, int direction)
    {
        int bubbleSize = 190;
        int bubbleDirection = 0;
        
        
        Bubble bubble = new Bubble();
        int i=0;
        
        
        while(i<18)
        {
            addObject(new Bubble(190-10*1,0+1*20),getWidth()/2,getHeight()/2);
            i++;
        }
        return size ;
        return direction;
        
        
    }
}
dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
I am still working Greenfoot 6.34 remove the existing loops from your setup() method. Write a new while loop that does the following: it creates 18 bubbles. the bubbles are all placed in the center of the world, and have sizes starting from 190, decreasing by 10. The last bubble has size 10. Make sure to create the largest first, and the smallest last, so that you have bubbles of sizes 190,180,170,etc., all lying on tip of each other. Use the third constructor of Bubble- the one with two parameters, it also lets you specify the initial direction. Set the direction as follows: the first bubble has direction 0, the next 20, the next 40,etc. That is , the direction each two bubbles increases by 20 degrees The 3rd constructor in Bubble is public Bubble(int size, int direction) { this(size); setRotation(direction); } When I attempt to use it as is in the World subclass I run into the issue of the statement that if I am going to use this it has to be placed at the beginning of the constructor, which interferes with super(900,600,1) line 20. I have tried using public void on line 45 and then putting setup(); line 24 I get the Message method setup in class Space cannot be applied to given types; required int,int found no arguments actual and formal arguments differ in length. I have tried changing the line 24 to setup( , ); I have tried using void and no return type for line 45 When I remove the setup(); from line 24 the world will compile by since there is no method to run the screen is blank. Am I repeating the same kind of mistakes as before? When I used the second suggestion for the while loop from 5 hours ago I was able to get the 18 bubbles in the center and it would both compile and run. I am having trouble with the last part of getting the direction set correctly.
dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
I think I have this correct now. I would appreciate any comments or guidance if I do not.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A bit of empty space for bubbles to float in.
 * 
 * @author Michael Kölling
 * @version 1.0
 */
public class Space extends World
{


    private int bubbleSize;
    private int bubbleDirection;
    
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
      
       super(900, 600, 1);
       getBackground().setColor(Color.BLACK);
       getBackground().fill();
       setup( bubbleSize, bubbleDirection);
       
       
    }
    

    //private void setup()
    //{ 
        
       // Bubble bubble = new Bubble();
       // int i = 0;
        
        

       // while( i < 18)
       // {
            
           // addObject(new Bubble(190-10*i),getWidth()/2, getHeight()/2);
           // i=i+1;

        
     
    private int setup(int size, int direction)
    {
        int bubbleSize = 0;
        int bubbleDirection = 0;
        
        
        Bubble bubble = new Bubble();
        int i=0;
        
        
        while(i<18)
        {
            addObject(new Bubble(190-10*i,0+1*20),getWidth()/2,getHeight()/2);
            i++;
        }
        
        return bubbleSize + bubbleDirection;
        
        
        
    }
}
Super_Hippo Super_Hippo

2017/12/16

#
Do the following changes:
//line 25
setup();

//line 47
private void setup()

//remove lines 13, 14, 49, 50, 53 and 63

//beginning of line 59 (i, not 1)
addObject(new Bubble(190-10*i, 20*i), ...
Or, starting with the commented setup method: Remove line 34 and use the line 59 with the i instead of 1 as line 42. (+ 2× '}')
dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
I caught the i instead of 1 on line 59 after I submitted this last night. I wanted to make sure I understood what you meant on the using the comment out method correctly. Are you saying make line 34 read as addObject(new Bubble(190-10*i,20*i+2),getWidth()/2,getHeight()/2); or addObject(new Bubble(190-10*i,0+i*20+2),getWidth()/2,getHeight()/2); to get the direction between each 2 bubbles to increase by 20 degrees. I am not sure I am understanding what the x'}') of the (+2x'}') is for or how to insert it without throwing that whole line into chaos. The following is my code after using the comment out section. Let me know if that is what you meant or if I am not understanding the second solution. The lines have all changed. Here is my new code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A bit of empty space for bubbles to float in.
 * 
 * @author Michael Kölling
 * @version 1.0
 */
public class Space extends World
{


    private int bubbleSize;
    private int bubbleDirection;
    
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
      
       super(900, 600, 1);
       getBackground().setColor(Color.BLACK);
       getBackground().fill();
       //setup( bubbleSize, bubbleDirection);
       setup();
       
    }
     
    private  void setup ()
    { 
        int i=0;
        while(i<18)
        {
            addObject(new Bubble(190-10*i,i*20+2),getWidth()/2,getHeight()/2);
            i++;
        } 
    }
}
Super_Hippo Super_Hippo

2017/12/16

#
You can remove the +2 in line 35. I only meant to add the two } (the curly brackets or how they are called again) because they were missing in the commented method. You can also still remove lines 13 and 14.
dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
Okay, Thank you very much.
You need to login to post a reply.