我是新来处理并正在创建一个草图,其中600px600px的画布填充了我的orange[]
调色板中随机颜色的50px矩形。块的随机形成需要位于draw()
函数内部,以便在稍后将放入的某些条件下正确运行。处理:数组索引超出范围
,我得到的错误是ArrayIndexOutOfBoundsException异常:12在这条线:
randomSize[varCreator] = sIncrement[int(random(0,sIncrement.length-1))];
我似乎无法找出为什么这个错误发生。我已研究过相关的问题,也许它只是因为我太新了,但我不能弄明白:
int x; //x coordinate
int y; //y coordinat
int s = 50; //rect size
int wide = 600; //canvas width
int tall = 600; //canvas height
int[] sIncrement = new int[12];//{s, s*2, s*3, s*4, s*5, s*6};
//colors
int[] oranges = {
#773600, #5f3613, #552700, #9c5215, #9c5c26
};
int[] blues = {
#004848, #0c3939, #003333, #107979, #1e7979
};
int[] palette = oranges;//holds current color pallete
//random
int fillColor = palette[int(random(0, palette.length))]; //random starting fill color
int changeColor = palette[int(random(0, palette.length))]; //random new color
int[] randomSize = new int[sIncrement.length]; //array of lots of random s values to place newly color changed blocks
//setup
void setup(){
size(wide, tall);
background(255);
noStroke();
frameRate(24);
/*fills sIncrement array with incrementing s values (i.e. if s = 50 then array
contains 50, 100, 150, etc...) from 0 to canvas width for use in a conditional statement*/
for(int i = 0; i <= sIncrement.length-1; i++){
sIncrement[i] = s*i;
}
//creates multiple randomSize variables for if() x or y == randomSize[varCreator]
for(int varCreator = 0; varCreator <= (width/s)+(height/s); varCreator++){
randomSize[varCreator] = sIncrement[int(random(0,sIncrement.length-1))];
}
}
//draw
void draw(){
fill(fillColor); //selects random color from palette
//draws grid colored boxes with s size
for (y = 0; y <= height; y+= s) {
for (x = 0; x <= width; x+= s) {
if(x == sIncrement[randomSize[1]] && y == sIncrement[randomSize[3]]){
fill(changeColor); //selects random color from palette
rect(x, y, s, s);
}
else{
fill(fillColor);
// fill(palette[int(random(0, palette.length))]); //selects random color from palette
rect(x, y, s, s);
}
}
}
}
这看起来不是Java。 – Vulcan 2013-02-11 03:12:34
即使如此......我不确定哪部分错误信息能准确告诉你问题是什么......令人困惑。 – 2013-02-11 03:14:08
@OMGPonies,因为那正是他们的! – Brannon 2013-02-11 03:21:15