Hello everyone,
I am a new user of imageJ and and presently using it first time for my project.For that i am writin codes for a plugin.I am facing a very basic programming difficulty and and i am unable to solve that.Here I am using an Array of Object which i have declared like ... class Result{ //Data fields... } public class Grid2 extends MouseAdapter implements PlugIn { *Result result[][] = new Result[32][32];* public void run(String arg) { for(int p=0;p<32;p++){ for(int q=0;q<32;q++){ result[p][q] = new Result(); } } //*other codes here....which uses This array result[32][32]* }//*run() ends here* /*/some other codes that uses result* }//PlugIn ends here this works nice for me.But i want that the array size is given by user during run time ,so need to be defined inside* run()*.But if i do that i loose my access over that array as soon as run() ends and can not use it outside it.I hope i am clear about my problem.*I want to access the array outside the run() block but at the same time declare it inside run().* Thank you all. Ashish |
To fix that you only need to declare the array as a class member and
inside the run() method you assign the memory to it. Something like: class Result{ //Data fields... } public class Grid2 extends MouseAdapter implements PlugIn { Result result[][]; // <<<--- I declare it, but it has no memory assigned yet. public void run(String arg) { int rows, cols; // <<<< Here you get matrix size in some way: from a textfield, as a parameter at constructor, or whatever... result = new Result[rows][cols]; // <<--- Now I assign memory for the matrix for(int p=0;p<32;p++){ for(int q=0;q<32;q++){ result[p][q] = new Result(); } } //*other codes here....which uses This array result[32][32]* }//*run() ends here* /*/some other codes that uses result* }//PlugIn ends here I hope that helps, but that's not a problem with imageJ, that's plain java ;) Sincerelly, Juanjo Vega. ashish ram escribió: > Hello everyone, > > I am a new user of imageJ and and presently using it first time for my > project.For that i am writin codes for a plugin.I am facing a very basic > programming difficulty and and i am unable to solve that.Here I am using an > Array of Object which i have declared like ... > > > > class Result{ > //Data fields... > > } > public class Grid2 extends MouseAdapter implements PlugIn { > *Result result[][] = new Result[32][32];* > > > public void run(String arg) { > > for(int p=0;p<32;p++){ > for(int q=0;q<32;q++){ > result[p][q] = new Result(); > } > } > //*other codes here....which uses This array result[32][32]* > }//*run() ends here* > > /*/some other codes that uses result* > }//PlugIn ends here > > > this works nice for me.But i want that the array size is given by user > during run time ,so need to be defined inside* run()*.But if i do that i > loose my access over that array as soon as run() ends and can not use it > outside it.I hope i am clear about my problem.*I want to access the array > outside the run() block but at the same time declare it inside run().* > > Thank you all. > Ashish > > -- Juanjo Vega ([hidden email]) Unidad de Biocomputación. Laboratorio B-13. Centro Nacional de Biotecnología. CNB-CSIC. C\ Darwin, 3. Campus de Cantoblanco. Universidad Autónoma de Madrid. 28049, Madrid, Spain. http://www.cnb.csic.es http://www.biocomp.cnb.uam.es +34 91 585 4510 "Las mejores almas son capaces de los mayores vicios como de las mayores virtudes, y aquellos que caminan despacio por el camino recto pueden llegar más lejos que los que corren pero se apartan de él." - Discurso del Método, René Descartes. |
Thank you Juanjo Vega for your reply.I cam compile my plugin by doing what
you said but the problem is that as soon as the control leaves the run(), i think the all the memory associated with result[row][column] is freed automatically. So i am unable to access my previous data stored in that array. If you have any suggestions or ideas regarding this please reply me.It may be very much useful for me. Thanking you, Ashish |
I think what you have sent me is not fully correct.Please have a look on
this. class Result{ //Data fields... } public class Grid2 extends MouseAdapter implements PlugIn { Result result[][]; // <<<--- I declare it, but it has no memory assigned yet. public void run(String arg) { int rows, cols; // <<<< Here you get matrix size in some way: from a textfield, as a parameter at constructor, or whatever... result = new Result[rows][cols]; *// <<--- Now I assign memory for the matrix(but actually it wont allocate any memory because you //are not calling the constructor and in my program it is causing an error)* for(int p=0;p<32;p++){ for(int q=0;q<32;q++){ result[p][q] = new Result(); } } //*other codes here....which uses This array result[32][32]* }//*run() ends here* /*/some other codes that uses result* }//PlugIn ends here Please tell me about this. Thanking you, Ashish |
In reply to this post by ashish ram
> Thank you Juanjo Vega for your reply.I cam compile my plugin by doing what
> you said but the problem is that as soon as the control leaves the run(), i > think the all the memory associated with result[row][column] is freed > automatically. No. But I don't see how you use code of your plugin after leaving run(...). Let me guess: You want to have the results from the previous run-call available during the next run-call---true? If that's it, make the field static, it will then persist across all instantiations of your plugin. static Result[][] result; And, don't take me wrong, but I think your question should better be addressed to a java beginners form http://www.google.de/search?q=java+beginner+forum because there is nothing ImageJ specific in it. Best regards, Stephan > So i am unable to access my previous data stored in that > array. > > If you have any suggestions or ideas regarding this please reply me.It may > be very much useful for me. > Thanking you, > Ashish |
In reply to this post by ashish ram
Well, I'm calling the constructor with:
result = new Result[rows][cols]; After that, you have a matrix with rows*cols empty cells. Then you call the constructor for each cell as you are doing with: for(int p=0;p<32;p++){ for(int q=0;q<32;q++){ result[p][q] = new Result(); } } Are you sure the problem is because of that? ashish ram escribió: > I think what you have sent me is not fully correct.Please have a look on > this. > > class Result{ > //Data fields... > > } > public class Grid2 extends MouseAdapter implements PlugIn { > Result result[][]; // <<<--- I declare it, but it has no memory > assigned yet. > > > public void run(String arg) { > int rows, cols; > > // <<<< Here you get matrix size in some way: from a textfield, as a > parameter at constructor, or whatever... > > result = new Result[rows][cols]; *// <<--- Now I assign memory for > the matrix(but actually it wont allocate any memory because you //are not > calling the constructor and in my program it is causing an error)* > > > for(int p=0;p<32;p++){ > for(int q=0;q<32;q++){ > result[p][q] = new Result(); > } > } > //*other codes here....which uses This array result[32][32]* > }//*run() ends here* > > /*/some other codes that uses result* > }//PlugIn ends here > > > Please tell me about this. > Thanking you, > Ashish > > -- Juanjo Vega ([hidden email]) Unidad de Biocomputación. Laboratorio B-13. Centro Nacional de Biotecnología. CNB-CSIC. C\ Darwin, 3. Campus de Cantoblanco. Universidad Autónoma de Madrid. 28049, Madrid, Spain. http://www.cnb.csic.es http://www.biocomp.cnb.uam.es +34 91 585 4510 "Las mejores almas son capaces de los mayores vicios como de las mayores virtudes, y aquellos que caminan despacio por el camino recto pueden llegar más lejos que los que corren pero se apartan de él." - Discurso del Método, René Descartes. |
Hello,
I accept that i dont know Java up to a great depth and for sure i did have no intention to say that you were wrong rather i was asking my doubt. I really appreciate your help that you tried to clear my mistakes. Thank you. |
Don't worry, dude. I'm not good in english either ;), so maybe you
misunderstood me. I just meant that it worked for me, and being a so simple code I think the problem is not with that. I'm glad to help you :) If you want, write me directly, so we don't annoy the rest of people with non imageJ, but java stuff. Sincerelly, Juanjo. ashish ram escribió: > Hello, > I accept that i dont know Java up to a great depth and for sure i did have > no intention to say that you were wrong rather i was asking my doubt. I > really appreciate your help that you tried to clear my mistakes. > > Thank you. > > -- Juanjo Vega ([hidden email]) Unidad de Biocomputación. Laboratorio B-13. Centro Nacional de Biotecnología. CNB-CSIC. C\ Darwin, 3. Campus de Cantoblanco. Universidad Autónoma de Madrid. 28049, Madrid, Spain. http://www.cnb.csic.es http://www.biocomp.cnb.uam.es +34 91 585 4510 "Las mejores almas son capaces de los mayores vicios como de las mayores virtudes, y aquellos que caminan despacio por el camino recto pueden llegar más lejos que los que corren pero se apartan de él." - Discurso del Método, René Descartes. |
Free forum by Nabble | Edit this page |