java - Cannot find array -


i error "cannot find named coordinates" @ line 110:

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

when trying run this:

import tuio.*; tuioprocessing tuioclient;  int cols = 15, rows = 10; boolean[][] states = new boolean[cols][rows]; int videoscale = 50;  // these helper variables used // create scalable graphical feedback int x, y, i, j; 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 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;    // create instance of tuioprocessing client   // since add "this" class argument tuioprocessing class expects   // implementation of tuio callback methods in class (see below)   tuioclient  = new tuioprocessing(this);  } void draw(){   // begin loop columns   (int k = 0; k < cols; k++) {     // begin loop rows     (int l = 0; l < rows; l++) {        // scaling draw rectangle @ (x,y)       int x = k*videoscale;       int y = l*videoscale;        fill(255);       stroke(0);        string[][] coordinates = new string[cols][rows];   (int = 0; < cols; i++) {   (int j = 0; j < rows; j++) {    coordinates[i][j] = string.valueof((char)(i+65)) + string.valueof(j).touppercase();    } }        //check if coordinates within box (these mouse x,y fiducial x,y)       //simply bounds (left,right,top,bottom)       if( (mousex >= x &&  mousex <= x + videoscale) && //check horzontal           (mousey >= y &&  mousey <= y + videoscale)){         //coordinates within box,        system.out.println(coordinates[k][l]);          //you can keep track of 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();   (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 within box,        system.out.println(coordinates[k][l]);       }  } } // -------------------------------------------------------------- // these callback methods called whenever tuio event occurs // there 3 callbacks add/set/del events each object/cursor/blob type // final refresh callback marks end of each tuio frame // called when object added scene  /* void addtuioobject(tuioobject tobj) {   if (verbose) println("add obj "+tobj.getsymbolid()+" ("+tobj.getsessionid()+") "+tobj.getx()+" "+tobj.gety()+" "+tobj.getangle()); }   // called when object moved void updatetuioobject (tuioobject tobj) {   if (verbose) println("set obj "+tobj.getsymbolid()+" ("+tobj.getsessionid()+") "+tobj.getx()+" "+tobj.gety()); }  // called when object removed scene void removetuioobject(tuioobject tobj) {   if (verbose) println("del obj "+tobj.getsymbolid()+" ("+tobj.getsessionid()+")"); } */  // -------------------------------------------------------------- // called @ end of each tuio frame void refresh(tuiotime frametime) {   if (verbose) println("frame #"+frametime.getframeid()+" ("+frametime.gettotalmilliseconds()+")");   if (callback) redraw(); } 

i don't understand why since declared line 57:

string[][] coordinates = new string[cols][rows];  

anyone knows why ? should declare in different way make accessible everywhere ?

thanks help

you're declaring coordinates variable inside first nested loop, it's in scope during execution of loop. it's recreated every time loop iterates.

but you're trying access variable after loop exits. @ point, coordinates variable out of scope, error.

you have refactor code coordinates in scope when access it.

in other words, won't work:

for(int = 0; < 10; i++){    string test = "xyz"; }  println(test); 

since test variable visible inside loop, can't use outside loop. instead have this:

for(int = 0; < 10; i++){    string test = "xyz";    println(test); } 

or this:

string test = "abc"; for(int = 0; < 10; i++){    test = "xyz"; }  println(test); 

which approach take depends on want code do.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -