mirror of
https://github.com/processing/processing4.git
synced 2026-02-02 13:21:07 +01:00
trying a JSON example, but not getting very far yet
This commit is contained in:
40
java/examples/Topics/Advanced Data/LoadSaveJSON/Bubble.pde
Normal file
40
java/examples/Topics/Advanced Data/LoadSaveJSON/Bubble.pde
Normal file
@@ -0,0 +1,40 @@
|
||||
// A Bubble class
|
||||
|
||||
class Bubble {
|
||||
float x,y;
|
||||
float diameter;
|
||||
String name;
|
||||
|
||||
boolean over = false;
|
||||
|
||||
// Create the Bubble
|
||||
Bubble(float x_, float y_, float diameter_, String s) {
|
||||
x = x_;
|
||||
y = y_;
|
||||
diameter = diameter_;
|
||||
name = s;
|
||||
}
|
||||
|
||||
// CHecking if mouse is over the Bubble
|
||||
void rollover(float px, float py) {
|
||||
float d = dist(px,py,x,y);
|
||||
if (d < diameter/2) {
|
||||
over = true;
|
||||
} else {
|
||||
over = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Display the Bubble
|
||||
void display() {
|
||||
stroke(0);
|
||||
strokeWeight(2);
|
||||
noFill();
|
||||
ellipse(x,y,diameter,diameter);
|
||||
if (over) {
|
||||
fill(0);
|
||||
textAlign(CENTER);
|
||||
text(name,x,y+diameter/2+20);
|
||||
}
|
||||
}
|
||||
}
|
||||
132
java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde
Normal file
132
java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* Loading XML Data
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use loadJSON()
|
||||
* to retrieve data from a JSON file and make objects
|
||||
* from that data.
|
||||
*
|
||||
* Here is what the JSON looks like (partial):
|
||||
*
|
||||
{
|
||||
"bubbles": {
|
||||
"bubble": [
|
||||
{
|
||||
"position": {
|
||||
"x": 160,
|
||||
"y": 103
|
||||
},
|
||||
"diameter": 43.19838,
|
||||
"label": "Happy"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"x": 121,
|
||||
"y": 179
|
||||
},
|
||||
"diameter": 44.758068,
|
||||
"label": "Melancholy"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// An Array of Bubble objects
|
||||
Bubble[] bubbles;
|
||||
// A Table object
|
||||
JSONArray json;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
loadData();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
// Display all bubbles
|
||||
// for (Bubble b : bubbles) {
|
||||
// b.display();
|
||||
// b.rollover(mouseX, mouseY);
|
||||
// }
|
||||
//
|
||||
// textAlign(LEFT);
|
||||
// fill(0);
|
||||
// text("Click to add bubbles.", 10, height-10);
|
||||
}
|
||||
|
||||
void loadData() {
|
||||
// Load JSON file
|
||||
String jsonString = join(loadStrings("data.json"),"\n");
|
||||
//println(jsonString);
|
||||
|
||||
json = JSONArray.parse(jsonString);
|
||||
println(json);
|
||||
|
||||
// Get all the child nodes named "bubble"
|
||||
// XML[] children = xml.getChildren("bubble");
|
||||
//
|
||||
// // The size of the array of Bubble objects is determined by the total XML elements named "bubble"
|
||||
// bubbles = new Bubble[children.length];
|
||||
//
|
||||
// for (int i = 0; i < bubbles.length; i++) {
|
||||
//
|
||||
// // The position element has two attributes: x and y
|
||||
// XML positionElement = children[i].getChild("position");
|
||||
// // Note how with attributes we can get an integer or float directly
|
||||
// float x = positionElement.getInt("x");
|
||||
// float y = positionElement.getInt("y");
|
||||
//
|
||||
// // The diameter is the content of the child named "diamater"
|
||||
// XML diameterElement = children[i].getChild("diameter");
|
||||
// // Note how with the content of an XML node, we retrieve as a String and then convert
|
||||
// float diameter = float(diameterElement.getContent());
|
||||
//
|
||||
// // The label is the content of the child named "label"
|
||||
// XML labelElement = children[i].getChild("label");
|
||||
// String label = labelElement.getContent();
|
||||
//
|
||||
// // Make a Bubble object out of the data read
|
||||
// bubbles[i] = new Bubble(x, y, diameter, label);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
// Still need to work on adding and deleting
|
||||
|
||||
void mousePressed() {
|
||||
|
||||
// Create a new XML bubble element
|
||||
// XML bubble = xml.addChild("bubble");
|
||||
//
|
||||
// // Set the poisition element
|
||||
// XML position = bubble.addChild("position");
|
||||
// // Here we can set attributes as integers directly
|
||||
// position.setInt("x",mouseX);
|
||||
// position.setInt("y",mouseY);
|
||||
//
|
||||
// // Set the diameter element
|
||||
// XML diameter = bubble.addChild("diameter");
|
||||
// // Here for a node's content, we have to convert to a String
|
||||
// diameter.setContent("" + random(40,80));
|
||||
//
|
||||
// // Set a label
|
||||
// XML label = bubble.addChild("label");
|
||||
// label.setContent("New label");
|
||||
//
|
||||
//
|
||||
// // Here we are removing the oldest bubble if there are more than 10
|
||||
// XML[] children = xml.getChildren("bubble");
|
||||
// // If the XML file has more than 10 bubble elements
|
||||
// if (children.length > 10) {
|
||||
// // Delete the first one
|
||||
// xml.removeChild(children[0]);
|
||||
// }
|
||||
//
|
||||
// // Save a new XML file
|
||||
// saveXML(xml,"data/data.xml");
|
||||
//
|
||||
// // reload the new data
|
||||
// loadData();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"bubbles": {
|
||||
"bubble": [
|
||||
{
|
||||
"position": {
|
||||
"x": 160,
|
||||
"y": 103
|
||||
},
|
||||
"diameter": 43.19838,
|
||||
"label": "Happy"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"x": 372,
|
||||
"y": 137
|
||||
},
|
||||
"diameter": 52.42526,
|
||||
"label": "Sad"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"x": 273,
|
||||
"y": 235
|
||||
},
|
||||
"diameter": 61.14072,
|
||||
"label": "Joyous"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"x": 121,
|
||||
"y": 179
|
||||
},
|
||||
"diameter": 44.758068,
|
||||
"label": "Melancholy"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user