Skip to content

Commit 11f0f14

Browse files
committed
Re-adding Topics to SVN
1 parent 6b387d3 commit 11f0f14

236 files changed

Lines changed: 31093 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* ArrayList of objects
3+
* by Daniel Shiffman.
4+
*
5+
* This example demonstrates how to use a Java ArrayList to store
6+
* a variable number of objects. Items can be added and removed
7+
* from the ArrayList.
8+
*
9+
* Click the mouse to add bouncing balls.
10+
*/
11+
12+
ArrayList balls;
13+
int ballWidth = 48;
14+
15+
void setup() {
16+
size(640, 360);
17+
smooth();
18+
noStroke();
19+
20+
// Create an empty ArrayList
21+
balls = new ArrayList();
22+
23+
// Start by adding one element
24+
balls.add(new Ball(width/2, 0, ballWidth));
25+
}
26+
27+
void draw() {
28+
background(255);
29+
30+
// With an array, we say balls.length, with an ArrayList, we say balls.size()
31+
// The length of an ArrayList is dynamic
32+
// Notice how we are looping through the ArrayList backwards
33+
// This is because we are deleting elements from the list
34+
for (int i = balls.size()-1; i >= 0; i--) {
35+
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
36+
Ball ball = (Ball) balls.get(i);
37+
ball.move();
38+
ball.display();
39+
if (ball.finished()) {
40+
// Items can be deleted with remove()
41+
balls.remove(i);
42+
}
43+
44+
}
45+
46+
}
47+
48+
void mousePressed() {
49+
// A new ball object is added to the ArrayList (by default to the end)
50+
balls.add(new Ball(mouseX, mouseY, ballWidth));
51+
}
52+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Simple bouncing ball class
2+
3+
class Ball {
4+
5+
float x;
6+
float y;
7+
float speed;
8+
float gravity;
9+
float w;
10+
float life = 255;
11+
12+
Ball(float tempX, float tempY, float tempW) {
13+
x = tempX;
14+
y = tempY;
15+
w = tempW;
16+
speed = 0;
17+
gravity = 0.1;
18+
}
19+
20+
void move() {
21+
// Add gravity to speed
22+
speed = speed + gravity;
23+
// Add speed to y location
24+
y = y + speed;
25+
// If square reaches the bottom
26+
// Reverse speed
27+
if (y > height) {
28+
// Dampening
29+
speed = speed * -0.8;
30+
y = height;
31+
}
32+
}
33+
34+
boolean finished() {
35+
// Balls fade out
36+
life--;
37+
if (life < 0) {
38+
return true;
39+
} else {
40+
return false;
41+
}
42+
}
43+
44+
void display() {
45+
// Display the circle
46+
fill(0,life);
47+
//stroke(0,life);
48+
ellipse(x,y,w,w);
49+
}
50+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Listing files in directories and subdirectories
3+
* by Daniel Shiffman.
4+
*
5+
* This example has three functions:<br />
6+
* 1) List the names of files in a directory<br />
7+
* 2) List the names along with metadata (size, lastModified)<br />
8+
* of files in a directory<br />
9+
* 3) List the names along with metadata (size, lastModified)<br />
10+
* of files in a directory and all subdirectories (using recursion)
11+
*/
12+
13+
14+
void setup() {
15+
16+
// Path
17+
String path = sketchPath;
18+
19+
println("Listing all filenames in a directory: ");
20+
String[] filenames = listFileNames(path);
21+
println(filenames);
22+
23+
println("\nListing info about all files in a directory: ");
24+
File[] files = listFiles(path);
25+
for (int i = 0; i < files.length; i++) {
26+
File f = files[i];
27+
println("Name: " + f.getName());
28+
println("Is directory: " + f.isDirectory());
29+
println("Size: " + f.length());
30+
String lastModified = new Date(f.lastModified()).toString();
31+
println("Last Modified: " + lastModified);
32+
println("-----------------------");
33+
}
34+
35+
println("\nListing info about all files in a directory and all subdirectories: ");
36+
ArrayList allFiles = listFilesRecursive(path);
37+
38+
for (int i = 0; i < allFiles.size(); i++) {
39+
File f = (File) allFiles.get(i);
40+
println("Name: " + f.getName());
41+
println("Full path: " + f.getAbsolutePath());
42+
println("Is directory: " + f.isDirectory());
43+
println("Size: " + f.length());
44+
String lastModified = new Date(f.lastModified()).toString();
45+
println("Last Modified: " + lastModified);
46+
println("-----------------------");
47+
}
48+
49+
noLoop();
50+
}
51+
52+
// Nothing is drawn in this program and the draw() doesn't loop because
53+
// of the noLoop() in setup()
54+
void draw() {
55+
56+
}
57+
58+
59+
// This function returns all the files in a directory as an array of Strings
60+
String[] listFileNames(String dir) {
61+
File file = new File(dir);
62+
if (file.isDirectory()) {
63+
String names[] = file.list();
64+
return names;
65+
} else {
66+
// If it's not a directory
67+
return null;
68+
}
69+
}
70+
71+
// This function returns all the files in a directory as an array of File objects
72+
// This is useful if you want more info about the file
73+
File[] listFiles(String dir) {
74+
File file = new File(dir);
75+
if (file.isDirectory()) {
76+
File[] files = file.listFiles();
77+
return files;
78+
} else {
79+
// If it's not a directory
80+
return null;
81+
}
82+
}
83+
84+
// Function to get a list ofall files in a directory and all subdirectories
85+
ArrayList listFilesRecursive(String dir) {
86+
ArrayList fileList = new ArrayList();
87+
recurseDir(fileList,dir);
88+
return fileList;
89+
}
90+
91+
// Recursive function to traverse subdirectories
92+
void recurseDir(ArrayList a, String dir) {
93+
File file = new File(dir);
94+
if (file.isDirectory()) {
95+
// If you want to include directories in the list
96+
a.add(file);
97+
File[] subfiles = file.listFiles();
98+
for (int i = 0; i < subfiles.length; i++) {
99+
// Call this function on all files in this directory
100+
recurseDir(a,subfiles[i].getAbsolutePath());
101+
}
102+
} else {
103+
a.add(file);
104+
}
105+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* HashMap example
3+
* by Daniel Shiffman.
4+
*
5+
* This example demonstrates how to use a HashMap to store
6+
* a collection of objects referenced by a key.
7+
* This is much like an array, only instead of accessing elements
8+
* with a numeric index, we use a String.
9+
* If you are familiar with associative arrays from other languages,
10+
* this is the same idea.
11+
*
12+
* This example uses the HashMap to perform a simple concordance
13+
* http://en.wikipedia.org/wiki/Concordance_(publishing)
14+
*/
15+
16+
17+
HashMap words; // HashMap object
18+
19+
String[] tokens; // Array of all words from input file
20+
int counter;
21+
22+
PFont f;
23+
24+
void setup() {
25+
size(640, 360);
26+
words = new HashMap();
27+
28+
// Load file and chop it up
29+
String[] lines = loadStrings("dracula.txt");
30+
String allText = join(lines, " ");
31+
tokens = splitTokens(allText, " ,.?!:;[]-");
32+
f = createFont("Georgia", 36, true);
33+
}
34+
35+
void draw() {
36+
background(51);
37+
fill(255);
38+
39+
// Look at words one at a time
40+
String s = tokens[counter];
41+
counter = (counter + 1) % tokens.length;
42+
43+
// Is the word in the HashMap
44+
if (words.containsKey(s)) {
45+
// Get the word object and increase the count
46+
// We access objects from a HashMap via its key, the String
47+
Word w = (Word) words.get(s);
48+
w.count();
49+
} else {
50+
// Otherwise make a new word
51+
Word w = new Word(s);
52+
// And add to the HashMap
53+
// put() takes two arguments, "key" and "value"
54+
// The key for us is the String and the value is the Word object
55+
words.put(s, w);
56+
}
57+
58+
// Make an iterator to look at all the things in the HashMap
59+
Iterator i = words.values().iterator();
60+
61+
// x and y will be used to locate each word
62+
float x = 0;
63+
float y = height-10;
64+
65+
while (i.hasNext()) {
66+
// Look at each word
67+
Word w = (Word) i.next();
68+
69+
// Only display words that appear 3 times
70+
if (w.count > 3) {
71+
// The size is the count
72+
int fsize = constrain(w.count, 0, 100);
73+
textFont(f, fsize);
74+
text(w.word, x, y);
75+
// Move along the x-axis
76+
x += textWidth(w.word + " ");
77+
}
78+
79+
// If x gets to the end, move Y
80+
if (x > width) {
81+
x = 0;
82+
y -= 100;
83+
// If y gets to the end, we're done
84+
if (y < 0) {
85+
break;
86+
}
87+
}
88+
}
89+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Word {
2+
3+
int count;
4+
String word;
5+
6+
Word(String s) {
7+
word = s;
8+
count = 1;
9+
}
10+
11+
void count() {
12+
count++;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)