2015-07-01 36 views
0

我得到的錯誤ArrayOutOfBoundException: 15 at line 110錯誤ArrayOutOfBoundException

System.out.println(coordinates[k][l]); 

當嘗試運行這段代碼:

import TUIO.*; 
TuioProcessing tuioClient; 

int cols = 15, rows = 10; 
boolean[][] states = new boolean[cols][rows]; 
String[][] coordinates = new String[cols][rows]; 
int videoScale = 50; 

// these are some helper variables which are used 
// to create scalable graphical feedback 
int x, y, i, j, k, l; 
float cursor_size = 15; 
float object_size = 60; 
float table_size = 760; 
float scale_factor = 1; 
PFont font; 

boolean verbose = false; // print console debug messages 
boolean callback = true; // updates only after callbacks 



void setup(){ 
    size(500,500); 
noCursor(); 

    noStroke(); 
    fill(0); 

    // periodic updates 
    if (!callback) { 
    frameRate(60); //<>// 
    loop(); 
    } else noLoop(); // or callback updates 

    font = createFont("Arial", 18); 
    scale_factor = height/table_size; 

    // finally we create an instance of the TuioProcessing client 
    // since we add "this" class as an argument the TuioProcessing class expects 
    // an implementation of the TUIO callback methods in this class (see below) 
    tuioClient = new TuioProcessing(this); 

} 
void draw(){ 
    // Begin loop for columns 
    for (k = 0; k < cols; k++) { 
    // Begin loop for rows 
    for (l = 0; l < rows; l++) { 

     // Scaling up to draw a rectangle at (x,y) 
     int x = k*videoScale; 
     int y = l*videoScale; 

     fill(255); 
     stroke(0); 


for (int i = 0; i < cols; i++) { 
    for (int j = 0; j < rows; j++) { 

coordinates[i][j] = String.valueOf((char)(i+65)) + String.valueOf(j).toUpperCase(); 

    } 
} 
    /* 
     //check if coordinates are within a box (these are mouse x,y but could be fiducial x,y) 
     //simply look for bounds (left,right,top,bottom) 
     if((mouseX >= x && mouseX <= x + videoScale) && //check horzontal 
      (mouseY >= y && mouseY <= y + videoScale)){ 
     //coordinates are within a box, do something about it 
     System.out.println(coordinates[k][l]); 
     //you can keep track of the boxes states (contains x,y or not) 
     states[k][l] = true; 

     if(mousePressed) println(k+"/"+l); 

     }else{ 

     states[k][l] = false; 

     } 

*/ 
     rect(x,y,videoScale,videoScale); 
    } 
    } 

    textFont(font,18*scale_factor); 
    float obj_size = object_size*scale_factor; 
    float cur_size = cursor_size*scale_factor; 

    ArrayList<TuioObject> tuioObjectList = tuioClient.getTuioObjectList(); 
    for (int i=0;i<tuioObjectList.size();i++) { 
    TuioObject tobj= tuioObjectList.get(i); 
    stroke(0); 
    fill(0,0,0); 
    pushMatrix(); 
    translate(tobj.getScreenX(width),tobj.getScreenY(height)); 
    rotate(tobj.getAngle()); 
    rect(-obj_size/2,-obj_size/2,obj_size,obj_size); 
    popMatrix(); 
    fill(255); 
    text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height)); 
    System.out.println(tobj.getSymbolID()+ " " + tobj.getX()); 

    if((tobj.getX()>= x && tobj.getX() <= x + videoScale) && //check horzontal 
      (tobj.getY() >= y && tobj.getY() <= y + videoScale)){ 
     //coordinates are within a box, do something about it 
     System.out.println(coordinates[k][l]); 
    } 
rect(x,y,videoScale,videoScale); 
} 
} 
// -------------------------------------------------------------- 
// these callback methods are called whenever a TUIO event occurs 
// there are three callbacks for add/set/del events for each object/cursor/blob type 
// the final refresh callback marks the end of each TUIO frame 
// called when an object is added to the scene 

/* void addTuioObject(TuioObject tobj) { 
    if (verbose) println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()); 
} 

// called when an object is moved 
void updateTuioObject (TuioObject tobj) { 
    if (verbose) println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()); 
} 

// called when an object is removed from the scene 
void removeTuioObject(TuioObject tobj) { 
    if (verbose) println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")"); 
} 
*/ 

// -------------------------------------------------------------- 
// called at the end of each TUIO frame 
void refresh(TuioTime frameTime) { 
    if (verbose) println("frame #"+frameTime.getFrameID()+" ("+frameTime.getTotalMilliseconds()+")"); 
    if (callback) redraw(); 
} 

這是否意味着它在比數較大的地方分配一個值這個數組可以包含的元素?我如何修改它?我在找不到指定數組大小的代碼中找不到。我創建它的基礎上colsrows(15來自那裏,因爲當我修改它,說4,誤差變ArrayOutOfBoundException: 4,但即使有1有一個錯誤,所以我不明白這一點),所以我改變這些值但我仍然得到錯誤。

感謝您的幫助

+0

請在此處顯示您在線路「110」處顯示的內容。 – kiruwka

+0

draw()中的前兩個循環結束於函數的末尾 - 這就是你想要做的事情嗎? (第三和第四個循環在第二個循環內) – Iamsomeone

+0

@kiruwka第110行是我剛纔引用的行 –

回答

1

您要使用的變量kl以下循環外 -

for (k = 0; k < cols; k++) { 
    // Begin loop for rows 
    for (l = 0; l < rows; l++) { 

好像K,L是在實例變量範圍內定義。上述循環結束後,k的值爲cols,l的值爲行,我假設座標長度爲(rows, cols)

因此,你所得到的問題,當您嘗試打印的if條件中 - if((tobj.getX()>= x && tobj.getX() <= x + videoScale) && (tobj.getY() >= y && tobj.getY() <= y + videoScale))

也許你想在循環之前打印出來的k和l`l變量已經結束了。這是以下行前 -

 rect(x,y,videoScale,videoScale); 
    } // <-- Here 'l' loop ends. 
    } // <---- Here 'k' loop ends. 

    textFont(font,18*scale_factor); 
    float obj_size = object_size*scale_factor; 
    float cur_size = cursor_size*scale_factor; 

或者,也許你不想結束kl循環(我在這篇文章的開頭即提到的那些)嗎?你有沒有想過評論這部分?

1

價值變量k 15在本月底for循環

for (k = 0; k < cols; k++) { 
// Begin loop for rows 
    for (l = 0; l < rows; l++) { 

     // Scaling up to draw a rectangle at (x,y) 
     int x = k*videoScale; 
     int y = l*videoScale; 

     fill(255); 
     stroke(0); 


     for (int i = 0; i < cols; i++) { 
      for (int j = 0; j < rows; j++) { 

      coordinates[i][j] = String.valueOf((char)(i+65)) +  String.valueOf(j).toUpperCase(); 

      } 
     } 
     rect(x,y,videoScale,videoScale); 
    } 
    } 

那麼你想在這一行訪問coordinates索引號coordinates[15][11]

System.out.println(coordinates[k][l]); 

這就是爲什麼你得到例外。

希望這會有所幫助。