The Coder's Handbook

Strings and Text

TEXT BASICS

Displaying Text

void text(String message, int x, int y)

  • Draws the message at x, y


Code:

text("Hello, World!", 50, 50);

Size

void textSize(int value)

  • Sets the font size to the specified value


Code:

textSize(24);

text("Big Hello!", 50, 50);

Alignment

void textAlign(int alignX, int alignY)

  • Sets the horizontal alignment: LEFT, CENTER, or RIGHT

  • Sets the vertical alignment: TOP, BOTTOM, or CENTER


Code:

textAlign(CENTER, TOP);

text("My Game Name", 500, 100);

STRINGS

What is a String?

A string is a data type that is used to represent text. You can use it to store and manipulate messages.


Example Code


  • You can assign a String just like you would any other variable

  • To compare a string, you'll use .equals()

  • When you want to combine strings, you use the + operator.



Strings have a *ton* of features. For more information,

check out the official processing reference here.

Code:

String myName;


void setup()

{

myName = "Michael";

}


void draw()

{

if(myName.equals("Michael"))

{

text("Hello, " + myName);

}
else
{
text("You are not welcome!");
}

}

FONTS

Fonts

Changing a font consists of two steps:

  • Create a font and store it in a variable

  • Set the active font to that variable


Code:

PFont bigFont;


void setup()

{

bigFont = createFont("Georgia", 32);

}


void draw()

{

textFont(bigFont);

text("Hello, World!", 50, 50);

}

RESOURCES

Chapter 17 - Text

13.1 - Strings and Drawing Text