From 577e3ce2d0c3cf2df27615228b6041a2b4285dec Mon Sep 17 00:00:00 2001 From: shiffman Date: Mon, 17 Dec 2012 16:11:59 +0000 Subject: [PATCH] new Table example --- .../Advanced Data/LoadSaveTable/Bubble.pde | 40 ++++++++++ .../LoadSaveTable/LoadSaveTable.pde | 78 +++++++++++++++++++ .../Advanced Data/LoadSaveTable/data/data.csv | 5 ++ .../LoadingXMLObjects/Bubble.pde | 6 -- 4 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 java/examples/Topics/Advanced Data/LoadSaveTable/Bubble.pde create mode 100644 java/examples/Topics/Advanced Data/LoadSaveTable/LoadSaveTable.pde create mode 100644 java/examples/Topics/Advanced Data/LoadSaveTable/data/data.csv diff --git a/java/examples/Topics/Advanced Data/LoadSaveTable/Bubble.pde b/java/examples/Topics/Advanced Data/LoadSaveTable/Bubble.pde new file mode 100644 index 000000000..3a7b9b7f9 --- /dev/null +++ b/java/examples/Topics/Advanced Data/LoadSaveTable/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/LoadSaveTable/LoadSaveTable.pde b/java/examples/Topics/Advanced Data/LoadSaveTable/LoadSaveTable.pde new file mode 100644 index 000000000..2e3f2a20e --- /dev/null +++ b/java/examples/Topics/Advanced Data/LoadSaveTable/LoadSaveTable.pde @@ -0,0 +1,78 @@ +/** + * Loading Tabular Data + * by Daniel Shiffman. + * + * This example demonstrates how to use loadTable() + * to retrieve data from a CSV file and make objects + * from that data. + * + * Here is what the CSV looks like: + * + x,y,diameter,name + 160,103,43.19838,Happy + 372,137,52.42526,Sad + 273,235,61.14072,Joyous + 121,179,44.758068,Melancholy + */ + +// An Array of Bubble objects +Bubble[] bubbles; +// A Table object +Table table; + +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 CSV file into a Table object + // "header" option indicates the file has a header row + table = loadTable("data.csv","header"); + + // The size of the array of Bubble objects is determined by the total number of rows in the CSV + bubbles = new Bubble[table.getRowCount()]; + + // You can access iterate over all the rows in a table + int rowCount = 0; + for (TableRow row : table.rows()) { + // You can access the fields via their column name (or index) + float x = row.getFloat("x"); + float y = row.getFloat("y"); + float d = row.getFloat("diameter"); + String n = row.getString("name"); + // Make a Bubble object out of the data read + bubbles[rowCount] = new Bubble(x, y, d, n); + rowCount++; + } + +} + +void mousePressed() { + // Create a new row + TableRow row = table.addRow(); + // Set the values of that row + row.setFloat("x", mouseX); + row.setFloat("y", mouseY); + row.setFloat("diameter", random(40, 80)); + row.setString("name", "Blah"); + + // Writing the CSV back to the same file + saveTable(table,"data/data.csv"); + // And reloading it + loadData(); +} + diff --git a/java/examples/Topics/Advanced Data/LoadSaveTable/data/data.csv b/java/examples/Topics/Advanced Data/LoadSaveTable/data/data.csv new file mode 100644 index 000000000..88ac4c1b7 --- /dev/null +++ b/java/examples/Topics/Advanced Data/LoadSaveTable/data/data.csv @@ -0,0 +1,5 @@ +x,y,diameter,name +160,103,43.19838,Happy +372,137,52.42526,Sad +273,235,61.14072,Joyous +121,179,44.758068,Melancholy \ No newline at end of file diff --git a/java/examples/Topics/Advanced Data/LoadingXMLObjects/Bubble.pde b/java/examples/Topics/Advanced Data/LoadingXMLObjects/Bubble.pde index 40517b5dd..e99f2a93d 100644 --- a/java/examples/Topics/Advanced Data/LoadingXMLObjects/Bubble.pde +++ b/java/examples/Topics/Advanced Data/LoadingXMLObjects/Bubble.pde @@ -1,9 +1,3 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 18-9: Using Processing's XML library - // A Bubble class class Bubble {