Use the XML below to initialize an array of objects. Design the objects to use all of the values in each XML element. (Feel free to rewrite the XML document to include more or less data.)
<?xml version="1.0"?>
<blobs>
<blob>
<location x="99" y="192"/>
<speed x="-0.88238335" y="2.2704291"/>
<size w="38" h="10"/>
</blob>
<blob>
<location x="97" y="14"/>
<speed x="2.8775783" y="2.9483867"/>
<size w="81" h="43"/>
</blob>
<blob>
<location x="159" y="193"/>
<speed x="-1.2341062" y="0.44016743"/>
<size w="19" h="95"/>
</blob>
<blob>
<location x="102" y="53"/>
<speed x="0.8000488" y="-2.2791147"/>
<size w="25" h="95"/>
</blob>
<blob>
<location x="152" y="181"/>
<speed x="1.9928784" y="-2.9540048"/>
<size w="74" h="19"/>
<\blob
</blobs>
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Exercise 18-13: Use the XML document (blobs.xml) to initialize an array of objects.
// Design the objects to use all of the values in each XML element.
// (Feel free to rewrite the XML document to include more or less data.)
import processing.xml.*;
// An array of Blob objects
Blob[] blobs;
void setup() {
size(200,200);
smooth();
// Load an XML document
XMLElement xml = new XMLElement(this, "blobs.xml" );
// Getting the total number of Blob objects with getChildCount().
int totalblobs = xml.getChildCount();
// Make an array the same size
blobs = new Blob[totalblobs];
// Get all the child elements
XMLElement[] children = xml.getChildren();
for (int i = 0; i < children.length; i ++ ) {
// The location is child 0
XMLElement locationElement = children[i].getChild(0);
float x = locationElement.getFloatAttribute("x");
float y = locationElement.getFloatAttribute("y");
// The speed is child 1
XMLElement speedElement = children[i].getChild(1);
float xspeed = speedElement.getFloatAttribute("x");
float yspeed = speedElement.getFloatAttribute("y");
// The size is child 2
XMLElement sizeElement = children[i].getChild(2);
float w = sizeElement.getFloatAttribute("w");
float h = sizeElement.getFloatAttribute("h");
// Make a new Blob object with values from XML document
blobs[i] = new Blob(x,y,xspeed,yspeed,w,h);
}
}
void draw() {
background(255);
// Display and move all blobs
for (int i = 0; i < blobs.length; i++ ) {
blobs[i].display();
blobs[i].move();
}
}
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 18-9: Using Processing's XML library
// A Bubble class
class Blob {
float x,y;
float xspeed, yspeed;
float w,h;
Blob(float x_, float y_, float xspeed_, float yspeed_, float w_, float h_) {
x = x_;
y = y_;
xspeed = xspeed_;
yspeed = yspeed_;
w = w_;
h = h_;
}
// Display Bubble
void display() {
stroke(0);
fill(0,50);
ellipse(x,y,w,h);
}
// Bubble drifts upwards
void move() {
x += xspeed;
y += yspeed;
if (x > width+w) x = -w;
if (x < -w) x = width+w;
if (y > height+h) y = -h;
if (y < -h) y = height+h;
}
}








