This site requires JavaScript, please enable it in your browser!
Greenfoot back
georgette
georgette wrote ...

2016/12/7

Loops/error

georgette georgette

2016/12/7

#
Hello, please help! My code compiles without any syntax errors, however, when I attempt to run it I keep getting the following error message: java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0 at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:189) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:130) at Bubble.<init>(Bubble.java:28) at Bubble.<init>(Bubble.java:50) at Bubble.<init>(Bubble.java:20) at Space.setUp(Space.java:33) at Space.<init>(Space.java:20)
public class Bubble extends Actor
{
    private int speed;
    
    /**
     * Create a Bubble that floats, with random size and random color.
     */
    public Bubble()
    {
        // create a random size, between 10 and 110 pixels
        this(Greenfoot.getRandomNumber(01), + 10);
    }

    /**
     * Create a Bubble that floats, with a given size and random color.
     */
    public Bubble(int size)
    {
        GreenfootImage img = new GreenfootImage(size, size);

        // create a random color, with every color channel between 30 and 230
        int red = Greenfoot.getRandomNumber(200) + 30;
        int green = Greenfoot.getRandomNumber(200) + 30;
        int blue = Greenfoot.getRandomNumber(200) + 30;
        int alpha = Greenfoot.getRandomNumber(190) + 60;

        img.setColor(new Color(red, green, blue, alpha));
        img.fillOval(0, 0, size-1, size-1);
        setImage(img);

         //random speed: 1 to 4
        speed = Greenfoot.getRandomNumber(4) + 1;
    }
    
    int x = 0;
    /**
     * Create a Bubble that floats, with given size and initial float direction.
     */
    public Bubble(int size, int direction)
    {
        this(size);
        setRotation(direction);
        setLocation (getX() *63, getY());
    
    }

    /**
     * Float.
     */
    public void act() 
    {
        //addObject (new Bubble() );
        Actor body = getOneIntersectingObject(Bubble.class);

        if (isAtEdge()) {
            turn(180);
        }

        move(speed);
        setLocation(getX()-4, getY());        

        if (Greenfoot.getRandomNumber(100) < 50) {
            turn(Greenfoot.getRandomNumber(5) -2 );   // -2 to 2
        }
    }   

}



public class Space extends World
{
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
        super(900, 600, 1);
        getBackground().setColor(Color.BLACK);
        getBackground().fill();        
        setUp();    

    }

    /**
     * Create 21 random bubbbles. Create 10 bubbles in horizontal line.
     */
    private void setUp()
    {
        Bubble[] bubble = new Bubble[21];
        int i = 0; 
        while (i < 21 )
        {
            addObject(new Bubble(), Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(600) );
            addObject(new Bubble(),getWidth() / 2, getHeight() / 2); 
            bubble[i] = new Bubble();
            addObject(new Bubble(10+i), i*45, 30);
            i = i + 1;
        }  
        int x = 0;
        while ( x < 10 ) 
        {
            addObject(new Bubble (x * 10 + 10), 300 + x*40, 100);
            x = x +1;
        }
    } 

}
Nosson1459 Nosson1459

2016/12/7

#
In the error there are line numbers, I can't use those since you didn't post every single line. If you're on windows press <Ctrl> 'a' to select all then <Ctrl> 'c' to copy and paste everything here (<Ctrl> 'p').
danpost danpost

2016/12/7

#
georgette wrote...
My code compiles without any syntax errors, however, when I attempt to run it I keep getting the following error message: < Error Trace Omitted > < Code Omitted >
In line 11, you have 'Greenfoot.getRandomNumber(01)' for the first parameter. This is always equal to zero and since you are using that parameter for the size of the bubble, you are asking the bubble to have no size (this is why you get the error; you are asking to create an image of size zero which you cannot do). First increase the number from '01' to whatever range (max-min) you want for the bubble size; then add the min value to that as well:
int minSize = 10;
int maxSize = 20;
int size = minSize+Greenfoot.getRandomNumber(maxSize-minSize+1);
You need to login to post a reply.