FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

trying a JSON example, but not getting very far yet · jasperboyd/processing@5d984b1 · GitHub

Repository navigation

Commit 5d984b1

Browse files
committed
trying a JSON example, but not getting very far yet
1 parent b322d28 commit 5d984b1

3 files changed

Lines changed: 210 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// A Bubble class
2+
3+
class Bubble {
4+
float x,y;
5+
float diameter;
6+
String name;
7+
8+
boolean over = false;
9+
10+
// Create the Bubble
11+
Bubble(float x_, float y_, float diameter_, String s) {
12+
x = x_;
13+
y = y_;
14+
diameter = diameter_;
15+
name = s;
16+
}
17+
18+
// CHecking if mouse is over the Bubble
19+
void rollover(float px, float py) {
20+
float d = dist(px,py,x,y);
21+
if (d < diameter/2) {
22+
over = true;
23+
} else {
24+
over = false;
25+
}
26+
}
27+
28+
// Display the Bubble
29+
void display() {
30+
stroke(0);
31+
strokeWeight(2);
32+
noFill();
33+
ellipse(x,y,diameter,diameter);
34+
if (over) {
35+
fill(0);
36+
textAlign(CENTER);
37+
text(name,x,y+diameter/2+20);
38+
}
39+
}
40+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Loading XML Data
3+
* by Daniel Shiffman.
4+
*
5+
* This example demonstrates how to use loadJSON()
6+
* to retrieve data from a JSON file and make objects
7+
* from that data.
8+
*
9+
* Here is what the JSON looks like (partial):
10+
*
11+
{
12+
"bubbles": {
13+
"bubble": [
14+
{
15+
"position": {
16+
"x": 160,
17+
"y": 103
18+
},
19+
"diameter": 43.19838,
20+
"label": "Happy"
21+
},
22+
{
23+
"position": {
24+
"x": 121,
25+
"y": 179
26+
},
27+
"diameter": 44.758068,
28+
"label": "Melancholy"
29+
}
30+
]
31+
}
32+
}
33+
*/
34+
35+
// An Array of Bubble objects
36+
Bubble[] bubbles;
37+
// A Table object
38+
JSONArray json;
39+
40+
void setup() {
41+
size(640, 360);
42+
loadData();
43+
}
44+
45+
void draw() {
46+
background(255);
47+
// Display all bubbles
48+
// for (Bubble b : bubbles) {
49+
// b.display();
50+
// b.rollover(mouseX, mouseY);
51+
// }
52+
//
53+
// textAlign(LEFT);
54+
// fill(0);
55+
// text("Click to add bubbles.", 10, height-10);
56+
}
57+
58+
void loadData() {
59+
// Load JSON file
60+
String jsonString = join(loadStrings("data.json"),"\n");
61+
//println(jsonString);
62+
63+
json = JSONArray.parse(jsonString);
64+
println(json);
65+
66+
// Get all the child nodes named "bubble"
67+
// XML[] children = xml.getChildren("bubble");
68+
//
69+
// // The size of the array of Bubble objects is determined by the total XML elements named "bubble"
70+
// bubbles = new Bubble[children.length];
71+
//
72+
// for (int i = 0; i < bubbles.length; i++) {
73+
//
74+
// // The position element has two attributes: x and y
75+
// XML positionElement = children[i].getChild("position");
76+
// // Note how with attributes we can get an integer or float directly
77+
// float x = positionElement.getInt("x");
78+
// float y = positionElement.getInt("y");
79+
//
80+
// // The diameter is the content of the child named "diamater"
81+
// XML diameterElement = children[i].getChild("diameter");
82+
// // Note how with the content of an XML node, we retrieve as a String and then convert
83+
// float diameter = float(diameterElement.getContent());
84+
//
85+
// // The label is the content of the child named "label"
86+
// XML labelElement = children[i].getChild("label");
87+
// String label = labelElement.getContent();
88+
//
89+
// // Make a Bubble object out of the data read
90+
// bubbles[i] = new Bubble(x, y, diameter, label);
91+
// }
92+
93+
}
94+
95+
// Still need to work on adding and deleting
96+
97+
void mousePressed() {
98+
99+
// Create a new XML bubble element
100+
// XML bubble = xml.addChild("bubble");
101+
//
102+
// // Set the poisition element
103+
// XML position = bubble.addChild("position");
104+
// // Here we can set attributes as integers directly
105+
// position.setInt("x",mouseX);
106+
// position.setInt("y",mouseY);
107+
//
108+
// // Set the diameter element
109+
// XML diameter = bubble.addChild("diameter");
110+
// // Here for a node's content, we have to convert to a String
111+
// diameter.setContent("" + random(40,80));
112+
//
113+
// // Set a label
114+
// XML label = bubble.addChild("label");
115+
// label.setContent("New label");
116+
//
117+
//
118+
// // Here we are removing the oldest bubble if there are more than 10
119+
// XML[] children = xml.getChildren("bubble");
120+
// // If the XML file has more than 10 bubble elements
121+
// if (children.length > 10) {
122+
// // Delete the first one
123+
// xml.removeChild(children[0]);
124+
// }
125+
//
126+
// // Save a new XML file
127+
// saveXML(xml,"data/data.xml");
128+
//
129+
// // reload the new data
130+
// loadData();
131+
}
132+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"bubbles": {
3+
"bubble": [
4+
{
5+
"position": {
6+
"x": 160,
7+
"y": 103
8+
},
9+
"diameter": 43.19838,
10+
"label": "Happy"
11+
},
12+
{
13+
"position": {
14+
"x": 372,
15+
"y": 137
16+
},
17+
"diameter": 52.42526,
18+
"label": "Sad"
19+
},
20+
{
21+
"position": {
22+
"x": 273,
23+
"y": 235
24+
},
25+
"diameter": 61.14072,
26+
"label": "Joyous"
27+
},
28+
{
29+
"position": {
30+
"x": 121,
31+
"y": 179
32+
},
33+
"diameter": 44.758068,
34+
"label": "Melancholy"
35+
}
36+
]
37+
}
38+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL