2013-02-11 142 views
-3

我是新来处理并正在创建一个草图,其中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); 
     } 
    } 
    } 
} 
+2

这看起来不是Java。 – Vulcan 2013-02-11 03:12:34

+0

即使如此......我不确定哪部分错误信息能准确告诉你问题是什么......令人困惑。 – 2013-02-11 03:14:08

+0

@OMGPonies,因为那正是他们的! – Brannon 2013-02-11 03:21:15

回答

1

您正在访问数组randomSize的元素。这个数组有多长?让我们来看看它的声明:

int[] randomSize = new int[sIncrement.length]; 

所以,它的长度相同阵列sIncrement。如果我们早一点:

int[] sIncrement = new int[12]; 

sIncrement的长度,并因此randomSize长度为12

在此代码:

for(int varCreator = 0; varCreator <= (width/s)+(height/s); varCreator++){ 
    randomSize[varCreator] = sIncrement[int(random(0,sIncrement.length-1))]; 
} 

索引使用访问数组randomSize中的元素在变量varCreator中,该变量从0到(width/s)+(height/s)。由于randomSize的长度为12,因此只能使用0到11的索引来访问元素,否则将会报告错误。

因此,如果(width/s)+(height/s)不应该是12或更多。这个多少钱 ?

这就是我们卡住的地方,因为代码中没有任何东西告诉我们在哪里声明了widthheight。我们只知道s是50

但是你有这些变量,widetall,每个等于600,我要带胡乱猜测和估计,在某处你的代码中有width = wideheight = tall

所以(width/s)+(height/s) = (600/50) + (600/50) = 12 + 12 = 24

就是这样。 varCreator从0变为24(包含),因此您用尽了数组的界限。

+0

非常感谢您的详细描述! – Brannon 2013-02-11 03:26:29

1

跟踪你的代码你有这样的:

int[] sIncrement = new int[12];     // sIncrement initialized with size 12 
int[] randomSize = new int[sIncrement.length];  // randomSize initialized with size 12 
for(... ; varCreator <= (width/s)+(height/s); ...) // varcreator <= 24! 
randomSize[varCreator] = ...      // Problem line 

varcreator >= 12你会得到一个数组索引超出范围的异常。从那里开始并修复你的代码。