We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Reading in XML and using it as frame information
Page Index Toggle Pages: 1
Reading in XML and using it as frame information (Read 1310 times)
Reading in XML and using it as frame information
Mar 9th, 2007, 3:41am
 
So I've got a java program that runs a GA and generates an XML file, and then Processing reads the XML and generates animation based on it. The trouble is, I've finally got it to compile, and I just get a blank canvas, with no animation or any error messages! Any help would be greatly appreciated!

Code:

import java.util.Vector;
import processing.xml.*;

PImage prey;
PImage pred;

Vector robots = new Vector();
XMLElement xml;

void setup()
{
//Read in the XML
xml = new XMLElement(this, "test.xml");

//Read in the images
prey = loadImage("prey.bmp");
pred = loadImage("pred.bmp");

//For each robot
for (int i = 0; i < xml.getChildCount(); i++) {
XMLElement currentBot = xml.getChild(i);
println(currentBot);
//Select the appropriate image
Robot current = new Robot(currentBot.getStringAttribute("type").equals("prey") ? prey : pred);
//Then for each frame of that robot
for ( int j = 0; j < currentBot.getChildCount(); j++) {
XMLElement currentFrame = currentBot.getChild(j);
Position pos = new Position(int(currentFrame.getChild(0).getContent()), int(currentFrame.getChild(1).getContent()), float(currentFrame.getChild(2).getContent()));
//Add the frame to its vector of positions
current.addFrame(pos);
}
}

size(800,600);
frameRate(1);
}

void draw()
{
background(102);
for (int i = 0; i < robots.size(); i++) {
((Robot)robots.get(i)).drawNext();
}
}

//Class to aid storing of positions in a vector
class Position {
int x;
int y;
float rot;

Position(int x, int y, float rot){
this.x = x;
this.y = y;
this.rot = rot;
}

}

class Robot {
Vector frames = new Vector();
PImage sprite;

Robot(PImage sprite) {
this.sprite = sprite;
}

//Add a frame from the XML to the vector
void addFrame(Position pos){
frames.add(pos);
}

void drawNext() {
pushMatrix();
//Remove the next sequential frame to show
Position pos = (Position)frames.remove(0);
rotate(pos.rot);
image(sprite, pos.x, pos.y);
popMatrix();
}
}


I've been having some trouble with rotation already, maybe thats where its going wrong? Thanks in advance for any help.
Re: Reading in XML and using it as frame informati
Reply #1 - Mar 9th, 2007, 9:26am
 
have you tried to put size() as the first thing inside setup?
F
Re: Reading in XML and using it as frame informati
Reply #2 - Mar 9th, 2007, 11:10am
 
Oh ok, I'll give that a go when I get home later, why  is it that that should make a difference? I'm not very familiar with using processing.
Re: Reading in XML and using it as frame informati
Reply #3 - Mar 9th, 2007, 11:33am
 
well .. besides that the reference states so, i guess it's because java needs a graphicsContext ( from the frame ) or it needs the frame to make Toolkit work or PApplet needs a PGraphics (or all of them) to load the images.

if you're curious, have a look at the source (look for "void size").

give it a try and if it doesn't work post here ...
Re: Reading in XML and using it as frame informati
Reply #4 - Mar 9th, 2007, 10:37pm
 
No, that didn't change anything, but thanks for the suggestion anyway!
Re: Reading in XML and using it as frame informati
Reply #5 - Mar 10th, 2007, 12:31am
 
ok sorry, it was just a guess. can you put the sketch (applet) somewhere so i can look at the xml and images .. and test it here?

F
Re: Reading in XML and using it as frame informati
Reply #6 - Mar 10th, 2007, 2:31am
 
Re: Reading in XML and using it as frame informati
Reply #7 - Mar 10th, 2007, 9:59am
 
Code:

import java.util.Vector;
import processing.xml.*;

PImage prey;
PImage pred;

Vector robots = new Vector();
XMLElement xml;

void setup()
{
size(800,600);
//Read in the XML
xml = new XMLElement(this, "test.xml");

//Read in the images
// BMP is not supported by Processing!
prey = loadImage("prey.gif");
pred = loadImage("pred.gif");

//For each robot
for (int i = 0; i < xml.getChildCount(); i++) {
XMLElement currentBot = xml.getChild(i);
//println(currentBot);
//Select the appropriate image
Robot current = new Robot(currentBot.getStringAttribute("type").equals("prey") ? prey : pred);
//Then for each frame of that robot
for ( int j = 0; j < currentBot.getChildCount(); j++) {
XMLElement currentFrame = currentBot.getChild(j);
//println(currentFrame);
Position pos = new Position(int(currentFrame.getChild(0).getContent()), int(currentFrame.getChild(1).getContent()), float(currentFrame.getChild(2).getContent()));
//Add the frame to its vector of positions
//println(pos);
current.addFrame(pos);
}

// you actually need robots in your vector to make it work!
robots.add(current);
}


frameRate(1);
}

void draw()
{
background(102);
// center on screen ...
translate( width/2, height/2 );
for (int i = 0; i < robots.size(); i++) {
((Robot)robots.get(i)).drawNext();
}
}

//Class to aid storing of positions in a vector
class Position {
int x;
int y;
float rot;

Position(int x, int y, float rot){
this.x = x;
this.y = y;
this.rot = rot;
}

String toString() {
return "x: "+x+", y: "+y+", rot: "+rot;
}



}

class Robot {
Vector frames = new Vector();
PImage sprite;
int frame = 0;

Robot(PImage sprite) {
this.sprite = sprite;
}

//Add a frame from the XML to the vector
void addFrame(Position pos){
frames.add(pos);
}

void drawNext() {
pushMatrix();
// remove() will just do that, remove one frame.
// you will end up with an empty vector which raises an exception when you try to remove another object.
// i'm using a counter which i update every step here.
Position pos = (Position)frames.get(frame++);
// the %= means: if the value to the left is equal or bigger than the one to the right
// it will be set to zero. otherwise it remains untouched.
frame %= frames.size();
pos.toString();
rotate(pos.rot);
image(sprite, pos.x, pos.y);
popMatrix();
}
}


many small things ... my tip to you is in such cases start by testing (println) the most basic things that the others build on. for example: it makes no sense to test if the draw() method is actually called when there are no robots in the vector to be drawn ... Smiley

F
Re: Reading in XML and using it as frame informati
Reply #8 - Mar 13th, 2007, 8:19pm
 
oh amazing, thankyou so much!
Page Index Toggle Pages: 1