我是处理新手,正在创建一个草图,其中一个 600 像素 x 600 像素的画布填充了我orange[]
调色板中随机颜色的 50 像素矩形。块的随机形成需要位于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);
}
}
}
}