We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › TreeMap - problem accessing variable
Page Index Toggle Pages: 1
TreeMap - problem accessing variable (Read 592 times)
TreeMap - problem accessing variable
Apr 26th, 2009, 3:57pm
 
Hello everyone!
I'm doing a project about treemaps and I used treemap library and the example of words treemap explained by Ben Fry in Visualizing Data. I need to make some modifications, but i have a problem when I'm trying access  a variable.

In the main tab I wrote:

import treemap.*;
Treemap map;
void setup( ) {
 size(1150, 768);
 smooth( );
 strokeWeight(2f);
 PFont font = createFont("verdana", 9);
 textFont(font);
 WordMap mapData = new WordMap( );  
 String[] lines = loadStrings("data_update.txt");
 for (int i = 0; i < lines.length; i++) {
   mapData.addWord(lines[i]);
 }
 mapData.finishAdd( );  
 map = new Treemap(mapData, 0, 0, width, height);
 
 noLoop( );

 if (lines.length>0 && lines.length<11) {
 int c=234;
 }

}

void draw( ) {
 background(255);
 map.draw( );
}

And in WordItem tab I have:

public class WordItem extends SimpleMapItem {
String word;
WordItem(String word) {
this.word = word;
}
void draw( ) {
fill(c);
rect(x, y, w, h);
fill(23,45,90);
textAlign(CENTER, CENTER);
text(word, x + w/2, y + h/2);
 }
}

It's obvious that processing cannot find anything named "c". However I tried write public methods and classes ... but anything works fine Sad
Any suggestions?

Thanks a lot!


Re: TreeMap - problem accessing variable
Reply #1 - Apr 26th, 2009, 6:14pm
 
You are declaring the variable inside the 'if' statement. This means that it is only visible inside that statement.

Quote:
if (lines.length>0 && lines.length<11) {
  int c=234;
}


Try changing to:
Code:

//Declare the variable before setup()
int c = 0;
...
if (lines.length>0 && lines.length<11) {
  c=234;
}
...
Re: TreeMap - problem accessing variable
Reply #2 - Apr 27th, 2009, 5:27am
 
It's the same thing, return the same error: Cannot find anything named "c" . Processing doesn't recognize "c" in WordItem tab  Undecided

How can I transport the variable int c from the main tab to the WordItem tab?
Re: TreeMap - problem accessing variable
Reply #3 - Apr 27th, 2009, 6:28am
 
putting "int c" at the top of the code works for me...are you sure you put "int c" before the setup() (and not in it)? This makes it a global variable that is visible from anything that this code launches.
Re: TreeMap - problem accessing variable
Reply #4 - Apr 27th, 2009, 7:07am
 
Sorry NoahBuddy and danI !!!

I put "int c" after setup.
What a lame Roll Eyes

Thanks! Smiley
Page Index Toggle Pages: 1