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 › use item across class
Page Index Toggle Pages: 1
use item across class (Read 1364 times)
use item across class
May 10th, 2010, 4:46am
 
I've this two class:


Code:
class WordItem extends SimpleMapItem {
String word;

color c;
float hue;
float brightness;
PFont font = createFont ("Arial", 10);

WordItem(String word) {
this.word = word;
}
void draw( ) {
colorMode(HSB, hh*ww);
fill(hh*ww/22, hh*ww, hh*ww, w*h*5);
stroke(0, 0, hh*ww);
strokeWeight (0.25f);
rect(x, y, w, h);
colorMode(RGB, 255);
smooth();
fill(255);
if (w > textWidth(word.toUpperCase() + " " +int((w*h)/(ww*hh)*100)+"%") + 20) {
if (h > textAscent( ) + 6) {
textFont(font);
textAlign(LEFT, BOTTOM);
text(word.toUpperCase() + " " +int((w*h)/(ww*hh)*100)+"%", x+5, y+20);

}
}
}
}

class WordMap extends SimpleMapModel implements MapModel {
MapLayout algorithm = new SquarifiedLayout( );
HashMap words;

WordMap( ) {
words = new HashMap( );
}
void addWord(String word) {
WordItem item = (WordItem) words.get(word);
if (item == null) {
item = new WordItem(word);
words.put(word, item);
}
item.incrementSize( );
int Size = (int)item.getSize();
println (Size);
}
void finishAdd( ) {
items = new WordItem[words.size( )];
words.values( ).toArray(items);

}

}

}



How can I use the "Size" of WordMap class:

Code:
	int Size = (int)item.getSize();
println (Size);


in the WordItem class?
Re: use item across class
Reply #1 - May 10th, 2010, 4:56am
 
I am not sure I understand your question.
You ask how to implement getSize() method in the WordItem class?
How do you define size? By textWidth?
Where is the problem exactly?

BTW, you shouldn't put createFont() inside a class instantiation, if you plan to have many items: this is a quite expensive operation, taking a good chunk of memory, and you will have a new font instance per item. Better create the font in setup() and either use the global font, or pass it as parameter to the constructor if you plan to have items with different fonts.
Re: use item across class
Reply #2 - May 10th, 2010, 5:52am
 
Size in WordMap is the "Size" if "item" calculated with item.getSize(), no I want to use this value (Size of item) in the WordItem Class like so:

Code:
WordItem(String word) {
this.word = word;
   }
   void draw( ) {
   
   .....
......  
   text(word.toUpperCase() + item.getSize(), x+5, y+20);
......

......

}
}  


Re: use item across class
Reply #3 - May 10th, 2010, 7:39am
 
Ah, so you want to display the size information.
Except I still fail to understand what is this size. A number of items? A text length? A bounding box? How do you compute it? I see neither a size field nor a getSize() method in your code.
Re: use item across class
Reply #4 - May 10th, 2010, 9:42am
 
ok.... I have a count in WordMap based on Hashmap. With this:

Code:
void addWord(String word) {
WordItem item = (WordItem) words.get(word);
if (item == null) {
item = new WordItem(word);
words.put(word, item);
}
item.incrementSize( );
}


item is incremented every time a words is put in (by the main sketch)

then I extract the Size of the item with this:

Code:
int Size = (int)item.getSize();
println (Size);


Now I want to use this "Size" value in another class (WordItem)

Forgive me... but I don't know how explain this in another way.....
Re: use item across class
Reply #5 - May 10th, 2010, 1:42pm
 
no idea?!?
Re: use item across class
Reply #6 - May 10th, 2010, 2:43pm
 
Ah, so you need to count the number of occurrences of a given word.
Should look like:
Code:
class WordItem extends SimpleMapItem {
   String word;

   color c;
   float hue;
   float brightness;

   int size = 1; // At least one at creation time

   WordItem(String word) {
this.word = word;
   }
   void draw( ) {
// Same code
   }
   void incrementSize() {
size++;
   }
   int getSize() {
return size;
   }
}
Page Index Toggle Pages: 1