Initial Commit
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* ArrayList of objects
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use a Java ArrayList to store
|
||||
* a variable number of objects. Items can be added and removed
|
||||
* from the ArrayList.
|
||||
*
|
||||
* Click the mouse to add bouncing balls.
|
||||
*/
|
||||
|
||||
ArrayList<Ball> balls;
|
||||
int ballWidth = 48;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
noStroke();
|
||||
|
||||
// Create an empty ArrayList (will store Ball objects)
|
||||
balls = new ArrayList<Ball>();
|
||||
|
||||
// Start by adding one element
|
||||
balls.add(new Ball(width/2, 0, ballWidth));
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
|
||||
// With an array, we say balls.length, with an ArrayList, we say balls.size()
|
||||
// The length of an ArrayList is dynamic
|
||||
// Notice how we are looping through the ArrayList backwards
|
||||
// This is because we are deleting elements from the list
|
||||
for (int i = balls.size()-1; i >= 0; i--) {
|
||||
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
|
||||
Ball ball = balls.get(i);
|
||||
ball.move();
|
||||
ball.display();
|
||||
if (ball.finished()) {
|
||||
// Items can be deleted with remove()
|
||||
balls.remove(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
// A new ball object is added to the ArrayList (by default to the end)
|
||||
balls.add(new Ball(mouseX, mouseY, ballWidth));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Simple bouncing ball class
|
||||
|
||||
class Ball {
|
||||
|
||||
float x;
|
||||
float y;
|
||||
float speed;
|
||||
float gravity;
|
||||
float w;
|
||||
float life = 255;
|
||||
|
||||
Ball(float tempX, float tempY, float tempW) {
|
||||
x = tempX;
|
||||
y = tempY;
|
||||
w = tempW;
|
||||
speed = 0;
|
||||
gravity = 0.1;
|
||||
}
|
||||
|
||||
void move() {
|
||||
// Add gravity to speed
|
||||
speed = speed + gravity;
|
||||
// Add speed to y location
|
||||
y = y + speed;
|
||||
// If square reaches the bottom
|
||||
// Reverse speed
|
||||
if (y > height) {
|
||||
// Dampening
|
||||
speed = speed * -0.8;
|
||||
y = height;
|
||||
}
|
||||
}
|
||||
|
||||
boolean finished() {
|
||||
// Balls fade out
|
||||
life--;
|
||||
if (life < 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
// Display the circle
|
||||
fill(0,life);
|
||||
//stroke(0,life);
|
||||
ellipse(x,y,w,w);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* CountingString example
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use a IntDict to store
|
||||
* a number associated with a String. Java HashMaps can also
|
||||
* be used for this, however, this example uses the IntDict
|
||||
* class offered by Processing's data package for simplicity
|
||||
* and added functionality.
|
||||
*
|
||||
* This example uses the IntDict to perform a simple concordance
|
||||
* http://en.wikipedia.org/wiki/Concordance_(publishing)
|
||||
*
|
||||
*/
|
||||
|
||||
// An IntDict pairs Strings with integers
|
||||
IntDict concordance;
|
||||
|
||||
// The raw array of words in
|
||||
String[] tokens;
|
||||
int counter = 0;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
|
||||
concordance = new IntDict();
|
||||
|
||||
// Load file and chop it up
|
||||
String[] lines = loadStrings("dracula.txt");
|
||||
String allText = join(lines, " ").toLowerCase();
|
||||
tokens = splitTokens(allText, " ,.?!:;[]-\"");
|
||||
|
||||
// Create the font
|
||||
textFont(createFont("SourceCodePro-Regular.ttf", 24));
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(51);
|
||||
fill(255);
|
||||
|
||||
// Look at words one at a time
|
||||
if (counter < tokens.length) {
|
||||
String s = tokens[counter];
|
||||
counter++;
|
||||
concordance.increment(s);
|
||||
}
|
||||
|
||||
// x and y will be used to locate each word
|
||||
float x = 0;
|
||||
float y = 48;
|
||||
|
||||
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, 48);
|
||||
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 += 48;
|
||||
// If y gets to the end, we're done
|
||||
if (y > height) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* HashMap example
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use a HashMap to store
|
||||
* a collection of objects referenced by a key. This is much like an array,
|
||||
* only instead of accessing elements with a numeric index, we use a String.
|
||||
* If you are familiar with associative arrays from other languages,
|
||||
* this is the same idea.
|
||||
*
|
||||
* A simpler example is CountingStrings which uses IntDict instead of
|
||||
* HashMap. The Processing classes IntDict, FloatDict, and StringDict
|
||||
* offer a simpler way of pairing Strings with numbers or other Strings.
|
||||
* Here we use a HashMap because we want to pair a String with a custom
|
||||
* object, in this case a "Word" object that stores two numbers.
|
||||
*
|
||||
* In this example, words that appear in one book (Dracula) only are colored white
|
||||
* while words the other (Frankenstein) are colored black.
|
||||
*/
|
||||
|
||||
HashMap<String, Word> words; // HashMap object
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
|
||||
// Create the HashMap
|
||||
words = new HashMap<String, Word>();
|
||||
|
||||
// Load two files
|
||||
loadFile("dracula.txt");
|
||||
loadFile("frankenstein.txt");
|
||||
|
||||
// Create the font
|
||||
textFont(createFont("SourceCodePro-Regular.ttf", 24));
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(126);
|
||||
|
||||
// Show words
|
||||
for (Word w : words.values()) {
|
||||
if (w.qualify()) {
|
||||
w.display();
|
||||
w.move();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load a file
|
||||
void loadFile(String filename) {
|
||||
String[] lines = loadStrings(filename);
|
||||
String allText = join(lines, " ").toLowerCase();
|
||||
String[] tokens = splitTokens(allText, " ,.?!:;[]-\"'");
|
||||
|
||||
for (String s : tokens) {
|
||||
// Is the word in the HashMap
|
||||
if (words.containsKey(s)) {
|
||||
// Get the word object and increase the count
|
||||
// We access objects from a HashMap via its key, the String
|
||||
Word w = words.get(s);
|
||||
// Which book am I loading?
|
||||
if (filename.contains("dracula")) {
|
||||
w.incrementDracula();
|
||||
}
|
||||
else if (filename.contains("frankenstein")) {
|
||||
w.incrementFranken();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Otherwise make a new word
|
||||
Word w = new Word(s);
|
||||
// And add to the HashMap put() takes two arguments, "key" and "value"
|
||||
// The key for us is the String and the value is the Word object
|
||||
words.put(s, w);
|
||||
if (filename.contains("dracula")) {
|
||||
w.incrementDracula();
|
||||
} else if (filename.contains("frankenstein")) {
|
||||
w.incrementFranken();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
class Word {
|
||||
|
||||
// Store a count for occurences in two different books
|
||||
int countDracula;
|
||||
int countFranken;
|
||||
// Also the total count
|
||||
int totalCount;
|
||||
|
||||
// What is the String
|
||||
String word;
|
||||
|
||||
// Where is it on the screen
|
||||
PVector position;
|
||||
|
||||
Word(String s) {
|
||||
position = new PVector(random(width), random(-height, height*2));
|
||||
word = s;
|
||||
}
|
||||
|
||||
// We will display a word if it appears at least 5 times
|
||||
// and only in one of the books
|
||||
boolean qualify() {
|
||||
if ((countDracula == totalCount || countFranken == totalCount) && totalCount > 5) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the count for Dracula
|
||||
void incrementDracula() {
|
||||
countDracula++;
|
||||
totalCount++;
|
||||
}
|
||||
|
||||
|
||||
// Increment the count for Frankenstein
|
||||
void incrementFranken() {
|
||||
countFranken++;
|
||||
totalCount++;
|
||||
}
|
||||
|
||||
// The more often it appears, the faster it falls
|
||||
void move() {
|
||||
float speed = map(totalCount, 5, 25, 0.1, 0.4);
|
||||
speed = constrain(speed,0,10);
|
||||
position.y += speed;
|
||||
|
||||
if (position.y > height*2) {
|
||||
position.y = -height;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Depending on which book it gets a color
|
||||
void display() {
|
||||
if (countDracula > 0) {
|
||||
fill(255);
|
||||
}
|
||||
else if (countFranken > 0) {
|
||||
fill(0);
|
||||
}
|
||||
// Its size is also tied to number of occurences
|
||||
float fs = map(totalCount,5,25,2,24);
|
||||
fs = constrain(fs,2,48);
|
||||
textSize(fs);
|
||||
textAlign(CENTER);
|
||||
text(word, position.x, position.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* IntList Lottery example
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates an IntList can be used to store a list of numbers.
|
||||
* While an array of integers serves a similar purpose it is of fixed size. The
|
||||
* An IntList can easily have values added or deleted and it can also be
|
||||
* shuffled and sorted. For lists of floats or Strings, you can use FloatList
|
||||
* and StringList. For lists of objects, use ArrayList.
|
||||
*
|
||||
* In this example, three lists of integers are created. One is a pool of numbers
|
||||
* that is shuffled and picked randomly from. One is the list of "picked" numbers.
|
||||
* And one is a lottery "ticket" which includes 5 numbers that are trying to be matched.
|
||||
*/
|
||||
|
||||
// Three lists of integers
|
||||
IntList lottery;
|
||||
IntList results;
|
||||
IntList ticket;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
frameRate(30);
|
||||
// Create empy lists
|
||||
lottery = new IntList();
|
||||
results = new IntList();
|
||||
ticket = new IntList();
|
||||
|
||||
|
||||
// Add 20 integers in order to the lottery list
|
||||
for (int i = 0; i < 20; i++) {
|
||||
lottery.append(i);
|
||||
}
|
||||
|
||||
// Pick five numbers from the lottery list to go into the Ticket list
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int index = int(random(lottery.size()));
|
||||
ticket.append(lottery.get(index));
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(51);
|
||||
|
||||
// The shuffle() method randomly shuffles the order of the values in the list
|
||||
lottery.shuffle();
|
||||
|
||||
// Call a method that will display the integers in the list at an x,y location
|
||||
showList(lottery, 16, 48);
|
||||
showList(results, 16, 100);
|
||||
showList(ticket, 16, 140);
|
||||
|
||||
|
||||
// This loop checks if the picked numbers (results)
|
||||
// match the ticket numbers
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
// Are the integers equal?
|
||||
if (results.get(i) == ticket.get(i)) {
|
||||
fill(0, 255, 0, 100); // if so green
|
||||
} else {
|
||||
fill(255, 0, 0, 100); // if not red
|
||||
}
|
||||
ellipse(16+i*32, 140, 24, 24);
|
||||
}
|
||||
|
||||
|
||||
// One every 30 frames we pick a new lottery number to go in results
|
||||
if (frameCount % 30 == 0) {
|
||||
if (results.size() < 5) {
|
||||
// Get the first value in the lottery list and remove it
|
||||
int val = lottery.remove(0);
|
||||
// Put it in the results
|
||||
results.append(val);
|
||||
} else {
|
||||
// Ok we picked five numbers, let's reset
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
// Put the picked results back into the lottery
|
||||
lottery.append(results.get(i));
|
||||
}
|
||||
// Clear the results and start over
|
||||
results.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw a list of numbers starting at an x,y location
|
||||
void showList(IntList list, float x, float y) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
// Use get() to pull a value from the list at the specified index
|
||||
int val = list.get(i);
|
||||
stroke(255);
|
||||
noFill();
|
||||
ellipse(x+i*32, y, 24, 24);
|
||||
textAlign(CENTER);
|
||||
fill(255);
|
||||
text(val, x+i*32, y+6);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Loading JSON 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
// An Array of Bubble objects
|
||||
Bubble[] bubbles;
|
||||
// A JSON 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
|
||||
// Temporary full path until path problem resolved.
|
||||
json = loadJSONObject("data.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++) {
|
||||
// Get each object in the array
|
||||
JSONObject bubble = bubbleData.getJSONObject(i);
|
||||
// Get a position object
|
||||
JSONObject position = bubble.getJSONObject("position");
|
||||
// Get x,y from position
|
||||
int x = position.getInt("x");
|
||||
int y = position.getInt("y");
|
||||
|
||||
// Get diamter and label
|
||||
float diameter = bubble.getFloat("diameter");
|
||||
String label = bubble.getString("label");
|
||||
|
||||
// Put object in array
|
||||
bubbles[i] = new Bubble(x, y, diameter, label);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
// Create a new JSON bubble object
|
||||
JSONObject newBubble = new JSONObject();
|
||||
|
||||
// Create a new JSON position object
|
||||
JSONObject position = new JSONObject();
|
||||
position.setInt("x", mouseX);
|
||||
position.setInt("y", mouseY);
|
||||
|
||||
// Add position to bubble
|
||||
newBubble.setJSONObject("position", position);
|
||||
|
||||
// Add diamater and label to bubble
|
||||
newBubble.setFloat("diameter", random(40, 80));
|
||||
newBubble.setString("label", "New label");
|
||||
|
||||
// Append the new JSON bubble object to the array
|
||||
JSONArray bubbleData = json.getJSONArray("bubbles");
|
||||
bubbleData.append(newBubble);
|
||||
|
||||
if (bubbleData.size() > 10) {
|
||||
bubbleData.remove(0);
|
||||
}
|
||||
|
||||
// Save new data
|
||||
saveJSONObject(json,"data/data.json");
|
||||
loadData();
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* 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");
|
||||
|
||||
// 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");
|
||||
// And reloading it
|
||||
loadData();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 via getInt() and getFloat()
|
||||
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 via getIntContent() and getFloatContent()
|
||||
float diameter = diameterElement.getFloatContent();
|
||||
|
||||
// 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.setFloatContent(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,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>
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Regular Expression example
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use matchAll() to create
|
||||
* a list of all matches of a given regex.
|
||||
*
|
||||
* Here we'll load the raw HTML from a URL and search for any
|
||||
* <a href=" "> links
|
||||
*/
|
||||
|
||||
// Our source url
|
||||
String url = "http://processing.org";
|
||||
// We'll store the results in an array
|
||||
String[] links;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
// Load the links
|
||||
links = loadLinks(url);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
// Display the raw links
|
||||
fill(255);
|
||||
for (int i = 0; i < links.length; i++) {
|
||||
text(links[i],10,16+i*16);
|
||||
}
|
||||
}
|
||||
|
||||
String[] loadLinks(String s) {
|
||||
// Load the raw HTML
|
||||
String[] lines = loadStrings(s);
|
||||
// Put it in one big string
|
||||
String html = join(lines,"\n");
|
||||
|
||||
// A wacky regex for matching a URL
|
||||
String regex = "<\\s*a\\s+href\\s*=\\s*\"(.*?)\"";
|
||||
// The matches are in a two dimensional array
|
||||
// The first dimension is all matches
|
||||
// The second dimension is the groups
|
||||
String[][] matches = matchAll(html, regex);
|
||||
|
||||
// An array for the results
|
||||
String[] results = new String[matches.length];
|
||||
|
||||
// We want group 1 for each result
|
||||
for (int i = 0; i < results.length; i++) {
|
||||
results[i] = matches[i][1];
|
||||
}
|
||||
|
||||
// Return the results
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Thread function example
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use thread() to spawn
|
||||
* a process that happens outside of the main animation thread.
|
||||
*
|
||||
* When thread() is called, the draw() loop will continue while
|
||||
* the code inside the function passed to thread() will operate
|
||||
* in the background.
|
||||
*
|
||||
* For more about threads, see: http://wiki.processing.org/w/Threading
|
||||
*/
|
||||
|
||||
// This sketch will load data from all of these URLs in a separate thread
|
||||
String[] urls = {
|
||||
"http://processing.org",
|
||||
"http://www.processing.org/exhibition/",
|
||||
"http://www.processing.org/reference/",
|
||||
"http://www.processing.org/reference/libraries",
|
||||
"http://www.processing.org/reference/tools",
|
||||
"http://www.processing.org/reference/environment",
|
||||
"http://www.processing.org/learning/",
|
||||
"http://www.processing.org/learning/basics/",
|
||||
"http://www.processing.org/learning/topics/",
|
||||
"http://www.processing.org/learning/gettingstarted/",
|
||||
"http://www.processing.org/download/",
|
||||
"http://www.processing.org/shop/",
|
||||
"http://www.processing.org/about/"
|
||||
};
|
||||
|
||||
// This will keep track of whether the thread is finished
|
||||
boolean finished = false;
|
||||
// And how far along
|
||||
float percent = 0;
|
||||
|
||||
// A variable to keep all the data loaded
|
||||
String allData;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
// Spawn the thread!
|
||||
thread("loadData");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
|
||||
// If we're not finished draw a "loading bar"
|
||||
// This is so that we can see the progress of the thread
|
||||
// This would not be necessary in a sketch where you wanted to load data in the background
|
||||
// and hide this from the user, allowing the draw() loop to simply continue
|
||||
if (!finished) {
|
||||
stroke(255);
|
||||
noFill();
|
||||
rect(width/2-150, height/2, 300, 10);
|
||||
fill(255);
|
||||
// The size of the rectangle is mapped to the percentage completed
|
||||
float w = map(percent, 0, 1, 0, 300);
|
||||
rect(width/2-150, height/2, w, 10);
|
||||
textSize(16);
|
||||
textAlign(CENTER);
|
||||
fill(255);
|
||||
text("Loading", width/2, height/2+30);
|
||||
}
|
||||
else {
|
||||
// The thread is complete!
|
||||
textAlign(CENTER);
|
||||
textSize(24);
|
||||
fill(255);
|
||||
text("Finished loading. Click the mouse to load again.", width/2, height/2);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
thread("loadData");
|
||||
}
|
||||
|
||||
void loadData() {
|
||||
// The thread is not completed
|
||||
finished = false;
|
||||
// Reset the data to empty
|
||||
allData = "";
|
||||
|
||||
// Look at each URL
|
||||
// This example is doing some highly arbitrary things just to make it take longer
|
||||
// If you had a lot of data parsing you needed to do, this can all happen in the background
|
||||
for (int i = 0; i < urls.length; i++) {
|
||||
String[] lines = loadStrings(urls[i]);
|
||||
// Demonstrating some arbitrary text splitting, joining, and sorting to make the thread take longer
|
||||
String allTxt = join(lines, " ");
|
||||
String[] words = splitTokens(allTxt, "\t+\n <>=\\-!@#$%^&*(),.;:/?\"\'");
|
||||
for (int j = 0; j < words.length; j++) {
|
||||
words[j] = words[j].trim();
|
||||
words[j] = words[j].toLowerCase();
|
||||
}
|
||||
words = sort(words);
|
||||
allData += join(words, " ");
|
||||
percent = float(i)/urls.length;
|
||||
}
|
||||
|
||||
String[] words = split(allData," ");
|
||||
words = sort(words);
|
||||
allData = join(words, " ");
|
||||
|
||||
// The thread is completed!
|
||||
finished = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Loading XML Data
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* This example demonstrates how to use loadXML()
|
||||
* to retrieve data from an XML document via a URL
|
||||
*/
|
||||
|
||||
// We're going to store the temperature
|
||||
int temperature = 0;
|
||||
// We're going to store text about the weather
|
||||
String weather = "";
|
||||
|
||||
|
||||
// Yahoo weather uses something called A WOEID (Where On Earth IDentifier)
|
||||
// https://en.wikipedia.org/wiki/WOEID
|
||||
// This is the WOEID for zip code 10003
|
||||
String zip = "10003";
|
||||
String woeid = "12761335";
|
||||
|
||||
PFont font;
|
||||
|
||||
void setup() {
|
||||
size(600, 360);
|
||||
|
||||
font = createFont("Merriweather-Light.ttf", 28);
|
||||
textFont(font);
|
||||
|
||||
// The URL for the XML document
|
||||
String url = "http://query.yahooapis.com/v1/public/yql?format=xml&q=select+*+from+weather.forecast+where+woeid=" + woeid + "+and+u='F'";
|
||||
|
||||
// Load the XML document
|
||||
XML xml = loadXML(url);
|
||||
|
||||
// Grab the element we want
|
||||
XML forecast = xml.getChild("results/channel/item/yweather:forecast");
|
||||
|
||||
// Get the attributes we want
|
||||
temperature = forecast.getInt("high");
|
||||
weather = forecast.getString("text");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
fill(0);
|
||||
|
||||
// Display all the stuff we want to display
|
||||
text("Zip: " + zip, width*0.15, height*0.33);
|
||||
text("Today’s high: " + temperature + "°F", width*0.15, height*0.5);
|
||||
text("Forecast: " + weather, width*0.15, height*0.66);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Animated Sprite (Shifty + Teddy)
|
||||
* by James Paterson.
|
||||
*
|
||||
* Press the mouse button to change animations.
|
||||
* Demonstrates loading, displaying, and animating GIF images.
|
||||
* It would be easy to write a program to display
|
||||
* animated GIFs, but would not allow as much control over
|
||||
* the display sequence and rate of display.
|
||||
*/
|
||||
|
||||
Animation animation1, animation2;
|
||||
|
||||
float xpos;
|
||||
float ypos;
|
||||
float drag = 30.0;
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
background(255, 204, 0);
|
||||
frameRate(24);
|
||||
animation1 = new Animation("PT_Shifty_", 38);
|
||||
animation2 = new Animation("PT_Teddy_", 60);
|
||||
ypos = height * 0.25;
|
||||
}
|
||||
|
||||
void draw() {
|
||||
float dx = mouseX - xpos;
|
||||
xpos = xpos + dx/drag;
|
||||
|
||||
// Display the sprite at the position xpos, ypos
|
||||
if (mousePressed) {
|
||||
background(153, 153, 0);
|
||||
animation1.display(xpos-animation1.getWidth()/2, ypos);
|
||||
} else {
|
||||
background(255, 204, 0);
|
||||
animation2.display(xpos-animation1.getWidth()/2, ypos);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Class for animating a sequence of GIFs
|
||||
|
||||
class Animation {
|
||||
PImage[] images;
|
||||
int imageCount;
|
||||
int frame;
|
||||
|
||||
Animation(String imagePrefix, int count) {
|
||||
imageCount = count;
|
||||
images = new PImage[imageCount];
|
||||
|
||||
for (int i = 0; i < imageCount; i++) {
|
||||
// Use nf() to number format 'i' into four digits
|
||||
String filename = imagePrefix + nf(i, 4) + ".gif";
|
||||
images[i] = loadImage(filename);
|
||||
}
|
||||
}
|
||||
|
||||
void display(float xpos, float ypos) {
|
||||
frame = (frame+1) % imageCount;
|
||||
image(images[frame], xpos, ypos);
|
||||
}
|
||||
|
||||
int getWidth() {
|
||||
return images[0].width;
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 314 B |
|
After Width: | Height: | Size: 314 B |
|
After Width: | Height: | Size: 318 B |
|
After Width: | Height: | Size: 328 B |
|
After Width: | Height: | Size: 343 B |
|
After Width: | Height: | Size: 379 B |
|
After Width: | Height: | Size: 420 B |
|
After Width: | Height: | Size: 501 B |
|
After Width: | Height: | Size: 615 B |
|
After Width: | Height: | Size: 733 B |
|
After Width: | Height: | Size: 806 B |
|
After Width: | Height: | Size: 842 B |
|
After Width: | Height: | Size: 877 B |
|
After Width: | Height: | Size: 877 B |
|
After Width: | Height: | Size: 951 B |
|
After Width: | Height: | Size: 934 B |
|
After Width: | Height: | Size: 917 B |
|
After Width: | Height: | Size: 907 B |
|
After Width: | Height: | Size: 907 B |
|
After Width: | Height: | Size: 892 B |
|
After Width: | Height: | Size: 854 B |
|
After Width: | Height: | Size: 841 B |
|
After Width: | Height: | Size: 886 B |
|
After Width: | Height: | Size: 805 B |
|
After Width: | Height: | Size: 733 B |
|
After Width: | Height: | Size: 667 B |
|
After Width: | Height: | Size: 596 B |
|
After Width: | Height: | Size: 539 B |
|
After Width: | Height: | Size: 503 B |
|
After Width: | Height: | Size: 451 B |
|
After Width: | Height: | Size: 392 B |
|
After Width: | Height: | Size: 351 B |
|
After Width: | Height: | Size: 333 B |
|
After Width: | Height: | Size: 323 B |
|
After Width: | Height: | Size: 316 B |
|
After Width: | Height: | Size: 316 B |
|
After Width: | Height: | Size: 315 B |
|
After Width: | Height: | Size: 312 B |
|
After Width: | Height: | Size: 305 B |
|
After Width: | Height: | Size: 309 B |
|
After Width: | Height: | Size: 310 B |
|
After Width: | Height: | Size: 310 B |
|
After Width: | Height: | Size: 315 B |
|
After Width: | Height: | Size: 329 B |
|
After Width: | Height: | Size: 339 B |
|
After Width: | Height: | Size: 354 B |
|
After Width: | Height: | Size: 390 B |
|
After Width: | Height: | Size: 415 B |
|
After Width: | Height: | Size: 451 B |
|
After Width: | Height: | Size: 482 B |
|
After Width: | Height: | Size: 546 B |
|
After Width: | Height: | Size: 566 B |
|
After Width: | Height: | Size: 607 B |
|
After Width: | Height: | Size: 692 B |
|
After Width: | Height: | Size: 778 B |
|
After Width: | Height: | Size: 853 B |
|
After Width: | Height: | Size: 914 B |
|
After Width: | Height: | Size: 936 B |
|
After Width: | Height: | Size: 968 B |
|
After Width: | Height: | Size: 971 B |
|
After Width: | Height: | Size: 987 B |
|
After Width: | Height: | Size: 1013 B |
|
After Width: | Height: | Size: 1014 B |
|
After Width: | Height: | Size: 1002 B |
|
After Width: | Height: | Size: 976 B |
|
After Width: | Height: | Size: 984 B |
|
After Width: | Height: | Size: 936 B |
|
After Width: | Height: | Size: 891 B |
|
After Width: | Height: | Size: 826 B |
|
After Width: | Height: | Size: 717 B |
|
After Width: | Height: | Size: 625 B |
|
After Width: | Height: | Size: 543 B |
|
After Width: | Height: | Size: 486 B |