Line break in strings

edited April 2017 in Library Questions

Hi, i was editing some geomerative's codes but i'm not able to line-break this. Could you help me with this example? i would like to write a sentence in the canvas and distorce it with mouse over. Thank you for your help

import processing.pdf.*;
import geomerative.*;
PGraphics pg;
RFont myFont;
RGroup myGroup;
RPoint[] myPoints;
String myText = "hello \n hello";
FontAgent[] myAgents;
int step = 0;
int dim;

boolean stopAnime = false;

void setup() {
  size(1050, 1050);
  background(255);
  smooth();
  ellipseMode(RADIUS);


  RG.init(this);
  myFont = new RFont("relative.ttf",100,RFont.CENTER);


  RCommand.setSegmentLength(2);
  RCommand.setSegmentator(RCommand.UNIFORMLENGTH);
  myGroup = myFont.toGroup(myText);
  myPoints = myGroup.getPoints();

  myAgents = new FontAgent[myPoints.length];
  for (int i=0; i<myPoints.length; i++) {
    myAgents[i] = new FontAgent(new PVector(myPoints[i].x, myPoints[i].y));
  }
}


void draw() {
  translate(width/2, height/2);
  background(0);
  fill(0);

  for (int i = 0; i < myPoints.length; i++) {
    myAgents[i].display();
    myAgents[i].motion();
  }
  //saveFrame("line-######.png");

}

void keyPressed() {
  if (key == 'f' || key == 'F') 
    stopAnime = !stopAnime;
  if (stopAnime == true)
    noLoop();
  else loop();

  if (key == '+')
    step++;
  println(step);

  if (key == '-')
    step--;
} 



class FontAgent {

  PVector loc;
  float motion;

  FontAgent(PVector l) {
    loc = l.get();
  }

  void motion() {

    float noiseScale = map(mouseX, mouseY, width, 0.001, 0.01);       



    motion = noise(loc.x * noiseScale, loc.y * noiseScale ) * 25;

  }  
  //-7  -16


  void display() {
    noStroke();
    fill(255);
    //noFill();
       // stroke(1);

    rect(loc.x, loc.y, motion+step, motion+step);
  }
} 

Answers

  • edited April 2017

    i can't figure out how to have both "hell " and "o " lines

       import processing.pdf.*;
        import geomerative.*;
        PGraphics pg;
        RFont myFont;
        RGroup myGroup;
        RPoint[] myPoints;
        //String myText = "ciao \n Ciao";
        //String s = "The quick brown fox jumped over the lazy dog.";
        int textHeight = 300;
        FontAgent[] myAgents;
        int step = 0;
        int dim;
        boolean stopAnime = false;
        String[] lines = {
          "hell", "o"
        };
    
        void setup() {
          size(800, 800);
          background(255);
          smooth();
          ellipseMode(RADIUS);
    
    
          RG.init(this);
          myFont = new RFont("relative.ttf", textHeight, RFont.CENTER);
          RCommand.setSegmentLength(1);
          RCommand.setSegmentator(RCommand.UNIFORMLENGTH);
    
          for (int z = 0; z < lines.length; z++) {
            myGroup = myFont.toGroup(lines[0+0]);
            myPoints = myGroup.getPoints();
            myAgents = new FontAgent[myPoints.length];
    
            for (int i=0; i<myPoints.length; i++) {
              myAgents[i] = new FontAgent(new PVector(myPoints[i].x, myPoints[i].y));
            }
          }
        }
    
    
        void draw() {
          translate(width/2, height/2.0+myFont.size/3.0); // optische mitte?
          background(0);
          fill(0);
    
          for (int i = 0; i < myPoints.length; i++) {
            myAgents[i].display();
            myAgents[i].motion();
          }
          //saveFrame("line-######.png");
        }
    
        void keyPressed() {
          if (key == 'f' || key == 'F') 
            stopAnime = !stopAnime;
          if (stopAnime == true)
            noLoop();
          else loop();
    
          if (key == '+')
            step++;
          println(step);
    
          if (key == '-')
            step--;
        } 
    
    
    
        class FontAgent {
    
          PVector loc;
          float motion;
    
          FontAgent(PVector l) {
            loc = l.get();
          }
    
          void motion() {
    
            float noiseScale = map(mouseX, mouseY, width, 0.001, 0.01);       
    
    
    
            motion = noise(loc.x * noiseScale, loc.y * noiseScale ) * 45;
          }  
          //-7  -16
    
    
          void display() {
            noStroke();
            fill(255);
            //noFill();
            // stroke(1);
    
            rect(loc.x, loc.y, motion+step, motion+step);
          }
        } 
    
  • When you format your code, make sure there is an empty line above and below your code block. You can edit your posts by clicking on the gear icon on the top right corner of each post.

    Kf

  • edited April 2017

    ah sorry, i now fixed that

  • any help please??

  • edited April 2017

    A 2D array might work. You may want to look into the idea.

Sign In or Register to comment.