writing text in a certain distance-space

edited October 2013 in How To...

Dear All, I am trying to write some words with giving a certain space each other...

for example:

Love (100 pxl space) Hate (100 pxl space) Crazy (100 pxl space) Bomb

this is the code that does not work / or is wrong;

String buttons []= { "Love", "Hate", "Crazy", "Bomb"}; 

for (int i = 0; i < buttons.length; i ++) {
    fill(204, 102, 0);  

    translate (................);  // give 100 pxl space 

    text (buttons[i], 0, 0);

Note: is it also possible giving different distances?

Thank u Best

Tagged:

Answers

  • edited October 2013 Answer ✓

    This is one way to do it.

    String[] buttons = { "Love", "Hate", "Crazy", "Bomb"};
    
    void setup() {
      
      size( 600, 200 );
      
    }
    
    void draw() {
      
      background(255);
      
      translate(0, height/2);
    
      for (int i = 0; i < buttons.length; i ++) {
        fill(204, 102, 0);
        translate (100,0);  // give 100 pxl space 
        text (buttons[i], 0, 0);
      }
      
    }
    

    You can of course give different translate values:

    String[] buttons = { "Love", "Hate", "Crazy", "Bomb"};
    
    int[] horizontalOffsets = { 5, 200, 60, 120 };
    
    void setup() {
      
      size( 600, 200 );
      
    }
    
    void draw() {
      
      background(255);
      
      translate(0, height/2);
    
      for (int i = 0; i < buttons.length; i ++) {
        fill(204, 102, 0);
        translate (horizontalOffsets[i],0);
        text (buttons[i], 0, 0);
      }
      
    }
    
    
  • Dear sableRaph, Thank you so much, everything works great... best

  • You're welcome.

    Remark: The space you give using translate() is not the distance between two words but the distance of one word from the left point of the previous word. For more control, you will want to use the textWidth() method (http://processing.org/reference/textWidth_.html).

Sign In or Register to comment.