Initial Commit

This commit is contained in:
plane000
2018-04-20 10:15:15 +01:00
parent 49150ccfe4
commit 62101e8e61
2870 changed files with 520122 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
/**
* ArrayList of objects
* by Daniel Shiffman.
*
* This example demonstrates how to use a Java ArrayList to store
* a variable number of objects. Items can be added and removed
* from the ArrayList.
*
* Click the mouse to add bouncing balls.
*/
ArrayList<Ball> balls;
int ballWidth = 48;
void setup() {
size(640, 360);
noStroke();
// Create an empty ArrayList (will store Ball objects)
balls = new ArrayList<Ball>();
// Start by adding one element
balls.add(new Ball(width/2, 0, ballWidth));
}
void draw() {
background(255);
// With an array, we say balls.length, with an ArrayList, we say balls.size()
// The length of an ArrayList is dynamic
// Notice how we are looping through the ArrayList backwards
// This is because we are deleting elements from the list
for (int i = balls.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
Ball ball = balls.get(i);
ball.move();
ball.display();
if (ball.finished()) {
// Items can be deleted with remove()
balls.remove(i);
}
}
}
void mousePressed() {
// A new ball object is added to the ArrayList (by default to the end)
balls.add(new Ball(mouseX, mouseY, ballWidth));
}

View File

@@ -0,0 +1,50 @@
// Simple bouncing ball class
class Ball {
float x;
float y;
float speed;
float gravity;
float w;
float life = 255;
Ball(float tempX, float tempY, float tempW) {
x = tempX;
y = tempY;
w = tempW;
speed = 0;
gravity = 0.1;
}
void move() {
// Add gravity to speed
speed = speed + gravity;
// Add speed to y location
y = y + speed;
// If square reaches the bottom
// Reverse speed
if (y > height) {
// Dampening
speed = speed * -0.8;
y = height;
}
}
boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
} else {
return false;
}
}
void display() {
// Display the circle
fill(0,life);
//stroke(0,life);
ellipse(x,y,w,w);
}
}

View File

@@ -0,0 +1,80 @@
/**
* CountingString example
* by Daniel Shiffman.
*
* This example demonstrates how to use a IntDict to store
* a number associated with a String. Java HashMaps can also
* be used for this, however, this example uses the IntDict
* class offered by Processing's data package for simplicity
* and added functionality.
*
* This example uses the IntDict to perform a simple concordance
* http://en.wikipedia.org/wiki/Concordance_(publishing)
*
*/
// An IntDict pairs Strings with integers
IntDict concordance;
// The raw array of words in
String[] tokens;
int counter = 0;
void setup() {
size(640, 360);
concordance = new IntDict();
// Load file and chop it up
String[] lines = loadStrings("dracula.txt");
String allText = join(lines, " ").toLowerCase();
tokens = splitTokens(allText, " ,.?!:;[]-\"");
// Create the font
textFont(createFont("SourceCodePro-Regular.ttf", 24));
}
void draw() {
background(51);
fill(255);
// Look at words one at a time
if (counter < tokens.length) {
String s = tokens[counter];
counter++;
concordance.increment(s);
}
// x and y will be used to locate each word
float x = 0;
float y = 48;
concordance.sortValues();
String[] keys = concordance.keyArray();
// Look at each word
for (String word : keys) {
int count = concordance.get(word);
// Only display words that appear 3 times
if (count > 3) {
// The size is the count
int fsize = constrain(count, 0, 48);
textSize(fsize);
text(word, x, y);
// Move along the x-axis
x += textWidth(word + " ");
}
// If x gets to the end, move y
if (x > width) {
x = 0;
y += 48;
// If y gets to the end, we're done
if (y > height) {
break;
}
}
}
}

View File

@@ -0,0 +1,82 @@
/**
* HashMap example
* by Daniel Shiffman.
*
* This example demonstrates how to use a HashMap to store
* a collection of objects referenced by a key. This is much like an array,
* only instead of accessing elements with a numeric index, we use a String.
* If you are familiar with associative arrays from other languages,
* this is the same idea.
*
* A simpler example is CountingStrings which uses IntDict instead of
* HashMap. The Processing classes IntDict, FloatDict, and StringDict
* offer a simpler way of pairing Strings with numbers or other Strings.
* Here we use a HashMap because we want to pair a String with a custom
* object, in this case a "Word" object that stores two numbers.
*
* In this example, words that appear in one book (Dracula) only are colored white
* while words the other (Frankenstein) are colored black.
*/
HashMap<String, Word> words; // HashMap object
void setup() {
size(640, 360);
// Create the HashMap
words = new HashMap<String, Word>();
// Load two files
loadFile("dracula.txt");
loadFile("frankenstein.txt");
// Create the font
textFont(createFont("SourceCodePro-Regular.ttf", 24));
}
void draw() {
background(126);
// Show words
for (Word w : words.values()) {
if (w.qualify()) {
w.display();
w.move();
}
}
}
// Load a file
void loadFile(String filename) {
String[] lines = loadStrings(filename);
String allText = join(lines, " ").toLowerCase();
String[] tokens = splitTokens(allText, " ,.?!:;[]-\"'");
for (String s : tokens) {
// Is the word in the HashMap
if (words.containsKey(s)) {
// Get the word object and increase the count
// We access objects from a HashMap via its key, the String
Word w = words.get(s);
// Which book am I loading?
if (filename.contains("dracula")) {
w.incrementDracula();
}
else if (filename.contains("frankenstein")) {
w.incrementFranken();
}
}
else {
// Otherwise make a new word
Word w = new Word(s);
// And add to the HashMap put() takes two arguments, "key" and "value"
// The key for us is the String and the value is the Word object
words.put(s, w);
if (filename.contains("dracula")) {
w.incrementDracula();
} else if (filename.contains("frankenstein")) {
w.incrementFranken();
}
}
}
}

View File

@@ -0,0 +1,72 @@
class Word {
// Store a count for occurences in two different books
int countDracula;
int countFranken;
// Also the total count
int totalCount;
// What is the String
String word;
// Where is it on the screen
PVector position;
Word(String s) {
position = new PVector(random(width), random(-height, height*2));
word = s;
}
// We will display a word if it appears at least 5 times
// and only in one of the books
boolean qualify() {
if ((countDracula == totalCount || countFranken == totalCount) && totalCount > 5) {
return true;
}
else {
return false;
}
}
// Increment the count for Dracula
void incrementDracula() {
countDracula++;
totalCount++;
}
// Increment the count for Frankenstein
void incrementFranken() {
countFranken++;
totalCount++;
}
// The more often it appears, the faster it falls
void move() {
float speed = map(totalCount, 5, 25, 0.1, 0.4);
speed = constrain(speed,0,10);
position.y += speed;
if (position.y > height*2) {
position.y = -height;
}
}
// Depending on which book it gets a color
void display() {
if (countDracula > 0) {
fill(255);
}
else if (countFranken > 0) {
fill(0);
}
// Its size is also tied to number of occurences
float fs = map(totalCount,5,25,2,24);
fs = constrain(fs,2,48);
textSize(fs);
textAlign(CENTER);
text(word, position.x, position.y);
}
}

View File

@@ -0,0 +1,99 @@
/**
* IntList Lottery example
* by Daniel Shiffman.
*
* This example demonstrates an IntList can be used to store a list of numbers.
* While an array of integers serves a similar purpose it is of fixed size. The
* An IntList can easily have values added or deleted and it can also be
* shuffled and sorted. For lists of floats or Strings, you can use FloatList
* and StringList. For lists of objects, use ArrayList.
*
* In this example, three lists of integers are created. One is a pool of numbers
* that is shuffled and picked randomly from. One is the list of "picked" numbers.
* And one is a lottery "ticket" which includes 5 numbers that are trying to be matched.
*/
// Three lists of integers
IntList lottery;
IntList results;
IntList ticket;
void setup() {
size(640, 360);
frameRate(30);
// Create empy lists
lottery = new IntList();
results = new IntList();
ticket = new IntList();
// Add 20 integers in order to the lottery list
for (int i = 0; i < 20; i++) {
lottery.append(i);
}
// Pick five numbers from the lottery list to go into the Ticket list
for (int i = 0; i < 5; i++) {
int index = int(random(lottery.size()));
ticket.append(lottery.get(index));
}
}
void draw() {
background(51);
// The shuffle() method randomly shuffles the order of the values in the list
lottery.shuffle();
// Call a method that will display the integers in the list at an x,y location
showList(lottery, 16, 48);
showList(results, 16, 100);
showList(ticket, 16, 140);
// This loop checks if the picked numbers (results)
// match the ticket numbers
for (int i = 0; i < results.size(); i++) {
// Are the integers equal?
if (results.get(i) == ticket.get(i)) {
fill(0, 255, 0, 100); // if so green
} else {
fill(255, 0, 0, 100); // if not red
}
ellipse(16+i*32, 140, 24, 24);
}
// One every 30 frames we pick a new lottery number to go in results
if (frameCount % 30 == 0) {
if (results.size() < 5) {
// Get the first value in the lottery list and remove it
int val = lottery.remove(0);
// Put it in the results
results.append(val);
} else {
// Ok we picked five numbers, let's reset
for (int i = 0; i < results.size(); i++) {
// Put the picked results back into the lottery
lottery.append(results.get(i));
}
// Clear the results and start over
results.clear();
}
}
}
// Draw a list of numbers starting at an x,y location
void showList(IntList list, float x, float y) {
for (int i = 0; i < list.size(); i++) {
// Use get() to pull a value from the list at the specified index
int val = list.get(i);
stroke(255);
noFill();
ellipse(x+i*32, y, 24, 24);
textAlign(CENTER);
fill(255);
text(val, x+i*32, y+6);
}
}

View File

@@ -0,0 +1,40 @@
// A Bubble class
class Bubble {
float x,y;
float diameter;
String name;
boolean over = false;
// Create the Bubble
Bubble(float x_, float y_, float diameter_, String s) {
x = x_;
y = y_;
diameter = diameter_;
name = s;
}
// CHecking if mouse is over the Bubble
void rollover(float px, float py) {
float d = dist(px,py,x,y);
if (d < diameter/2) {
over = true;
} else {
over = false;
}
}
// Display the Bubble
void display() {
stroke(0);
strokeWeight(2);
noFill();
ellipse(x,y,diameter,diameter);
if (over) {
fill(0);
textAlign(CENTER);
text(name,x,y+diameter/2+20);
}
}
}

View File

@@ -0,0 +1,111 @@
/**
* Loading JSON Data
* by Daniel Shiffman.
*
* This example demonstrates how to use loadJSON()
* to retrieve data from a JSON file and make objects
* from that data.
*
* Here is what the JSON looks like (partial):
*
{
"bubbles": [
{
"position": {
"x": 160,
"y": 103
},
"diameter": 43.19838,
"label": "Happy"
},
{
"position": {
"x": 372,
"y": 137
},
"diameter": 52.42526,
"label": "Sad"
}
]
}
*/
// An Array of Bubble objects
Bubble[] bubbles;
// A JSON object
JSONObject json;
void setup() {
size(640, 360);
loadData();
}
void draw() {
background(255);
// Display all bubbles
for (Bubble b : bubbles) {
b.display();
b.rollover(mouseX, mouseY);
}
//
textAlign(LEFT);
fill(0);
text("Click to add bubbles.", 10, height-10);
}
void loadData() {
// Load JSON file
// Temporary full path until path problem resolved.
json = loadJSONObject("data.json");
JSONArray bubbleData = json.getJSONArray("bubbles");
// The size of the array of Bubble objects is determined by the total XML elements named "bubble"
bubbles = new Bubble[bubbleData.size()];
for (int i = 0; i < bubbleData.size(); i++) {
// Get each object in the array
JSONObject bubble = bubbleData.getJSONObject(i);
// Get a position object
JSONObject position = bubble.getJSONObject("position");
// Get x,y from position
int x = position.getInt("x");
int y = position.getInt("y");
// Get diamter and label
float diameter = bubble.getFloat("diameter");
String label = bubble.getString("label");
// Put object in array
bubbles[i] = new Bubble(x, y, diameter, label);
}
}
void mousePressed() {
// Create a new JSON bubble object
JSONObject newBubble = new JSONObject();
// Create a new JSON position object
JSONObject position = new JSONObject();
position.setInt("x", mouseX);
position.setInt("y", mouseY);
// Add position to bubble
newBubble.setJSONObject("position", position);
// Add diamater and label to bubble
newBubble.setFloat("diameter", random(40, 80));
newBubble.setString("label", "New label");
// Append the new JSON bubble object to the array
JSONArray bubbleData = json.getJSONArray("bubbles");
bubbleData.append(newBubble);
if (bubbleData.size() > 10) {
bubbleData.remove(0);
}
// Save new data
saveJSONObject(json,"data/data.json");
loadData();
}

View File

@@ -0,0 +1,36 @@
{
"bubbles": [
{
"position": {
"x": 160,
"y": 103
},
"diameter": 43.19838,
"label": "Happy"
},
{
"position": {
"x": 372,
"y": 137
},
"diameter": 52.42526,
"label": "Sad"
},
{
"position": {
"x": 273,
"y": 235
},
"diameter": 61.14072,
"label": "Joyous"
},
{
"position": {
"x": 121,
"y": 179
},
"diameter": 44.758068,
"label": "Melancholy"
}
]
}

View File

@@ -0,0 +1,40 @@
// A Bubble class
class Bubble {
float x,y;
float diameter;
String name;
boolean over = false;
// Create the Bubble
Bubble(float x_, float y_, float diameter_, String s) {
x = x_;
y = y_;
diameter = diameter_;
name = s;
}
// CHecking if mouse is over the Bubble
void rollover(float px, float py) {
float d = dist(px,py,x,y);
if (d < diameter/2) {
over = true;
} else {
over = false;
}
}
// Display the Bubble
void display() {
stroke(0);
strokeWeight(2);
noFill();
ellipse(x,y,diameter,diameter);
if (over) {
fill(0);
textAlign(CENTER);
text(name,x,y+diameter/2+20);
}
}
}

View File

@@ -0,0 +1,83 @@
/**
* Loading Tabular Data
* by Daniel Shiffman.
*
* This example demonstrates how to use loadTable()
* to retrieve data from a CSV file and make objects
* from that data.
*
* Here is what the CSV looks like:
*
x,y,diameter,name
160,103,43.19838,Happy
372,137,52.42526,Sad
273,235,61.14072,Joyous
121,179,44.758068,Melancholy
*/
// An Array of Bubble objects
Bubble[] bubbles;
// A Table object
Table table;
void setup() {
size(640, 360);
loadData();
}
void draw() {
background(255);
// Display all bubbles
for (Bubble b : bubbles) {
b.display();
b.rollover(mouseX, mouseY);
}
textAlign(LEFT);
fill(0);
text("Click to add bubbles.", 10, height-10);
}
void loadData() {
// Load CSV file into a Table object
// "header" option indicates the file has a header row
table = loadTable("data.csv", "header");
// The size of the array of Bubble objects is determined by the total number of rows in the CSV
bubbles = new Bubble[table.getRowCount()];
// You can access iterate over all the rows in a table
int rowCount = 0;
for (TableRow row : table.rows()) {
// You can access the fields via their column name (or index)
float x = row.getFloat("x");
float y = row.getFloat("y");
float d = row.getFloat("diameter");
String n = row.getString("name");
// Make a Bubble object out of the data read
bubbles[rowCount] = new Bubble(x, y, d, n);
rowCount++;
}
}
void mousePressed() {
// Create a new row
TableRow row = table.addRow();
// Set the values of that row
row.setFloat("x", mouseX);
row.setFloat("y", mouseY);
row.setFloat("diameter", random(40, 80));
row.setString("name", "Blah");
// If the table has more than 10 rows
if (table.getRowCount() > 10) {
// Delete the oldest row
table.removeRow(0);
}
// Writing the CSV back to the same file
saveTable(table, "data/data.csv");
// And reloading it
loadData();
}

View File

@@ -0,0 +1,5 @@
x,y,diameter,name
160,103,43.19838,Happy
372,137,52.42526,Sad
273,235,61.14072,Joyous
121,179,44.758068,Melancholy
1 x y diameter name
2 160 103 43.19838 Happy
3 372 137 52.42526 Sad
4 273 235 61.14072 Joyous
5 121 179 44.758068 Melancholy

View File

@@ -0,0 +1,40 @@
// A Bubble class
class Bubble {
float x,y;
float diameter;
String name;
boolean over = false;
// Create the Bubble
Bubble(float x_, float y_, float diameter_, String s) {
x = x_;
y = y_;
diameter = diameter_;
name = s;
}
// CHecking if mouse is over the Bubble
void rollover(float px, float py) {
float d = dist(px,py,x,y);
if (d < diameter/2) {
over = true;
} else {
over = false;
}
}
// Display the Bubble
void display() {
stroke(0);
strokeWeight(2);
noFill();
ellipse(x,y,diameter,diameter);
if (over) {
fill(0);
textAlign(CENTER);
text(name,x,y+diameter/2+20);
}
}
}

View File

@@ -0,0 +1,118 @@
/**
* Loading XML Data
* by Daniel Shiffman.
*
* This example demonstrates how to use loadXML()
* to retrieve data from an XML file and make objects
* from that data.
*
* Here is what the XML looks like:
*
<?xml version="1.0"?>
<bubbles>
<bubble>
<position x="160" y="103"/>
<diameter>43.19838</diameter>
<label>Happy</label>
</bubble>
<bubble>
<position x="372" y="137"/>
<diameter>52.42526</diameter>
<label>Sad</label>
</bubble>
</bubbles>
*/
// An Array of Bubble objects
Bubble[] bubbles;
// A Table object
XML xml;
void setup() {
size(640, 360);
loadData();
}
void draw() {
background(255);
// Display all bubbles
for (Bubble b : bubbles) {
b.display();
b.rollover(mouseX, mouseY);
}
textAlign(LEFT);
fill(0);
text("Click to add bubbles.", 10, height-10);
}
void loadData() {
// Load XML file
xml = loadXML("data.xml");
// Get all the child nodes named "bubble"
XML[] children = xml.getChildren("bubble");
// The size of the array of Bubble objects is determined by the total XML elements named "bubble"
bubbles = new Bubble[children.length];
for (int i = 0; i < bubbles.length; i++) {
// The position element has two attributes: x and y
XML positionElement = children[i].getChild("position");
// Note how with attributes we can get an integer or float via getInt() and getFloat()
float x = positionElement.getInt("x");
float y = positionElement.getInt("y");
// The diameter is the content of the child named "diamater"
XML diameterElement = children[i].getChild("diameter");
// Note how with the content of an XML node, we retrieve via getIntContent() and getFloatContent()
float diameter = diameterElement.getFloatContent();
// The label is the content of the child named "label"
XML labelElement = children[i].getChild("label");
String label = labelElement.getContent();
// Make a Bubble object out of the data read
bubbles[i] = new Bubble(x, y, diameter, label);
}
}
// Still need to work on adding and deleting
void mousePressed() {
// Create a new XML bubble element
XML bubble = xml.addChild("bubble");
// Set the poisition element
XML position = bubble.addChild("position");
// Here we can set attributes as integers directly
position.setInt("x",mouseX);
position.setInt("y",mouseY);
// Set the diameter element
XML diameter = bubble.addChild("diameter");
// Here for a node's content, we have to convert to a String
diameter.setFloatContent(random(40,80));
// Set a label
XML label = bubble.addChild("label");
label.setContent("New label");
// Here we are removing the oldest bubble if there are more than 10
XML[] children = xml.getChildren("bubble");
// If the XML file has more than 10 bubble elements
if (children.length > 10) {
// Delete the first one
xml.removeChild(children[0]);
}
// Save a new XML file
saveXML(xml,"data/data.xml");
// reload the new data
loadData();
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<bubbles>
<bubble>
<position x="160" y="103"/>
<diameter>43.19838</diameter>
<label>Happy</label>
</bubble>
<bubble>
<position x="372" y="137"/>
<diameter>52.42526</diameter>
<label>Sad</label>
</bubble>
<bubble>
<position x="273" y="235"/>
<diameter>61.14072</diameter>
<label>Joyous</label>
</bubble>
<bubble>
<position x="121" y="179"/>
<diameter>44.758068</diameter>
<label>Melancholy</label>
</bubble>
</bubbles>

View File

@@ -0,0 +1,56 @@
/**
* Regular Expression example
* by Daniel Shiffman.
*
* This example demonstrates how to use matchAll() to create
* a list of all matches of a given regex.
*
* Here we'll load the raw HTML from a URL and search for any
* <a href=" "> links
*/
// Our source url
String url = "http://processing.org";
// We'll store the results in an array
String[] links;
void setup() {
size(640, 360);
// Load the links
links = loadLinks(url);
}
void draw() {
background(0);
// Display the raw links
fill(255);
for (int i = 0; i < links.length; i++) {
text(links[i],10,16+i*16);
}
}
String[] loadLinks(String s) {
// Load the raw HTML
String[] lines = loadStrings(s);
// Put it in one big string
String html = join(lines,"\n");
// A wacky regex for matching a URL
String regex = "<\\s*a\\s+href\\s*=\\s*\"(.*?)\"";
// The matches are in a two dimensional array
// The first dimension is all matches
// The second dimension is the groups
String[][] matches = matchAll(html, regex);
// An array for the results
String[] results = new String[matches.length];
// We want group 1 for each result
for (int i = 0; i < results.length; i++) {
results[i] = matches[i][1];
}
// Return the results
return results;
}

View File

@@ -0,0 +1,109 @@
/**
* Thread function example
* by Daniel Shiffman.
*
* This example demonstrates how to use thread() to spawn
* a process that happens outside of the main animation thread.
*
* When thread() is called, the draw() loop will continue while
* the code inside the function passed to thread() will operate
* in the background.
*
* For more about threads, see: http://wiki.processing.org/w/Threading
*/
// This sketch will load data from all of these URLs in a separate thread
String[] urls = {
"http://processing.org",
"http://www.processing.org/exhibition/",
"http://www.processing.org/reference/",
"http://www.processing.org/reference/libraries",
"http://www.processing.org/reference/tools",
"http://www.processing.org/reference/environment",
"http://www.processing.org/learning/",
"http://www.processing.org/learning/basics/",
"http://www.processing.org/learning/topics/",
"http://www.processing.org/learning/gettingstarted/",
"http://www.processing.org/download/",
"http://www.processing.org/shop/",
"http://www.processing.org/about/"
};
// This will keep track of whether the thread is finished
boolean finished = false;
// And how far along
float percent = 0;
// A variable to keep all the data loaded
String allData;
void setup() {
size(640, 360);
// Spawn the thread!
thread("loadData");
}
void draw() {
background(0);
// If we're not finished draw a "loading bar"
// This is so that we can see the progress of the thread
// This would not be necessary in a sketch where you wanted to load data in the background
// and hide this from the user, allowing the draw() loop to simply continue
if (!finished) {
stroke(255);
noFill();
rect(width/2-150, height/2, 300, 10);
fill(255);
// The size of the rectangle is mapped to the percentage completed
float w = map(percent, 0, 1, 0, 300);
rect(width/2-150, height/2, w, 10);
textSize(16);
textAlign(CENTER);
fill(255);
text("Loading", width/2, height/2+30);
}
else {
// The thread is complete!
textAlign(CENTER);
textSize(24);
fill(255);
text("Finished loading. Click the mouse to load again.", width/2, height/2);
}
}
void mousePressed() {
thread("loadData");
}
void loadData() {
// The thread is not completed
finished = false;
// Reset the data to empty
allData = "";
// Look at each URL
// This example is doing some highly arbitrary things just to make it take longer
// If you had a lot of data parsing you needed to do, this can all happen in the background
for (int i = 0; i < urls.length; i++) {
String[] lines = loadStrings(urls[i]);
// Demonstrating some arbitrary text splitting, joining, and sorting to make the thread take longer
String allTxt = join(lines, " ");
String[] words = splitTokens(allTxt, "\t+\n <>=\\-!@#$%^&*(),.;:/?\"\'");
for (int j = 0; j < words.length; j++) {
words[j] = words[j].trim();
words[j] = words[j].toLowerCase();
}
words = sort(words);
allData += join(words, " ");
percent = float(i)/urls.length;
}
String[] words = split(allData," ");
words = sort(words);
allData = join(words, " ");
// The thread is completed!
finished = true;
}

View File

@@ -0,0 +1,52 @@
/**
* Loading XML Data
* by Daniel Shiffman.
*
* This example demonstrates how to use loadXML()
* to retrieve data from an XML document via a URL
*/
// We're going to store the temperature
int temperature = 0;
// We're going to store text about the weather
String weather = "";
// Yahoo weather uses something called A WOEID (Where On Earth IDentifier)
// https://en.wikipedia.org/wiki/WOEID
// This is the WOEID for zip code 10003
String zip = "10003";
String woeid = "12761335";
PFont font;
void setup() {
size(600, 360);
font = createFont("Merriweather-Light.ttf", 28);
textFont(font);
// The URL for the XML document
String url = "http://query.yahooapis.com/v1/public/yql?format=xml&q=select+*+from+weather.forecast+where+woeid=" + woeid + "+and+u='F'";
// Load the XML document
XML xml = loadXML(url);
// Grab the element we want
XML forecast = xml.getChild("results/channel/item/yweather:forecast");
// Get the attributes we want
temperature = forecast.getInt("high");
weather = forecast.getString("text");
}
void draw() {
background(255);
fill(0);
// Display all the stuff we want to display
text("Zip: " + zip, width*0.15, height*0.33);
text("Todays high: " + temperature + "°F", width*0.15, height*0.5);
text("Forecast: " + weather, width*0.15, height*0.66);
}