From fa30e6b83032441614f785db03cc4a9651153628 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Fri, 5 Apr 2013 15:56:34 -0400 Subject: [PATCH 1/5] working on JSON example using java JSON objects in the interim --- .../Advanced Data/LoadSaveJSONTest/Bubble.pde | 40 ++++++ .../LoadSaveJSONTest/LoadSaveJSONTest.pde | 126 ++++++++++++++++++ .../LoadSaveJSONTest/data/data.json | 36 +++++ 3 files changed, 202 insertions(+) create mode 100644 java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde create mode 100644 java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde create mode 100644 java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde new file mode 100644 index 000000000..3a7b9b7f9 --- /dev/null +++ b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde @@ -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); + } + } +} diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde new file mode 100644 index 000000000..df55a3db9 --- /dev/null +++ b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde @@ -0,0 +1,126 @@ +/** + * 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": [ + { + "position": { + "x": 160, + "y": 103 + }, + "diameter": 43.19838, + "label": "Happy" + }, + { + "position": { + "x": 372, + "y": 137 + }, + "diameter": 52.42526, + "label": "Sad" + } + ] +} + */ + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +// An Array of Bubble objects +Bubble[] bubbles; +// A Table object +JSONObject 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); + + try { + json = new JSONObject(jsonString); + JSONArray bubbleData = json.getJSONArray("bubbles"); + + // The size of the array of Bubble objects is determined by the total XML elements named "bubble" + bubbles = new Bubble[bubbleData.length()]; + + for (int i = 0; i < bubbleData.length(); i++) { + JSONObject bubble = bubbleData.getJSONObject(i); + JSONObject position = bubble.getJSONObject("position"); + int x = position.getInt("x"); + int y = position.getInt("y"); + + float diameter = (float)bubble.getDouble("diameter"); + String label = bubble.getString("label"); + + bubbles[i] = new Bubble(x, y, diameter, label); + } + } + catch (JSONException e) { + e.printStackTrace(); + } +} + +// 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(); +} + diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json new file mode 100644 index 000000000..bcb0079c3 --- /dev/null +++ b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json @@ -0,0 +1,36 @@ +{ + "bubbles": [ + { + "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" + } + ] +} \ No newline at end of file From 3a663e3c08532a88d4f20ff049de0cd4ace37464 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Sun, 7 Apr 2013 22:03:28 -0400 Subject: [PATCH 2/5] playing with JSON example a bit --- .../LoadSaveJSON/LoadSaveJSON.pde | 180 ++++++++---------- .../Advanced Data/LoadSaveJSON/data/data.json | 4 +- .../LoadSaveJSONTest/LoadSaveJSONTest.pde | 2 +- 3 files changed, 86 insertions(+), 100 deletions(-) diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde b/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde index 06f6e20d2..9c8700608 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde +++ b/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde @@ -8,34 +8,32 @@ * * 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" - } - ] - } -} + { + "bubbles": [ + { + "position": { + "x": 160, + "y": 103 + }, + "diameter": 43.19838, + "label": "Happy" + }, + { + "position": { + "x": 372, + "y": 137 + }, + "diameter": 52.42526, + "label": "Sad" + } + ] + } */ - + // An Array of Bubble objects Bubble[] bubbles; -// A Table object -JSONArray json; +// A JSON object +JSONObject json; void setup() { size(640, 360); @@ -45,88 +43,78 @@ void setup() { 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); + 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"); + 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); -// } + json = JSONObject.parse(jsonString); + println(json); + + JSONArray bubbleData = json.getJSONArray("bubbles"); + + // The size of the array of Bubble objects is determined by the total XML elements named "bubble" + bubbles = new Bubble[bubbleData.size()]; + + for (int i = 0; i < bubbleData.size(); i++) { + /*JSONObject bubble = bubbleData.getJSONObject(i); + JSONObject position = bubble.getJSONObject("position"); + int x = position.getInt("x"); + int y = position.getInt("y"); + + float diameter = (float)bubble.getDouble("diameter"); + String label = bubble.getString("label"); + + 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(); + // 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(); } diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json b/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json index 6e0449866..bcb0079c3 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json +++ b/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json @@ -1,6 +1,5 @@ { - "bubbles": { - "bubble": [ + "bubbles": [ { "position": { "x": 160, @@ -34,5 +33,4 @@ "label": "Melancholy" } ] - } } \ No newline at end of file diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde index df55a3db9..46b4a1e7f 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde +++ b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde @@ -36,7 +36,7 @@ import org.json.JSONObject; // An Array of Bubble objects Bubble[] bubbles; -// A Table object +// A JSON object JSONObject json; void setup() { From 389163205185fa5d57029a2cd696fb8550c60c1d Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 10 Apr 2013 15:00:00 -0400 Subject: [PATCH 3/5] removed back reference to PImage from ImageCache (issue #1391) --- core/src/processing/core/PGraphicsJava2D.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/src/processing/core/PGraphicsJava2D.java b/core/src/processing/core/PGraphicsJava2D.java index 9d7f18c57..ec5ddc410 100644 --- a/core/src/processing/core/PGraphicsJava2D.java +++ b/core/src/processing/core/PGraphicsJava2D.java @@ -1258,7 +1258,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ { } if (who.modified) { - cash.update(tint, tintColor); + cash.update(who, tint, tintColor); who.modified = false; } @@ -1291,14 +1291,13 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ { class ImageCache { - PImage source; boolean tinted; int tintedColor; int tintedPixels[]; // one row of tinted pixels BufferedImage image; public ImageCache(PImage source) { - this.source = source; +// this.source = source; // even if RGB, set the image type to ARGB, because the // image may have an alpha value for its tint(). // int type = BufferedImage.TYPE_INT_ARGB; @@ -1311,7 +1310,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ { * has changed, or the pixels have changed, so should just go through * with the update without further checks. */ - public void update(boolean tint, int tintColor) { + public void update(PImage source, boolean tint, int tintColor) { int bufferType = BufferedImage.TYPE_INT_ARGB; boolean opaque = (tintColor & 0xFF000000) == 0xFF000000; if (source.format == RGB) { From 07a7ad30a1d8eeac7aa082ba202dfd8ce742ff4b Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 10 Apr 2013 15:59:55 -0400 Subject: [PATCH 4/5] Fixes issue #1667 --- core/src/processing/core/PShape.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 5865a026f..026d3214c 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -1266,12 +1266,10 @@ public class PShape implements PConstants { for (int i = 0; i < src.vertexCount; i++) { float[] vert = src.vertices[i]; - - - dest.fill((int)(vert[PGraphics.R] * 255) << 24 | - (int)(vert[PGraphics.G] * 255) << 16 | - (int)(vert[PGraphics.B] * 255) << 8 | - (int)(vert[PGraphics.A] * 255)); + dest.fill((int)(vert[PGraphics.A] * 255) << 24 | + (int)(vert[PGraphics.R] * 255) << 16 | + (int)(vert[PGraphics.G] * 255) << 8 | + (int)(vert[PGraphics.B] * 255)); // Do we need to copy these as well? // dest.ambient(vert[PGraphics.AR] * 255, vert[PGraphics.AG] * 255, vert[PGraphics.AB] * 255); From 902fbeaae8e4b400e7a33f9d8fcafd9f9b9badc8 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 10 Apr 2013 18:37:27 -0400 Subject: [PATCH 5/5] DISABLE_OPTIMIZED_STROKE hint only works in 3D --- core/src/processing/opengl/PGraphicsOpenGL.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 1d56284f5..30026f09b 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -1939,8 +1939,12 @@ public class PGraphicsOpenGL extends PGraphics { flush(); setFlushMode(FLUSH_WHEN_FULL); } else if (which == DISABLE_OPTIMIZED_STROKE) { - flush(); - setFlushMode(FLUSH_CONTINUOUSLY); + if (is2D()) { + PGraphics.showWarning("Optimized strokes can only be disabled in 3D"); + } else { + flush(); + setFlushMode(FLUSH_CONTINUOUSLY); + } } else if (which == DISABLE_STROKE_PERSPECTIVE) { if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) { // We flush the geometry using the previous line setting.