// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 18-9: Using Processing's XML library
import processing.xml.*;
// An array of Bubble objects
Bubble[] bubbles;
void setup() {
size(200,200);
smooth();
// Load an XML document
XMLElement xml = new XMLElement(this, "bubbles.xml" );
// Getting the total number of Bubble objects with getChildCount().
int totalBubbles = xml.getChildCount();
// Make an array the same size
bubbles = new Bubble[totalBubbles];
// Get all the child elements
XMLElement[] children = xml.getChildren();
for (int i = 0; i < children.length; i ++ ) {
// The diameter is child 0
XMLElement diameterElement = children[i].getChild(0);
// The diameter is the content of the first element while red and green are attributes of the second.
int diameter = int(diameterElement.getContent());
// Color is child 1
XMLElement colorElement = children[i].getChild(1);
int r = colorElement.getIntAttribute("red");
int g = colorElement.getIntAttribute("green");
// Make a new Bubble object with values from XML document
bubbles[i] = new Bubble(r,g,diameter);
}
}
void draw() {
background(255);
// Display and move all bubbles
for (int i = 0; i < bubbles.length; i++ ) {
bubbles[i].display();
bubbles[i].drift();
}
}
// 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;
float speed;
float r,g;
Bubble(float r_,float g_, float diameter_) {
x = random(width);
y = height;
r = r_;
g = g_;
diameter = diameter_;
}
// True or False if point is inside circle
boolean rollover(int mx, int my) {
if (dist(mx,my,x,y) < diameter/2) {
return true;
} else {
return false;
}
}
// Change Bubble variables
void change() {
r = constrain(r + random(-10,10),0,255);
g = constrain(g + random(-10,10),0,255);
diameter = constrain(diameter + random(-2,4),4,72);
}
// Display Bubble
void display() {
stroke(0);
fill(r,g,255,150);
ellipse(x,y,diameter,diameter);
}
// Bubble drifts upwards
void drift() {
y += random(-3,-0.1);
x += random(-1,1);
if (y < -diameter*2) {
y = height + diameter*2;
}
}
}