I am a student at the University of Maine Orono as a New Media student and this is my first semester working with processing. For our most current project, we need to create a tag cloud.
The project is to create a tag cloud that will be able to analyze any text document that you load into processing and allow it to display in any manner you choose.
My tag cloud is influenced by this code:
w w w .openprocessing .o r g /visuals/ ?visualID=7042I enjoy has it is overall pieced together, but what I wanted to do is allow the text to to randomly placed, as well as appear vertically and horizontally, all while
not overlapping. This is where I run into a problem, the "collision" code is not working, so they are placed wherever (and overlaps).
Can anybody please help and maybe provide some clarity on what had to be done? This would be much appreciated!
This is the full code:
h t t p:// slash203.2 5 0 f r e e .c o m /TagCloud.zip (please excuse the spaces in the url's)Thanks!
Code:
class ScreenManager{
int rows=0, columns=40; //Number of rows and columns on the screen
int rowHeight = 60; //Height of each row
int colWidth = 30; //Width of each column
PFont font, smallFont; //Fonts used for display
int cursorCol=0, cursorRow=1; //Current position of the cursor
StemmedWord[] words; //Array of alphabetically sorted words
String[] allWords; //Array of all words in the text
int letterCount=0; //Count of all letters in all words
int maxCount=0, minCount=1000;//Maximum and minimum count of any one word
int maxDist=50; //Maximum distance between maxCount and minCount
int maxWords=50; //The maximum number of words in the cloud
int arrayPos=0; //Current position of the text array counter
boolean complete=false; //Are all the words drawn?
int fSize= 96;
int maxSize = 92;
int minSize = 48;
int currentIndex;
int[] count;
int most;
int least;
ScreenManager(){
//Some basic setup tasks since this should be created once in setup
//this.font = loadFont("CenturyGothic-48.vlw");
this.font = loadFont("Serif-48.vlw"); //counter
textFont(font);
textMode(SCREEN);
smooth();
//this.colWidth = (int) textWidth("8");
background(color(255, 255, 254, 0));
}
void drawScreen(){
if(!this.complete){
for(int i=0; i<this.words.length; i++){
this.drawWord(this.words[i].word, this.words[i].count +fSize); //reading the drawWord and using the stems in 'Stemmed_Word'
float relsize = map(this.words[i].count,minCount,maxCount,minSize,maxSize);
boolean drawn = false;
while (!drawn) {
drawn = drawWord(words[i].word, relsize);
if (!drawn)
println("redrawing "+words[i].word);
relsize = relsize * 0.95;
}
}
}
noLoop();
}
//--------------------------------------
//void drawWord(StemmedWord word, boolean override){
//Draw a word on the screen (if override is true, always make it a bright color)
//---------------------------------
boolean drawWord(String word, float wordSize) {
int intSize = (int)wordSize;
textFont(font, wordSize);
int w = int(textWidth(word));
PGraphics g = createGraphics(w, intSize, P2D);
g.beginDraw();
g.background(color(255, 255, 254, 0));
g.fill(color(0));
g.textAlign(CENTER, CENTER);
// g.rotate(HALF_PI); //rotate words to be verticle or horizontal
g.translate(w/2, wordSize/2);
g.scale(wordSize / fSize);
g.textFont(this.font);
g.text(word, 0, 0);
g.endDraw();
// int x = (int)random(width-w);
// int y = (int)random(height-intSize);
// image(g,x,y);
// return true;
PGraphics gMask = createGraphics(w, intSize, P2D);
gMask.beginDraw();
//gMask.background(color(0, 0, 1, 1));
gMask.image(g, 0, 0);
gMask.filter(ERODE);
gMask.filter(ERODE);
gMask.endDraw();
for (int tries=50; tries>0; tries--) {
int x = (int)random(width-w);
int y = (int)random(height-intSize);
boolean fits = true;
for (int dx = 0; dx< w && fits; dx++) {
for (int dy = 0; dy<intSize && fits; dy++) {
if (brightness(gMask.get(dx, dy))<0.5) {
if (brightness(get(x+dx, y+dy))<0.5) {
fits = false;
}
}
}
}
if (fits) {
image(g, x, y);
return true;
}
}
return false;
}
void setText(String[] _allWords){
//Store the entire text being processed in the ScreenManager object
this.allWords = _allWords;
}
void setWords(StemmedWord[] _words){
//Save the passed array into the object, and count all the letters
//Also figure out the maximum and minimum number of times any one word appears
this.words = _words;
//Initialize max and min to extreme numbers
this.maxCount = 0;
this.minCount = 1000;
for(int i=0; i<this.words.length; i++){
if(!this.words[i].word.equals("")){
//add a space to the end of the word for display purposes
this.words[i].word += " ";
if(this.words[i].count > this.maxCount) this.maxCount = this.words[i].count;
if(this.words[i].count < this.minCount) this.minCount = this.words[i].count;
this.letterCount += this.words[i].length();
}
}
//Make sure the distance between max and min isn't too big
//or else the colors won't look very good
if(this.maxCount - this.minCount > this.maxDist) this.maxCount = this.minCount+this.maxDist;
}