Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Up to this point I've shown you how to include shapes and colors into your sketches in Processing. On the other hand, there may be times that you'll actually want to include text. To this point we most have been showing text in the console beneath, but I want to show you how to actually include it in the sketch window itself. I'll start by just putting a little comment here at the top. And what I'm going to do is create a window, 600 x 200; I'm going to give it a black background; then I'm going to type in a string for the text that I want to include.
I put string with a capital S, because it is actually an object. Then I give the String variable a name. I'm going to call it wordText. It could be anything you want. And equals and then I put it in double quotes. Strings go in double quotes, char variables go in single quotes. I'll put here "A word is worth 1/1000th of a picture." So that's my text, and I finish with a semicolon. And then all I need to do is position the text, and I use that with the text function.
So, first I need to tell Processing what text it is that I want to position, because I may have more than one string. So I say I'm going to use in a wordText. Then I give it the X and the Y for the baseline of the first line of the text. In this case I'll just do 50 pixels over and I'll put it halfway down, and that is sufficient to put some text in. And what you have here is the default font and size. Now, it may be that there are times when you want to be able to change that stuff, and so I'm going to show you how to do a slightly more elaborate version that allows you to customize the font, the color, the size, as well as some options on placement.
And in fact, what I'm going to do is I'm going to save this one right here as the basic version, and then we will copy that, put it down here, and I'm just going to comment that out so it doesn't run right now. We'll have the elaborate version right beneath. Again, we're going to keep a lot of this stuff the same. I'm going to give this one a different color. I'm actually going to put in a little hex code here 302F2F. Great! And then what I'm going to do is I'm going to choose a font to use.
Now fonts work a little differently in Processing than in other programs you use, so you just kind of pick them and use the GUI dropdown. You actually have to create the font, or rather, you had to create a font file to be used with this. And so what we do is we need to come up here to Tools, and the first choice there is Create Font, and these are the choices that we have on this particular computer. Your choices may be different. So you just select one and you'll get some sample text. So for instance, here is GilSansMT, and I think what I'm going to do is I'm going to make this one not 48 point, but 20 point. Here we go.
And what it is going to do is it is going to create a file called GillSansMT-20.vlw, and that's going to be a font file for Processing. So, I'm going to hit OK, and where that's going to show up is in the Processing data folder. So now, for instance, if I go back to the desktop and I go to the folder for 08_01, you see I now have a data folder. And when I click open that data folder, there's my font file. And actually, I'm going to need that fonts name.
So I'm going to copy all of that with the extension, and I am going to go back to the Processing sketch, because what I am going to do here is I'm now going to create another object, or a composite variable PFont. Please note the capital P, capital F. Again, where Processing is case-specific, these things matter. Now this is going to be a variable. It's actually not--it's like a variable. And what we need to do is I'm declaring the variable and I need to give it a name, and I'm going to give it sampleFont. You can call whatever you want.
You could call it GilSans20. Now using fonts is a several-step process in Processing. You saw for instance that I had to create a font. I've created my PFont object, or composite variable here, but that simply declared it. Now I need to initialize it by loading the font. And so what I do is I put in sampleFont is equal to loadFont, so I'm going to load a font into that, and then I put in quotes the full name of the font with the extension that I got earlier, and that's "GilSansMT-20.vlw." And I think it's always easy is to simply copy and paste these things, so you get all the capitalization and the spacing correct. And it has to be in quotes, in parentheses, and then I finish from the semicolon.
After that I can then specify that I want to use that particular font in my next piece of text, so now I'm calling it. I also want to change the color of the font, and for that you use fill. I'm going to use a hex code FFE224, and then I'm going to place the text itself. The way we do that it's the way we did here early. I'm just going to copy that and bring it back down, and that should be sufficient to show it with a font and the colors. Perfect! So I'm in GilSans. It's in yellow on a dark gray background.
I've changed the size. I want to show you a few other things that you can do at the same time. Let's say I want to have a slightly longer text. I can put it here. I could position it as a separate line, but I can also use the little sneaky Java commands. I can do a blackslash that's right above the Enter key and then an n, and I can put a second line of text. And then let's also say that I want to change the alignment slightly. What I can do then is I can enter the text align function, so textAlign.
Now, textAlign gives you a number of options. You can type in left, which is the default, or center or right. I may just type in left. And the reason I would do that, even though it's the default, is because that's the horizontal line. You can also modify the vertical alignment, but there has to be a second argument, and that one gets to be either centered, top to bottom; or it can be along the baseline, which is the default; or it can be top or bottom. This one, I'm going to do CENTER, and you'll notice that these are in all caps. That's necessary. You can also change the leading, which is like the line spacing.
If I wanted to, I could do that like this. Now I'll put like 50 points in there and I can run that all now. And you see now I have two lines. They're spaced out. They're centered vertically, and this is a way to give you some of the options that you have for working with text in Processing to integrate them--especially useful for labels and axes when you start doing data visualizations. But there is another element to add some information and some interest here on sketches.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97501 Viewers
61 Video lessons · 84744 Viewers
71 Video lessons · 68922 Viewers
56 Video lessons · 101373 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.