Skip to content

Instantly share code, notes, and snippets.

@mikkoelo
Last active March 29, 2019 08:06
Show Gist options
  • Save mikkoelo/bd1a63d2ef69a19ec1e852830111a651 to your computer and use it in GitHub Desktop.
Save mikkoelo/bd1a63d2ef69a19ec1e852830111a651 to your computer and use it in GitHub Desktop.
//This processing-program generates random poems based on the words in a text-file in the program-folder.
//In this example, the text-file is called shakespeare.txt. You can add your own txt file to the program-folder and randomize words from that!
// Source for Shakespeare texts: http://shakespeare.mit.edu/.
// If questions, ask mikko@mehackit.org.
PFont f;
String[] lines;
String[] words;
String[] shakespeare = new String[1];
void setup() {
shakespeare[0] = "Shakespeare"; // Save one word to the final shakespeare-array to avoid nullPointer Exception.
size(200,200); // Size of the canvas.
f = createFont("Arial",16); // The font that is used and it's size.
lines = loadStrings("shakespeare.txt"); // Load the strings from the txt-file to an Array. One line in txt-file is now one string in the lines-array.
for (int i = 0 ; i < lines.length; i++) { // The words are added to different array line by line.
println(lines[i]); // Print the text to the monitor, line by line. This isn't mandatory.
words = split(lines[i], ' '); // One line (sentence) is separated to words and each word is saved to words-array as a separate object
for (int k = 0; k < words.length; k++){ // Saving the words to the shakespeare-array by appending the previous list. After this, all the words of the original file are separated as different strings to shakespeare-array.
shakespeare = append(shakespeare, words[k]);
}
}
frameRate(0.2);
}
void draw() {
background(222); // Background-colour of the canvas.
textFont(f,16); // Determining the font. You can edit the size!
fill(0); // Colour of the text is black
//text("Hello Strings!",10,100);
text(shakespeare[int(random(shakespeare.length))], 10, 10); // Printing the random words to canvas. You can edit the coordinates.
text(shakespeare[int(random(shakespeare.length))], 10, 25);
text(shakespeare[int(random(shakespeare.length))], 50, 25);
text(shakespeare[int(random(shakespeare.length))], 80, 25);
text(shakespeare[int(random(shakespeare.length))], 10, 40);
text(shakespeare[int(random(shakespeare.length))], 50, 40);
text(shakespeare[int(random(shakespeare.length))], 10, 55);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment