How to break line using loadspring()?

Hello,
I’d like to split my text into Strings and to move the same Strings to new coordinates. First when I split the text I don’t manage to insert a line break, how should I proceed?
Thanks a lot in advance for your help.
Best,
L

PFont f;
String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ2019,.;:!?\n ";
String wordsList="FOLLY,WHAT,FOR,TO,IS,THE,WORD,ALL,GIVEN,IS,FROM,ALL,THAT,SEEING,THIS,SEE,GLIMPSE,NEED,SEEM,WHERE,OVER,AWAY,AFAR,AFAINT";
String []lines;
String message;
int posX = 20;
int posY = 50;
boolean [] drawWords = new boolean[wordsList.length()];
boolean drawText;
char upperCaseChar;

void setup() {
  size(1080, 1600);
  f = createFont("Arial", 25, true);
  textFont(f);
  lines = loadStrings("Beckett.txt");  //laden des zu analysierenden textes
  message= join(lines, " ");
  lines=split(message, ",");
  //message = message.replaceAll(",", "");
}

void draw() {

  background(0);
  translate(100, 100);
  smooth();
  posX=-40;
  posY=10;
  float[] sortPositionsX = new float[wordsList.length()];
  String [] words= split(message," ");
  for (int i=0; i<words.length; i++) {
    String word= words[i].toLowerCase();
    int index = wordsList.indexOf(word);
    if (index<0)index=0;
    float m=map(mouseX, 50, width-50, 0, 1);
    m= constrain(m, 0, 1);
    float sortX = sortPositionsX[index];
    float interX = lerp(posX, sortX, m);
    float sortY =index*30+30;
    float interY = lerp(posY, sortY, m);

    posX+=textWidth(words[i])+10;
      if (word.equals(",")) {
        posY+=40;
    }

    textSize(40);
    fill(255);
    text(word, interX, interY);
  }
}


PFont f;
String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ2019,.;:!?\n ";
String wordsList="FOLLY,WHAT,FOR,TO,IS,THE,WORD,ALL,GIVEN,IS,FROM,ALL,THAT,SEEING,THIS,SEE,GLIMPSE,NEED,SEEM,WHERE,OVER,AWAY,AFAR,AFAINT";
String []lines = {"fdsj kdsfjfdslka mnmxcn abandbdf", 
  "dslkjf cmxv xcyv, mycnvjdsh ioweretu", 
  "asasassa qwqwq vvvvvv, lolklokkloklo zuzuzuzuzuz"};
String message;
int posX = 20;
int posY = -50;
boolean [] drawWords = new boolean[wordsList.length()];
boolean drawText;
char upperCaseChar;


void setup() {
  size(1080, 1600);
  f = createFont("Arial", 25, true);
  textFont(f);
  // lines = loadStrings("Beckett.txt");  //laden des zu analysierenden textes
  message= join(lines, " ");
  lines=split(message, ",");
  //message = message.replaceAll(",", "");
}

void draw() {

  background(0);
  translate(100, 100);
  smooth();
  posX = 40;
  posY = 50;

  float[] sortPositionsX = new float[wordsList.length()];
  String [] words= split(message, " ");

  for (int i=0; i<words.length; i++) {
    String word= words[i].toLowerCase();
    int index = wordsList.indexOf(word);
    if (index<0)
      index=0;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = sortPositionsX[index];
    float interX = lerp(posX, sortX, m);
    float sortY =index*30+30;
    float interY = lerp(posY, sortY, m);

    posX += textWidth(words[i])+10;

    if (word.equals(",") || word.indexOf(",")>0) {
      // new line 
      posX = 40; 
      posY += 40;
    }

    textSize(40);
    fill(255);
    text(word, 
      interX, posY);
  }
}
//

Not sure what you mean by loadspring().

Anyway, in this sketch we receive a line break by changing the xpos and ypos when the if-clause is true:

    if (word.equals(",") || word.indexOf(",")>0) {
      // new line 
      posX = 40; 
      posY += 40;
    }

    textSize(40);
    fill(255);
    text(word, 
      interX, posY);

Thank you very much dear @Chrisir,
well the loadspring() is just here to upload a ‘long’ text instead of including it in the code…
What I’d like to achieve ist to sort the most employed words in this Beckett’s poem !
Thanks you very much for the help!!

if you want to count the words in the poem(s):

I recommend HashMap - see reference.

First do the counting, then show the results

I can make an example




import java.util.Map;

PFont f;

String wordsList=
  "FOLLY,WHAT,FOR,TO,IS,THE,WORD,ALL,GIVEN,IS,FROM,ALL,THAT,SEEING,THIS,SEE,GLIMPSE,NEED,SEEM,WHERE,OVER,AWAY,AFAR,AFAINT";
String[] wordsList2; 

String []lines = {"fdsj kdsfjfdslka mnmxcn abandbdf,", 
  "dslkjf cmxv xcyv, mycnvjdsh ioweretu,", 
  "FOLLY,WHAT,FOR,TO,IS,THE,WORD,ALL,GIVEN,IS,FROM,ALL,THAT,SEEING,THIS,SEE,GLIMPSE,NEED,SEEM,WHERE,OVER,AWAY,AFAR,AFAINT,", 
  "FOLLY,WHAT,FOR,TO,IS,THE,WORD,ALL,GIVEN,IS,FROM,", 
  ",THE,WORD,ALL,GIVEN,IS,FROM,", 
  "asasassa qwqwq vvvvvv, lolklokkloklo zuzuzuzuzuz"};
String []lines2; 

int posX = 20;
int posY = -50;

//***********************************************************************************************
// Note the HashMap's "key" is a String and "value" is an Integer
HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
//***********************************************************************************************

void setup() {
  size(1080, 1600);

  f = createFont("Arial", 25, true);
  textFont(f);

  String message;
  // lines = loadStrings("Beckett.txt");  //laden des zu analysierenden textes
  message= join(lines, " ");
  lines2=split(message, ",");

  // Init 
  wordsList2 = split(wordsList, ","); 
  for (String s : wordsList2) {
    hm1.put(s.toLowerCase(), 0); // all 0
  }

  // Counting 
  for (int i=0; i<lines2.length; i++) {
    String word = lines2[i].toLowerCase().trim();
    if (hm1.get(word) != null) {
      hm1.put(word, ((int) (hm1.get(word)))+1);  // add 1
    }
  }
}

void draw() {
  background(0);
  posX = 40;
  posY = 50;

  // Showing 
  textSize(40);
  fill(255);
  //// Using an enhanced loop to iterate over each entry
  //for (Map.Entry me : hm1.entrySet()) {
  //  text(me.getKey() + "  ", 33, posY);
  //  text((me.getValue().toString()), 177, posY);
  //  posY += 40;
  //}//for

  for (String s : wordsList2) {
    if (hm1.get(s.toLowerCase()) != null) {
      text(s, 33, posY);
      text(hm1.get(s.toLowerCase()), 237, posY);
      posY += 40;
    }
  }
}//func 
//

Waooow! Thank you so much dear @Chrisir for this great example using HashMap !
Unfortunately I still have strange problems:
when using
posX+=textWidth(words[i])+10;
the spaces between theStrings (words[i]) gets weird, some are normal other are too wide or too narrow?!
How come? Thanks a lot. best, L

posX+=textWidth(words[i])+10;

It depends from the sketch:

  • e.g. in your first post you don’t use posX but interX - you use map()

  • also, you text() the variable word which is lowerCase but use upper case with textWidth. The two can’t match

Hey @Chrisir,

thanks for your reply andhelp as usual :))

“you text() word which is loweCase but use upper case with textWidth”

do you mean the textWidth() function use uppercase by default?!

Regarding the HashMap method, it’s really great and powerful and thank you again for spending some time

writing this example. I still don’t get it completely, but will try to work with it to change the textSize()

of my main text(). I managed to pass the value here, not to my main text… I’ll post my attempts asap!

    for (String s : wordsList2) {
if (hm1.get(s.toLowerCase()) != null) {
      textSize(hm1.get(s.toLowerCase())*5);      text(s, 33, posY);
text(hm1.get(s.toLowerCase()), 237, posY);
posY += 40;
}

Laurent Mareschal
https://laurentmareschal.com


P +33 (0)6.65.62.44.59

No.

I was referring to this line: posX+=textWidth(words[i])+10;

words[i] is not lower case

but you use text() with word and you made this lower case. So the text width textWidth(words[i]) is not matching the text you output with text(word…).

Remark

will try to work with it to change the textSize() of my main text().

see Word Cloud Threads - Processing 2.x and 3.x Forum

Remark

Remember to hit ctrl-t in processing prior to posting.

Dear @Chrisir,

I still have the same problem, even if all the texts are now in lowerCase…?!
Where does it come from?! Thanks a lot in advance. best, L

String wordsList="afaint,afar,all,and,away,folly,for,from,given,glimpse,is,need,over,see,seeing,seem,that,the,there,this,to,where,what,word,";
String[]lines;
String message;
int posX;
int posY;
boolean [] drawWords = new boolean[wordsList.length()];
boolean drawText;

//*****************************************************************************************************************************************

void setup() {
  size(1080, 1600);
  lines = loadStrings("Beckett.txt");  //laden des zu analysierenden textes
  message= join(lines, " ");
  lines=split(message, ",");
}

//*****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(100, 100);
  smooth();
  posX = 0;
  posY = 0;

  String [] words= split(message, " ");

  for (int i=0; i<words.length; i++) {
    String word= words[i];
    int index = wordsList.indexOf(word);
    if (index<0)
      index=0;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = 0;
    //float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX += textWidth(word)+20;

    if (word.equals(",") || word.indexOf(",")>0) {
      // new line 
      posX=0; 
      posY += 20;
    }
    textSize(20);
    fill(255);
    text(word, posX, interY);
  }
}

Forgot to ask you if you want me to send you the .txt file?!
Cheers,
L

I don’t know the answer

Maybe the textWidth function is not good

Also you should use textFont in setup

Or split lines?

Or use lines instead of words? Because you defined it in setup ()?

Thanks @Chrisir to try to help me.
The crazy thing is that the same sketch with char() instead of String() works?
Here is my last attempt:

String wordsList="afaint,afar,all,and,away,folly,for,from,given,glimpse,is,need,over,see,seeing,seem,that,the,there,this,to,where,what,word,";
String message;
String []lines;
boolean drawText;
PFont font;
String [] words;

//*****************************************************************************************************************************************

void setup() {
  size(1080, 1600);

  lines = loadStrings("Beckett.txt");  //link to the text
  message= join(lines, " "); // join all the words together
  lines= split(message, ","); //split the text line by line
  words = splitTokens(message, " "); // now split each line into words?!
  font = createFont("helvetica", 30);
  textFont(font);
  textAlign(LEFT);
}

//*****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(500, 100);
  smooth();
  int posX = 0;
  int posY = 0;

  for (int j=0; j<words.length; j++) {
   
    int index = wordsList.indexOf(words[j]);
    //if (index<0) continue;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    //float sortX = 0;
    //float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX +=textWidth(words[j])+10;
    
    if (words[j].equals(",") || words[j].indexOf(",")>0) {
      // new line 
      posX=0; 
      posY += 40;
    }

    fill(255);
    text(words[j], posX, interY);
  }
}

sorry, I don’t have time

but for the others that come after me:

maybe you can explain what your intention is.

Do you want to sort the text?

Do you want to Display the

  • “afaint,afar,all,and,away,folly,for,from,given,glimpse,is,need,over,see,seeing,seem,that,the,there,this,to,where,what,word,”?

Do you want to make an animation or not?

What is the mouse supposed to do?

maybe you can explain what your intention is.

Do you want to sort the text?
Yes in alphabetical order

Do you want to Display the

  • “afaint,afar,all,and,away,folly,for,from,given,glimpse,is,need,over,see,seeing,seem,that,the,there,this,to,where,what,word,”?
    no, just the text itself sorted according to this list itself already classified in alphabetical order

Do you want to make an animation or not?
yes, according to mouse movements

Want is the mouse supposed to do?
mouseX=50: the text is at its starting position, displayed to be read
mouseX=width-50: each word sorted will have a different position, A-words are the higest on screen, B-words just below, according to the index of each word…
And everything works as it is supposed to work except that the words when mouseX=50 are not displayed in the right order. I played with the text.file, the TexAlign(), function, but haven’t found…?!
Thanks @Chrisir for your help. Best, L

I don’t know

Chrisir

Dear @Chrisir, I’ve posted my question on the forum, hopefully someone, will be able to help me.
It’s really a stupid problem, but anyway…
Thanks,
L

1 Like

I don’t understand. You don’t need the “afaint,afar,all,and,away,folly,for,from,given…" list to sort. What’s its purpose???

mouseX=50: the text is at its starting position, displayed to be read
mouseX=width-50: each word sorted will have a different position

Do you mean this like text is not sorted mouseX=50: and is sorted mouseX=width-50 ?

So only 2 situations sorted yes/no?

OR

do you want every position between like when the mouse moves, the letters move slowly?