私が作成しているインタラクティブなグラフィックのコードを書いています。これには3つの状態があります。switchステートメントを使用して、画像の配列を表示する状態間を移動しました。すべての画像をアップロードしましたが、アップロードした8つの画像の配列を持つ1つの状態を呼び出すと、エラーメッセージが表示されます。ArrayIndexOutOfBoundsException 8
スケッチに正しい数の画像がロードされていても、それを取り除くことができないようです。
以下は私のコードで、Processing2.0b7を使用してMacbookProOSXで作業しています。
PImage[] teams;
int n = 24;
PImage[] teams2;
int m = 16;
PImage [] quarterFinalWinners;
int o = 7;
PImage bg;
float damping = 0.1;
PFont font;
String[] positions, prevPositions;
float x;
float y;
int afconState = 3;
/**----------------------------------------------------------------------------------------------------------------------------**/
public void setup() {
size(600, 1600);
bg = loadImage("football-pitch-hi.png");
positions = new String[]{"18", "26", "32", "45", "58", "56", "59", "61", "66", "69", "71", "85", "98", "100", "116", "133"};
prevPositions = new String[]{"14", "19", "25", "30", "34", "45", "52", "69", "71", "72", "87", "84", "89", "105", "107", "110"};
int a = 16;
font = loadFont("Calibri-Bold-48.vlw");
textFont(font);
frame.setResizable(true);
teams = new PImage[n];
for (int i = 0; i < teams.length; i++) {
teams[i] = loadImage(i + ".png");
}
teams2 = new PImage[m];
for (int i = 0; i < teams2.length; i++) {
teams2[i] = loadImage(i + ".gif");
}
quarterFinalWinners = new PImage[o];
for (int o = 0; o < quarterFinalWinners.length; o++); {
quarterFinalWinners[o] = loadImage(o + "_.png");
}
}
/**----------------------------------------------------------------------------------------------------------------------------**/
void draw() {
background(#000000);
if ((x < width) && (y < height)) {
for (int i = 0; i < 16; i++);
}
}
/**----------------------------------------------------------------------------------------------------------------------------**/
public void keyPressed() {
noLoop();
switch(afconState) {
case 1:
afconState = 1;
background(#000000);
for (int i = 0; i < 16; i++) {
image(teams[i], 150, 60*i);
}
}
if ((key == 's') || (key == 'S')) {
afconState = 2;
background(#000000);
for (int i = 0; i < 16; i++) {
image(teams2[i], 150, 60*i);
}
}
else if ((key == 'q') || (key == 'Q')) {
afconState = 3;
background(#000000);
for (int i = 0; i < 16; i++) {
image(quarterFinalWinners[i], 150, 60*i);
}
}
break;
/**----------------------------------------------------------------------------------------------------------------------------**/
case 2:
if((key == 's') || (key == 'S')) {
afconState = 2;
background(#000000);
for (int i = 0; i < 16; i++) {
image(teams2[i], 150, 60*i);
}
}
else if ((key == 'q') || (key == 'Q')) {
afconState = 3;
for (int i = 0; i < 16; i++) {
image(quarterFinalWinners[i], 150, 60*i);
}
}
break;
case 3:
background(#000000);
if ((key == 'q') || (key == 'Q')) {
afconState = 3;
for (int i = 0; i < 16; i++) {
image(quarterFinalWinners[i], 150, 60*i);
}
}
else if((key == 's') || (key == 'S')) {
afconState = 2;
background(#000000);
for (int i = 0; i < 16; i++) {
image(teams2[i], 150, 60*i);
}
}
break;
default:
afconState = 1;
background(#000000);
for (int i = 0; i < 16; i++) {
image(teams[i], 150, 60*i);
}
if ((key == 's') || (key == 'S')) {
afconState = 2;
// modify the positions list
for (int i = 0; i < 16; i++) {
image(teams2[i], 150, 60*i);
}
}
else if ((key == 'q') || (key == 'Q')) {
afconState = 3;
for (int i = 0; i < 16; i++) {
image(quarterFinalWinners[i], 150, 60*i);
}
}
}
redraw();
}
void mousePressed () {
// revert to previous array. We can only do this once.
// if someone pressed the key seven times, we can't revert
// seven times, because the code only has one history state
// that is updated every time a key is pressed
// redraw now that our state has changed
redraw();
}