This commit is contained in:
benfry
2011-01-26 19:22:19 +00:00
parent d3a18c7964
commit eb64b2d4fc
1234 changed files with 96518 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/**
* 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 balls;
int ballWidth = 48;
void setup() {
size(200, 200);
smooth();
noStroke();
// Create an empty ArrayList
balls = new ArrayList();
// 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 = (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));
}

View File

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

View File

@@ -0,0 +1,122 @@
import processing.core.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class ArrayListClass extends PApplet {
/**
* 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 balls;
int ballWidth = 48;
public void setup() {
size(200, 200);
smooth();
noStroke();
// Create an empty ArrayList
balls = new ArrayList();
// Start by adding one element
balls.add(new Ball(width/2, 0, ballWidth));
}
public 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 = (Ball) balls.get(i);
ball.move();
ball.display();
if (ball.finished()) {
// Items can be deleted with remove()
balls.remove(i);
}
}
}
public void mousePressed() {
// A new ball object is added to the ArrayList (by default to the end)
balls.add(new Ball(mouseX, mouseY, ballWidth));
}
// 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.1f;
}
public 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.8f;
y = height;
}
}
public boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
} else {
return false;
}
}
public void display() {
// Display the circle
fill(0,life);
//stroke(0,life);
ellipse(x,y,w,w);
}
}
static public void main(String args[]) {
PApplet.main(new String[] { "ArrayListClass" });
}
}

View File

@@ -0,0 +1,52 @@
/**
* 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 balls;
int ballWidth = 48;
void setup() {
size(200, 200);
smooth();
noStroke();
// Create an empty ArrayList
balls = new ArrayList();
// 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 = (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));
}

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@@ -0,0 +1,105 @@
/**
* Listing files in directories and subdirectories
* by Daniel Shiffman.
*
* This example has three functions:<br />
* 1) List the names of files in a directory<br />
* 2) List the names along with metadata (size, lastModified)<br />
* of files in a directory<br />
* 3) List the names along with metadata (size, lastModified)<br />
* of files in a directory and all subdirectories (using recursion)
*/
void setup() {
// Path
String path = sketchPath;
println("Listing all filenames in a directory: ");
String[] filenames = listFileNames(path);
println(filenames);
println("\nListing info about all files in a directory: ");
File[] files = listFiles(path);
for (int i = 0; i < files.length; i++) {
File f = files[i];
println("Name: " + f.getName());
println("Is directory: " + f.isDirectory());
println("Size: " + f.length());
String lastModified = new Date(f.lastModified()).toString();
println("Last Modified: " + lastModified);
println("-----------------------");
}
println("\nListing info about all files in a directory and all subdirectories: ");
ArrayList allFiles = listFilesRecursive(path);
for (int i = 0; i < allFiles.size(); i++) {
File f = (File) allFiles.get(i);
println("Name: " + f.getName());
println("Full path: " + f.getAbsolutePath());
println("Is directory: " + f.isDirectory());
println("Size: " + f.length());
String lastModified = new Date(f.lastModified()).toString();
println("Last Modified: " + lastModified);
println("-----------------------");
}
noLoop();
}
// Nothing is drawn in this program and the draw() doesn't loop because
// of the noLoop() in setup()
void draw() {
}
// This function returns all the files in a directory as an array of Strings
String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
}
// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
File[] files = file.listFiles();
return files;
} else {
// If it's not a directory
return null;
}
}
// Function to get a list ofall files in a directory and all subdirectories
ArrayList listFilesRecursive(String dir) {
ArrayList fileList = new ArrayList();
recurseDir(fileList,dir);
return fileList;
}
// Recursive function to traverse subdirectories
void recurseDir(ArrayList a, String dir) {
File file = new File(dir);
if (file.isDirectory()) {
// If you want to include directories in the list
a.add(file);
File[] subfiles = file.listFiles();
for (int i = 0; i < subfiles.length; i++) {
// Call this function on all files in this directory
recurseDir(a,subfiles[i].getAbsolutePath());
}
} else {
a.add(file);
}
}

View File

@@ -0,0 +1,89 @@
/**
* 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.
*
* This example uses the HashMap to perform a simple concordance
* http://en.wikipedia.org/wiki/Concordance_(publishing)
*/
HashMap words; // HashMap object
String[] tokens; // Array of all words from input file
int counter;
PFont f;
void setup() {
size(640, 360);
words = new HashMap();
// Load file and chop it up
String[] lines = loadStrings("dracula.txt");
String allText = join(lines, " ");
tokens = splitTokens(allText, " ,.?!:;[]-");
f = createFont("Georgia", 36, true);
}
void draw() {
background(255);
fill(0);
// Look at words one at a time
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
// 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 = (Word) words.get(s);
w.count();
} 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);
}
// Make an iterator to look at all the things in the HashMap
Iterator i = words.values().iterator();
// x and y will be used to locate each word
float x = 0;
float y = height-10;
while (i.hasNext()) {
// Look at each word
Word w = (Word) i.next();
// Only display words that appear 3 times
if (w.count > 3) {
// The size is the count
int fsize = constrain(w.count, 0, 100);
textFont(f, fsize);
text(w.word, x, y);
// Move along the x-axis
x += textWidth(w.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 < 0) {
break;
}
}
}
}

View File

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

View File

@@ -0,0 +1,126 @@
import processing.core.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class HashMapClass extends PApplet {
/**
* 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.
*
* This example uses the HashMap to perform a simple concordance
* http://en.wikipedia.org/wiki/Concordance_(publishing)
*/
// HashMap object
HashMap words;
// Array of all words from input file
String[] tokens;
int counter;
PFont f;
public void setup() {
size(640, 360);
words = new HashMap();
// Load file and chop it up
String[] lines = loadStrings("dracula.txt");
String allText = join(lines," ");
tokens = splitTokens(allText," ,.?!:;[]-");
f = createFont("Georgia",36,true);
}
public void draw() {
background(255);
fill(0);
// Look at words one at a time
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
// 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 = (Word) words.get(s);
w.count();
} 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);
}
// Make an iterator to look at all the things in the HashMap
Iterator i = words.values().iterator();
// x and y will be used to locate each word
float x = 0;
float y = height-10;
while (i.hasNext()) {
// Look at each word
Word w = (Word) i.next();
// Only display words that appear 3 times
if (w.count > 3) {
// The size is the count
int fsize = constrain(w.count,0,100);
textFont(f,fsize);
text(w.word,x,y);
// Move along the x-axis
x+=textWidth(w.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 < 0) {
break;
}
}
}
}
class Word {
int count;
String word;
Word(String s) {
word = s;
count = 1;
}
public void count() {
count++;
}
}
static public void main(String args[]) {
PApplet.main(new String[] { "HashMapClass" });
}
}

View File

@@ -0,0 +1,91 @@
/**
* 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.
*
* This example uses the HashMap to perform a simple concordance
* http://en.wikipedia.org/wiki/Concordance_(publishing)
*/
// HashMap object
HashMap words;
// Array of all words from input file
String[] tokens;
int counter;
PFont f;
void setup() {
size(640, 360);
words = new HashMap();
// Load file and chop it up
String[] lines = loadStrings("dracula.txt");
String allText = join(lines," ");
tokens = splitTokens(allText," ,.?!:;[]-");
f = createFont("Georgia",36,true);
}
void draw() {
background(255);
fill(0);
// Look at words one at a time
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
// 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 = (Word) words.get(s);
w.count();
} 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);
}
// Make an iterator to look at all the things in the HashMap
Iterator i = words.values().iterator();
// x and y will be used to locate each word
float x = 0;
float y = height-10;
while (i.hasNext()) {
// Look at each word
Word w = (Word) i.next();
// Only display words that appear 3 times
if (w.count > 3) {
// The size is the count
int fsize = constrain(w.count,0,100);
textFont(f,fsize);
text(w.word,x,y);
// Move along the x-axis
x+=textWidth(w.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 < 0) {
break;
}
}
}
}

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff