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

2015/10/9

"Bound must be positive"

Vellyxenya Vellyxenya

2015/10/9

#
Hi i'm trying this code but it tells me: java.lang.IllegalArgumentException : bound must be positive...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import greenfoot.*;
import java.awt.Color;
 
public class Deformator extends SmoothMover
{
    Color [][] pixels = new Color[140][50];
    private int counter;
    GreenfootImage deformation = new GreenfootImage(140, 50);
 
    public Deformator()
    {
        counter = 0;
    }
 
    public void act()
    {
        movements();
        for(int i = 0; i<140 ; i++)
        {
            for (int j = 0; j<50; j++)
            {
                pixels [i][j] = getWorld().getColorAt(this.getX()-70+i, this.getY()-25+j);
            }
        }
        for(int i = 0; i<140 ; i++)
        {
            for (int j = 0; j<50; j++)
            {
                deformation.setColorAt(139-i, 49-j, pixels[Greenfoot.getRandomNumber(i)][j]);
                setImage(deformation);
            }
        }
    }
 
    public void movements()
    {
        if(this.getX()>70)
        {
            move(-1);
        }
    }
}
It's at line 29.. I want to get a randomNumber from 0 to i, I don't see how it could be negative. Thank you for helping^^
ElNo ElNo

2015/10/9

#
Hi! In some way you have already given the answer yourself: Your i ranges from 0 to 139 and 0 is not negative - but it isn't positive either! The method getRandomNumber requires a positive bound, that means a number bigger than 0 (that means beginning with 1).
danpost danpost

2015/10/9

#
Vellyxenya wrote...
I want to get a randomNumber from 0 to i, I don't see how it could be negative. Thank you for helping^^
If you want a random number within that range you need to use a parameter of 'i+1', not 'i'. You cannot use:
1
int randomNumber = Greenfoot.getRandomNumber(0);
The parameter must be positive (as the error message is trying to tell you), not zero or negative.
Vellyxenya Vellyxenya

2015/10/10

#
Oh yes I see, thank you very much!
You need to login to post a reply.