Loading text files from the hard drive
This recipe will cover the basics of loading text files from the hard drive. You will also use this technique in some of the other recipes in this chapter.
Getting ready
You'll need a text file with a few lines of text. I've used the poem My life as a progress meter by fridge. You can read it on the open source poetry website, at http://opensourcepoetry.org/index.html?poemDisplay.php?poem_id=765. You need to add the text file to the data folder of your Processing sketch.
How to do it...
We'll start by declaring an array of the type String
. The loadStrings()
function inside the setup()
function will load the text file from the hard drive into the String
array.
String[] textLines; void setup() { size( 640, 200 ); smooth(); textLines = loadStrings("poem.txt"); noLoop(); }
In the draw()
function, we'll loop through the array and use the number of characters in each line of text to calculate the diameter for the ellipse we'll draw. Each line of...