keyPressed & Data Rep 1st Steps
in
Programming Questions
•
2 years ago
Currently I am working on dividing the data coming in per gender of the entries. The code works with out the key pressed but only manages to place all feelings on the screen irrespective of gender. Here is the
code. I would like to tackle first the issue I am having with the If keyPressed function. When I run it I get a blank screen. I also get the println reading as 500 elements which it should but within seconds that turns to a another println with a value of 0. is the placement of the keyPressed function wrong? I have tried placing it above the for loop as well as above the xml call out with proper enclosure braces but that has resulted in other errors. Thanks in advance.
import processing.opengl.*;
String url = "http://api.wefeelfine.org:8080/ShowFeelings?returnfields=feeling,sentence,gender&display=xml";
ArrayList<FeelingEntry> feelingList = new ArrayList(); // an array list is built in. java is a strictly written language
// array list can only hold one type of object
PFont font;
void setup() {
size(600, 600, OPENGL);
background(0);
smooth();
loadFeelings();
}
void draw() {
background(0);
for (FeelingEntry fe:feelingList) {
fe.update();
fe.render();
}
}
void loadFeelings() {
//make an xml element
XMLElement xml = new XMLElement(this, url); // pass it an element
println(xml.getChildCount()); //asking it how many children it has
for (int i = 0; i < xml.getChildCount(); i++) {
XMLElement child = xml.getChild(i);
FeelingEntry fe = new FeelingEntry(); // define/call/created a new thing and now we are invoking it
fe.feeling = child.getString("feeling");
fe.sentence = child.getString("sentence");
fe.gender = child.getString("gender");
fe.x = width/2;
fe.y = height/2;
fe.tx = random(50, width - 50);
fe.ty = random(50, height - 50);
if (keyPressed) {
if (key =='g' || key == 'B') {
feelingList.add(fe);
}
}
else {
font = loadFont("Helvetica-48.vlw");
textFont(font);
fill(255, 0, 0);
text("WRONG KEY", width/2, height/2);
}
}
println(feelingList.size());
}
1