Source Code. Projects. Nerd Stuff. Art Stuff.

Strings, Text, PFont, Parsing Strings

Here are Mimi’s examples on text functionality in Processing.

[gn_spoiler title="charAt source code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Print the characters in a sentence, one at a time
* at approximately the x-location that the character
* appears in the sentence.
*
* Makes use of String.charAt() and charWidth() functions.
*/


String sentence, thisChar;
int charIndex;
float charWidth;

void setup() {
  size(400, 200);
  fill(0);
  textSize(24);

  sentence = "I want to spell this out one character at a time.";
  thisChar = "";
  charIndex = 0;
 
  // Average width of each character
  charWidth = textWidth(sentence)/sentence.length();
}

void draw() {
  background(255);
 
  // Only print a character every 10 frames
  if (frameCount % 10 == 0) {
   
    // Loop around to the beginning when you get to the last character in the sentence
    if (charIndex >= sentence.length()-1)
      charIndex = 0;
    else
      charIndex++;
   
    // Convert character to string
    thisChar = str(sentence.charAt(charIndex));
  }
 
  // Display character
  // Calculate approximate x-position of character
  text(thisChar, charIndex*charWidth + 10, height/3);
  text(sentence, 10, height/2);
}
[/gn_spoiler]

 

[gn_spoiler title="mouseBooleanVersusClick source code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Click button on and off with hover effect
* Makes use of mouseClicked()
*/

// Booleans to keep track of whether button is ON/OFF
// Whether your mouse is hovering over button
boolean isOn = false;
boolean isHovering = false;

// Button Attributes
float buttonXLoc, buttonYLoc;
int buttonWidth = 50;
int buttonHeight = 20;

void setup() {
  size(400, 200);
 
  // Center the button
  rectMode(CENTER);
  buttonXLoc = width/2;
  buttonYLoc = height/2;
 
}

void draw() {
  background(255);
  noStroke();
 
  // Check for hovering
  if(mouseX > buttonXLoc-buttonWidth/2 && mouseX < buttonXLoc + buttonWidth/2 && mouseY > buttonYLoc - buttonHeight/2 && mouseY < buttonYLoc + buttonHeight/2)
    isHovering = true;
  else
    isHovering = false;
 
  // If the button is on *or* you're hovering over it, make it green
  // How would you make the button turn grey when you click it off, but your mouse is still hovering over it?
  // Hint: you need a boolean to capture when the button has gone from on to off without leaving the hover area
  if(isOn || isHovering)
    fill(0, 255, 0);    
   
  else
    fill(128);
 
  rect(buttonXLoc, buttonYLoc, buttonWidth, buttonHeight);
 
}

void mouseClicked() {
    // Turn the button on/off if your mouse clicked and you're hovering over it
    if(isHovering)
      isOn = !isOn;
}
[/gn_spoiler]

 

There are more examples that require external files. Unzip them here.