XMLElemement not working in online applet
in
Programming Questions
•
2 years ago
Hey Folks,
I'm working on a little boids modification that gets the number of boids from a nasdaq rss feed using XMLElement. The problem is that the code works great on my computer, but doesn't work at all when I put it online (I've tried in safari, firefox, and chrome). I'm putting both the processing code and the applet code on here for reference. I had originally tried doing this with the network library until I realized that the network library makes no claims to work on web, but it looks like XMLElement should, help?
Thanks so much!
// Modified Boids code
XMLElement xml;
int xmlIndex = 24;
Flock flock;
int numBoids; // 150, 300
PImage parchment;
PVector m;
boolean init = false;
void setup() {
size(300, 300);
parchment = loadImage("parch.jpg");
noCursor();
flock = new Flock();
smooth();
noFill();
xml = new XMLElement(this, "http://www.nasdaq.com/aspxcontent/NasdaqRSS.aspx?data=quotes&symbol=UDOW");
XMLElement[] table = xml.getChildren("channel/item/description");
String data = table[0].getContent();
String[] pairs = splitTokens(data, "<>\n");
int numBoids = int(pairs[xmlIndex]);
if (numBoids < 10) { numBoids = 300; }
for (int i = 0; i < numBoids; i++) {
flock.addBoid(new Boid(new PVector(width/2,height/2), 2.0, 0.2)); // 3.0, 0.05
}
}
void draw() {
//initFlock();
tint(255,100);
image(parchment,0,0);
flock.run();
}
// Add a new boid into the System
void mousePressed() {
//flock.addBoid(new Boid(new PVector(mouseX,mouseY),2.0f,0.05f));
}
// The Boid class
class Boid {
PVector loc;
PVector vel;
PVector acc;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Boid(PVector l, float ms, float mf) {
acc = new PVector(0,0);
vel = new PVector(random(-1,1),random(-1,1));
loc = l.get();
r = 2.0;
maxspeed = ms;
maxforce = mf;
}
void run(ArrayList boids) {
flock(boids);
update();
borders();
render();
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList boids) {
PVector sep = separate(boids); // Separation
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(1.4); // 1.5
ali.mult(1.0); // 1.0
coh.mult(1.2); // 1.0
// Add the force vectors to acceleration
acc.add(sep);
acc.add(ali);
acc.add(coh);
}
// Method to update location
void update() {
// Update velocity
vel.add(acc);
// Limit speed
vel.limit(maxspeed);
loc.add(vel);
// Reset accelertion to 0 each cycle
acc.mult(0);
}
void seek(PVector target) {
acc.add(steer(target,false));
}
void arrive(PVector target) {
acc.add(steer(target,true));
}
void avoid(PVector target) {
acc.sub(steer(target, false));
}
// A method that calculates a steering vector towards a target
// Takes a second argument, if true, it slows down as it approaches the target
PVector steer(PVector target, boolean slowdown) {
PVector steer; // The steering vector
PVector desired = target.sub(target,loc); // A vector pointing from the location to the target
float d = desired.mag(); // Distance from the target is the magnitude of the vector
// If the distance is greater than 0, calc steering (otherwise return zero vector)
if (d > 0) {
// Normalize desired
desired.normalize();
// Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
else desired.mult(maxspeed);
// Steering = Desired minus Velocity
steer = target.sub(desired,vel);
steer.limit(maxforce); // Limit to maximum steering force
}
else {
steer = new PVector(0,0);
}
return steer;
}
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = vel.heading2D() + PI/2;
stroke(100, 50); // 255
strokeWeight(2);
//color c = get(int(loc.x), int(loc.y));
//float b = brightness(c);
//stroke(b-100,150);
pushMatrix();
translate(loc.x,loc.y);
rotate(theta);
line(0,-r*1,0,r*1);
popMatrix();
}
// Keep in frame
void borders() {
int edge = 30;
m = new PVector(mouseX, mouseY);
if (loc.x < edge ||loc.y < edge ||loc.x > width-edge || loc.y > height-edge) seek(m);
float d = loc.dist(m);
if (d < 30) {avoid(m); avoid(m); avoid(m);}
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList boids) {
float desiredseparation = 20.0;
PVector steer = new PVector(0,0,0);
int count = 0;
// For every boid in the system, check if it's too close
for (int i = 0 ; i < boids.size(); i++) {
Boid other = (Boid) boids.get(i);
float d = PVector.dist(loc,other.loc);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(loc,other.loc);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div((float)count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(vel);
steer.limit(maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList boids) {
float neighbordist = 25.0;
PVector steer = new PVector(0,0,0);
int count = 0;
for (int i = 0 ; i < boids.size(); i++) {
Boid other = (Boid) boids.get(i);
float d = PVector.dist(loc,other.loc);
if ((d > 0) && (d < neighbordist)) {
steer.add(other.vel);
count++;
}
}
if (count > 0) {
steer.div((float)count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(vel);
steer.limit(maxforce);
}
return steer;
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
PVector cohesion (ArrayList boids) {
float neighbordist = 25.0;
PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations
int count = 0;
for (int i = 0 ; i < boids.size(); i++) {
Boid other = (Boid) boids.get(i);
float d = loc.dist(other.loc);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.loc); // Add location
count++;
}
}
if (count > 0) {
sum.div((float)count);
return steer(sum,false); // Steer towards the location
}
return sum;
}
}
// The Flock (a list of Boid objects)
class Flock {
ArrayList boids; // An arraylist for all the boids
Flock() {
boids = new ArrayList(); // Initialize the arraylist
}
void run() {
for (int i = 0; i < boids.size(); i++) {
Boid b = (Boid) boids.get(i);
b.run(boids); // Passing the entire list of boids to each boid individually
}
}
// Applet code I'm using in an MySQL database - this has worked well for other sketches.
<APPLET name="applet6" WIDTH="300" HEIGHT="300" archive=" http://matthewmosher.us/processing/applet5/boidsClean2.jar" standby="Loading Processing software�" codebase=" http://matthewmosher.us/processing/applet6/" code="boidsClean2" mayscript="true" scriptable="true" image=" http://matthewmosher.us/processing/applet6/loading.gif" boxmessage="Loading Processing software�" boxcolor="#FFFFFF"> <IMG SRC=" http://matthewmosher.us/processing/applet6/loading.gif" WIDTH="1" HEIGHT="1" /> </APPLET>
1