mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Cleaning examples for Beta launch
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// Learning Processing
|
||||
// Daniel Shiffman
|
||||
// http://www.learningprocessing.com
|
||||
|
||||
// Example 18-9: Using Processing's XML library
|
||||
|
||||
// A Bubble class
|
||||
class Bubble {
|
||||
|
||||
float x,y;
|
||||
float diameter;
|
||||
color c;
|
||||
Bubble(float r,float g, float b, float d) {
|
||||
x = width/2;
|
||||
y = height/2;
|
||||
c = color(r,g,b,150);
|
||||
diameter = d;
|
||||
}
|
||||
|
||||
// Display Bubble
|
||||
void display() {
|
||||
stroke(0);
|
||||
fill(c);
|
||||
ellipse(x,y,diameter,diameter);
|
||||
}
|
||||
|
||||
// Bubble drifts upwards
|
||||
void drift() {
|
||||
x += random(-1,1);
|
||||
y += random(-1,1);
|
||||
x = constrain(x,0,width);
|
||||
y = constrain(y,0,height);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Loading XML Data
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use loadXML()
|
||||
* to retrieve data from an XML document and make
|
||||
* objects from that data
|
||||
*
|
||||
* Here is what the XML looks like:
|
||||
*
|
||||
* <bubbles>
|
||||
<bubble>
|
||||
<diameter>40</diameter>
|
||||
<color red="75" green="255" blue="0"/>
|
||||
</bubble>
|
||||
</bubbles>
|
||||
*/
|
||||
|
||||
// An array of Bubble objects
|
||||
Bubble[] bubbles;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
smooth();
|
||||
// Load an XML document
|
||||
XML xml = loadXML("bubbles.xml");
|
||||
|
||||
// Get all the child elements
|
||||
XML[] children = xml.getChildren("bubble");
|
||||
|
||||
// Make an array of objects the same size
|
||||
bubbles = new Bubble[children.length];
|
||||
|
||||
for (int i = 0; i < children.length; i ++ ) {
|
||||
|
||||
// The diameter is the content of the child named "Diamater"
|
||||
XML diameterElement = children[i].getChild("diameter");
|
||||
int diameter = int(diameterElement.getContent());
|
||||
|
||||
// The color element has three attributes
|
||||
XML colorElement = children[i].getChild("color");
|
||||
// An int for r g and b
|
||||
int r = colorElement.getInt("red");
|
||||
int g = colorElement.getInt("green");
|
||||
int b = colorElement.getInt("blue");
|
||||
|
||||
// Make a new Bubble object with values from XML document
|
||||
bubbles[i] = new Bubble(r, g, b, diameter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
|
||||
// Display and move all bubbles
|
||||
for (int i = 0; i < bubbles.length; i++ ) {
|
||||
bubbles[i].display();
|
||||
bubbles[i].drift();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<?xml version="1.0"?>
|
||||
@@ -0,0 +1 @@
|
||||
mode=Standard
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 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 = "";
|
||||
|
||||
// The zip code we'll check for
|
||||
String zip = "10003";
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
|
||||
// The URL for the XML document
|
||||
String url = "http://xml.weather.yahoo.com/forecastrss?p=" + zip;
|
||||
|
||||
// Load the XML document
|
||||
XML xml = loadXML(url);
|
||||
|
||||
// Grab the element we want
|
||||
XML forecast = xml.getChild("channel").getChild("item").getChild("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 code: " + zip, 10, 160);
|
||||
text("Today's high: " + temperature, 10, 40);
|
||||
text("Forecast: " + weather, 10, 90);
|
||||
|
||||
// Draw a little thermometer based on the temperature
|
||||
stroke(0);
|
||||
fill(175);
|
||||
rect(10, 50, temperature*2, 20);
|
||||
}
|
||||
Reference in New Issue
Block a user