zaphod
YaBB Newbies
Offline
Posts: 16
array problems?
Aug 23rd , 2005, 3:08am
I've got a program that's supposed to draw a grid of squares on the screen, using a 2 dimensional array to identify the squares and store their color values. When I try to execute the program, I get this error from the compiler (or debugger or what ever you call it): java.lang.ArrayIndexOutOfBoundsException: 0 I don't think that my code could have generated a number larger than the size of the array, so I'm confused as to what is going wrong. Here's my program: int ssize = 15; color z, whi, bla; int ncount, w, h; color[][] current = new color[w][h]; color[][] next = new color [w][h]; void setup(){ size(900, 600); background(0); framerate (60); stroke(122); w = width/ssize; h = height/ssize; bla = color(0,0,0); whi = color(255,255,255); for(int i=0; i<w; i++){ for(int j=0; j<h; j++){ current[i][j] = bla; } } } void draw(){ for(int i=0; i<w; i++){ for(int j=0; j<h; j++){ fill(current[i][j]); rect(14*i,14*j,15,15); } } for(int i=0; i<width; i++){ for(int j=0; j<height; j++){ neighbors (i, j); if (ncount == 3){ next[i][j] = whi;} else if(ncount != 2){ next[i][j] = bla;} } } for(int i=0; i<w; i++){ for(int j=0; j<h; j++){ current[i][j] = next[i][j]; } } } void neighbors (int wi, int wj){ ncount = 0; if (current[wi-1][wj-1]==whi){ ncount ++;} if (current[wi][wj-1]==whi){ ncount ++;} if (current[wi+1][wj-1]==whi){ ncount ++;} if (current[wi-1][wj]==whi){ ncount ++;} if (current[wi+1][wj]==whi){ ncount++;} if (current[wi-1][wj+1]==whi){ ncount++;} if (current[wi][wj+1]==whi){ ncount++;} if (current[wi+1][wj+1]==whi){ ncount++;} } Also, this code seems pretty cumbersome to me. Especially the function neighbors at the bottom. It's supposed to check the eight squares around the current one to see what their colors are, but there is a whole lot of duplicated work the way I have it here. Does anyone have any suggestions about how to make this a bit more streamlined?