How to make Tag cloud with processing
in
Programming Questions
•
2 years ago
Hello community,
i am a beginner in processing and i am trying to do a tag cloud but i have a problem with the tags because the words mixed each other when i have transparent background in JAVA2D. I simply want an orange background with my tag cloud with white words.
If somebody can help, thank you very very much.
My code is this:
- PFont font;
- PImage img;
- String fontFile = "Swiss721BT-BoldRounded-48.vlw";//cambie la fuente
- int fSize =96;// a num +alto tags +pequeños por scale de abajo wordSize/fSize
- int maxSize = 50;//int maxSize = 192;
- int minSize = 15;//int minSize = 48;
- String wordFile = "innovacion.txt"; //texto
- String[] words;
- int[] count;
- int most;
- int least;
- float currentSize;
- int currentIndex;
- void setup(){
- size(298,183, JAVA2D);//P2D);
- img = loadImage("degrad.jpg");
- colorMode(HSB, TWO_PI, 1, 1, 1);//modo HSB,matiz 2pi,saturacion, brillo y alpha 0<x>1
- rectMode(CORNER);
- //background(color(PI/6,0.94,0.86));//color de fondo valores entre 0 y 1 gris(0,0,0.7)
- smooth();
- font = loadFont(fontFile);
- initializeWords();
- noLoop();
- }
- void draw()
- {
- image (img,0,0);
- while(currentIndex < words.length) {
- float relsize = map(count[currentIndex],least,most,minSize,maxSize);
- boolean drawn = false;
- while (!drawn) {
- drawn = drawWord(words[currentIndex], relsize);
- if (!drawn)
- println("redrawing "+words[currentIndex]);
- relsize = relsize * 0.95;
- }
- currentIndex++;
- }
- }
- boolean drawWord(String word, float wordSize) {
- int intSize = (int)wordSize;
- textFont(font, wordSize);
- int w = int(textWidth(word));// valor del ancho de la palabra
- PGraphics g = createGraphics(w, intSize, JAVA2D);//P2D);//crea la caja de cada palabra entiendo yo
- g.beginDraw();
- //g.background(color(PI/6,0.94,0.86,0));//fondo de cada palabra,ultimo numero transparencia
- g.fill(color(0,0,99)); //g.fill(color(PI/6,0.94,0.86));//relleno de letra naranja
- g.textAlign(CENTER,CENTER);
- g.translate(w/2, wordSize/2);//traslada a x=anchopalabra/2, y=altoletra/2
- g.scale(wordSize / fSize*2);//(wordSize / fSize);//escala altura/96 mas diferencia entre unas y otras
- g.textFont(font);//fuente
- g.text(word, 0, 0);//palabra y posicion
- g.endDraw();
- /*
- PGraphics f = createGraphics(w, intSize, P2D);//P2D);//crea la caja de cada palabra entiendo yo
- f.beginDraw();
- f.background(color(PI/6,0.94,0.86,0));//fondo de cada palabra,ultimo numero transparencia
- f.fill(color(0,0,99)); //g.fill(color(PI/6,0.94,0.86));//relleno de letra naranja
- f.textAlign(CENTER,CENTER);
- f.translate(w/2, wordSize/2);//traslada a x=anchopalabra/2, y=altoletra/2
- f.scale(wordSize / fSize*2);//(wordSize / fSize);//escala altura/96 mas diferencia entre unas y otras
- f.textFont(font);//fuente
- f.text(word, 0, 0);//palabra y posicion
- f.endDraw();
- */
- PGraphics gMask = createGraphics(w, intSize, P2D); //JAVA2D);
- gMask.beginDraw();
- gMask.background(color(0,99,0));
- 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.8) {//brightness extrae el valor del brillo if (color (gMask.get (dx, dy)= color(0,0,0.7))){//
- if (brightness(get(x+dx, y+dy))<0.8) {//original <0.5
- fits = false;
- }
- }
- }
- }
- if (fits) {
- image(g, x, y);
- return true;
- }
- }
- return false;
- }
- boolean equalColor(color c1, color c2) {
- String h1 = hex(color(c1));
- String h2 = hex(color(c2));
- return h1.equals(h2);
- }
- void initializeWords() {
- ArrayList innovacion = new ArrayList();
- String[] innovacionStrs = loadStrings("innovacion.txt");//cambie el texto por este otro
- for (int i = 0; i < innovacionStrs.length; i++) {
- innovacion.add(innovacionStrs[i].trim().toUpperCase());
- }
- HashMap wordcount = new HashMap();
- String[] lines = loadStrings("innovacion.txt");//cambie el wordFile por el archivo de texto
- for (int i = 0; i < lines.length; i++) {
- String[] words = split(lines[i], " ");
- for (int j = 0; j < words.length; j++) {
- String word = clean(words[j]).toUpperCase();
- if (word.length() == 0) {
- continue;
- }
- if (innovacion.contains(word)) {
- continue;
- }
- Integer count = (Integer)wordcount.get(word);
- if (count == null) {
- wordcount.put(word, new Integer(1));
- }
- else {
- int newCount = count.intValue() + 1;
- wordcount.put(word, new Integer(newCount));
- }
- }
- }
- words = new String[wordcount.size()];
- count = new int[wordcount.size()];
- int idx = 0;
- Iterator it = wordcount.entrySet().iterator(); // Get an iterator
- while (it.hasNext()) {
- Map.Entry me = (Map.Entry)it.next();
- words[idx] = (String)me.getKey();
- count[idx] = ((Integer)(me.getValue())).intValue();
- idx++;
- }
- sortWords();
- String[] sorted = new String[words.length];
- for (int i = 0; i < words.length; i++) {
- sorted[i] = count[i]+" "+words[i];
- }
- most = count[0];
- least = count[count.length-1];
- //saveStrings("totals.txt", sorted);
- }
- String clean(String word) {
- word = word.trim();
- if (word.endsWith(".") || word.endsWith(",") || word.endsWith(";"))
- word = word.substring(0, word.length() - 1);
- return word.trim();
- }
- void sortWords() {
- boolean changed = true;
- while (changed) {
- boolean madeChange = false;
- for (int i = 0; i < count.length-1; i++) {
- if (count[i] < count[i+1]) {
- int temp = count[i];
- String tempW = words[i];
- count[i] = count[i+1];
- words[i] = words[i+1];
- count[i+1] = temp;
- words[i+1] = tempW;
- madeChange = true;
- }
- }
- changed = madeChange;
- }
- }
1