Loading...
Logo
Processing Forum
tokefrello's Profile
1 Posts
5 Responses
0 Followers

Activity Trend

Last 30 days

Loading Chart...

Show:
Private Message
    Hi all,

    I am trying to make a sketch that switches between two modes: Clearly displaying sentences and displaying sentences in a big mess. I have therefore tried to make a class that let's me manipulate the placement of words in their original sentence. I pasted the code, and you can look at it, but I think my most important question is this:

    Can I rely on textWidth() to make accurate placements of text?

    In the Story class, I try to use it in setting up a sentence "from scratch", but the words are being placed overlapping each other when they should be placed with neat spaces between them. I even tried a mono font.

    (And yes, I know the code is messy and uncommented. I think the interesting area is the .display() function of the Story class.)

    1. /***
    2. Used for putting text in a big pile
    3. ***/

    4. Story[] stories;
    5. String[] strings;

    6. void setup() {
    7.   size(750, 750);
    8.   background(255);
    9.   stroke(0);
    10.   fill(0);
    11.   rectMode(CORNER);
    12.   
    13.   strings = loadStrings("kartoffeltxt01.txt");
    14.   stories = new Story[strings.length];
    15.   textFont(createFont("AndaleMono", 11));
    16.   textAlign(CENTER, CENTER);
    17.   for (int i = 0; i < strings.length; i++) {
    18.     stories[i] =
    19.     new Story(strings[i],
    20.     new PVector(random(width-40),
    21.     random(height)));
    22.   }
    23. }

    24. void draw() {
    25.   background(255);
    26.   rectMode(CORNER);
    27.   float a = map(mouseX, 0, width, 0, 15);
    28.   for (int j = 0; j < stories.length; j++) {
    29.     stories[j].display();
    30.     //text(stories[j].fullText, stories[j].presentLoc.x, stories[j].presentLoc.y+30);
    31.     stories[j].update();
    32.   }
    33. }

    34. class Story {
    35.   String fullText;
    36.   String[] words;
    37.   
    38.   PVector orgLoc;
    39.   PVector presentLoc;
    40.   
    41.   Story(String tempText, PVector tempLoc) {
    42.     fullText = tempText;
    43.     words = split(fullText, 'X');
    44.     
    45.     orgLoc = tempLoc;
    46.     presentLoc = orgLoc;
    47.   }
    48.   
    49.   void update() {
    50.     float inc = map(mouseX, 0, width, 0, 1);
    51.     PVector m = new PVector(width/2, height/2);
    52.     PVector p = PVector.sub(m, orgLoc);
    53.     p.mult(inc);
    54.     presentLoc = PVector.add(m, p);
    55.   }
    56.   
    57.   void display() {
    58.     float space = textWidth(' ');
    59.     rectMode(CORNER);
    60.     for (int i = 0; i < words.length; i++) {
    61.       if (i == 0) {
    62.         text(words[i], presentLoc.x, presentLoc.y);
    63.       } else {
    64.         float combinedWidth = 0;
    65.         for (int j = 0; j < i; j++) {
    66.           combinedWidth += textWidth(words[j])+space;
    67.         }
    68.         text(words[i], presentLoc.x + combinedWidth, presentLoc.y);
    69.         /*
    70.         fill(0, 30);
    71.         noStroke();
    72.         rect(presentLoc.x, presentLoc.y + 20, combinedWidth, 10);
    73.         fill(0);
    74.         stroke(0);
    75.         */
    76.       }
    77.     }
    78.   }
    79. }