Merge branch 'master' of github.com:processing/processing

This commit is contained in:
Florian Jenett
2013-03-11 21:09:29 +01:00
16 changed files with 23882 additions and 93 deletions

View File

@@ -0,0 +1,75 @@
/**
* CountingString example
* by Daniel Shiffman.
*
* This example demonstrates how to use a IntHash to store
* a number associated with a String. Java HashMaps can also
* be used for this, however, this example uses the IntHash
* class offered by Processing's data package for simplicity
* and added functionality.
*/
// The next line is needed if running in JavaScript Mode with Processing.js
/* @pjs font="Georgia.ttf"; */
IntHash concordance; // HashMap object
String[] tokens;
int counter = 0;
void setup() {
size(640, 360);
concordance = new IntHash();
// Load file and chop it up
String[] lines = loadStrings("dracula.txt");
String allText = join(lines, " ");
tokens = splitTokens(allText, " ,.?!:;[]-");
// Create the font
textFont(createFont("Georgia", 24));
}
void draw() {
background(51);
fill(255);
// Look at words one at a time
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
concordance.increment(s);
// x and y will be used to locate each word
float x = 0;
float y = 100;
concordance.sortValues();
String[] keys = concordance.keyArray();
// Look at each word
for (String word : keys) {
int count = concordance.get(word);
// Only display words that appear 3 times
if (count > 3) {
// The size is the count
int fsize = constrain(count, 0, 100);
textSize(fsize);
text(word, x, y);
// Move along the x-axis
x += textWidth(word + " ");
}
// If x gets to the end, move y
if (x > width) {
x = 0;
y += 100;
// If y gets to the end, we're done
if (y > height) {
break;
}
}
}
}

View File

@@ -0,0 +1,15 @@
class Word {
int count;
String word;
Word(String s) {
word = s;
count = 1;
}
void count() {
count++;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View 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);
}
}
}

View 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();
}

View File

@@ -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"
}
]
}
}

View File

@@ -69,6 +69,12 @@ void mousePressed() {
row.setFloat("y", mouseY);
row.setFloat("diameter", random(40, 80));
row.setString("name", "Blah");
// If the table has more than 10 rows
if (table.getRowCount() > 10) {
// Delete the oldest row
table.removeRow(0);
}
// Writing the CSV back to the same file
saveTable(table,"data/data.csv");

View 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);
}
}
}

View File

@@ -0,0 +1,118 @@
/**
* Loading XML Data
* by Daniel Shiffman.
*
* This example demonstrates how to use loadXML()
* to retrieve data from an XML file and make objects
* from that data.
*
* Here is what the XML looks like:
*
<?xml version="1.0"?>
<bubbles>
<bubble>
<position x="160" y="103"/>
<diameter>43.19838</diameter>
<label>Happy</label>
</bubble>
<bubble>
<position x="372" y="137"/>
<diameter>52.42526</diameter>
<label>Sad</label>
</bubble>
</bubbles>
*/
// An Array of Bubble objects
Bubble[] bubbles;
// A Table object
XML xml;
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 XML file
xml = loadXML("data.xml");
// 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();
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<bubbles>
<bubble>
<position x="160" y="103"/>
<diameter>43.19838</diameter>
<label>Happy</label>
</bubble>
<bubble>
<position x="372" y="137"/>
<diameter>52.42526</diameter>
<label>Sad</label>
</bubble>
<bubble>
<position x="273" y="235"/>
<diameter>61.14072</diameter>
<label>Joyous</label>
</bubble>
<bubble>
<position x="121" y="179"/>
<diameter>44.758068</diameter>
<label>Melancholy</label>
</bubble>
</bubbles>

View File

@@ -1,29 +0,0 @@
// A Bubble class
class Bubble {
float x,y;
float diameter;
color c;
Bubble(float r,float g, float b, float d) {
x = width/2;
y = height/2;
c = color(r, g, b, 204);
diameter = d;
}
// Display Bubble
void display() {
noStroke();
fill(c);
ellipse(x, y, diameter, diameter);
}
// Bubble drifts upwards
void drift() {
x += random(-1, 1);
y += random(-1, 1);
x = constrain(x, 0, width);
y = constrain(y, 0, height);
}
}

View File

@@ -1,62 +0,0 @@
/**
* Loading XML Data
* by Daniel Shiffman.
*
* This example demonstrates how to use loadXML()
* to retrieve data from an XML document and make
* objects from that data
*
* Here is what the XML looks like:
*
* <bubbles>
<bubble>
<diameter>40</diameter>
<color red="75" green="255" blue="0"/>
</bubble>
</bubbles>
*/
// An array of Bubble objects
Bubble[] bubbles;
void setup() {
size(640, 360);
// Load an XML document
XML xml = loadXML("bubbles.xml");
// Get all the child elements
XML[] children = xml.getChildren("bubble");
// Make an array of objects the same size
bubbles = new Bubble[children.length];
for (int i = 0; i < children.length; i++ ) {
// The diameter is the content of the child named "Diamater"
XML diameterElement = children[i].getChild("diameter");
int diameter = int(diameterElement.getContent());
// The color element has three attributes
XML colorElement = children[i].getChild("color");
// An int for r g and b
int r = colorElement.getInt("red");
int g = colorElement.getInt("green");
int b = colorElement.getInt("blue");
// Make a new Bubble object with values from XML document
bubbles[i] = new Bubble(r, g, b, diameter);
}
}
void draw() {
background(255);
// Display and move all bubbles
for (int i = 0; i < bubbles.length; i++ ) {
bubbles[i].display();
bubbles[i].drift();
}
}

View File

@@ -1 +0,0 @@
<?xml version="1.0"?>