Simple reference updates

This commit is contained in:
Casey Reas
2012-12-12 04:01:33 +00:00
parent 2e92b0eabc
commit 289f46ff61
11 changed files with 55 additions and 53 deletions

View File

@@ -3,9 +3,8 @@
* 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.
* 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.
*
@@ -23,6 +22,7 @@ int counter;
void setup() {
size(640, 360);
words = new HashMap();
// Load file and chop it up
@@ -51,8 +51,7 @@ void draw() {
} else {
// Otherwise make a new word
Word w = new Word(s);
// And add to the HashMap
// put() takes two arguments, "key" and "value"
// 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);
}
@@ -74,7 +73,7 @@ void draw() {
x += textWidth(w.word + " ");
}
// If x gets to the end, move Y
// If x gets to the end, move y
if (x > width) {
x = 0;
y -= 100;

View File

@@ -25,7 +25,9 @@ void setup()
// Set random cells to 'on'
for (int i = 0; i < sx * sy * density; i++) {
world[(int)random(sx)][(int)random(sy)][1] = 1;
int x = int(random(sx));
int y = int(random(sy));
world[x][y][1] = 1;
}
}

View File

@@ -35,7 +35,7 @@ void draw() {
update(mouseX, mouseY);
background(currentColor);
if(rectOver) {
if (rectOver) {
fill(rectHighlight);
} else {
fill(rectColor);
@@ -43,7 +43,7 @@ void draw() {
stroke(255);
rect(rectX, rectY, rectSize, rectSize);
if(circleOver) {
if (circleOver) {
fill(circleHighlight);
} else {
fill(circleColor);
@@ -53,7 +53,7 @@ void draw() {
}
void update(int x, int y) {
if( overCircle(circleX, circleY, circleSize) ) {
if ( overCircle(circleX, circleY, circleSize) ) {
circleOver = true;
rectOver = false;
} else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
@@ -65,10 +65,10 @@ void update(int x, int y) {
}
void mousePressed() {
if(circleOver) {
if (circleOver) {
currentColor = circleColor;
}
if(rectOver) {
if (rectOver) {
currentColor = rectColor;
}
}
@@ -85,7 +85,7 @@ boolean overRect(int x, int y, int width, int height) {
boolean overCircle(int x, int y, int diameter) {
float disX = x - mouseX;
float disY = y - mouseY;
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
} else {
return false;

View File

@@ -5,14 +5,13 @@
*/
Handle[] handles;
int num;
void setup() {
size(640, 360);
num = height/15;
int num = height/15;
handles = new Handle[num];
int hsize = 10;
for(int i=0; i<num; i++) {
for (int i = 0; i < handles.length; i++) {
handles[i] = new Handle(width/2, 10+i*15, 50-hsize/2, 10, handles);
}
}
@@ -20,7 +19,7 @@ void setup() {
void draw() {
background(153);
for(int i=0; i<num; i++) {
for (int i = 0; i < handles.length; i++) {
handles[i].update();
handles[i].display();
}
@@ -30,7 +29,7 @@ void draw() {
}
void mouseReleased() {
for(int i=0; i<num; i++) {
for (int i = 0; i < handles.length; i++) {
handles[i].releaseEvent();
}
}
@@ -39,7 +38,7 @@ class Handle {
int x, y;
int boxx, boxy;
int length;
int stretch;
int size;
boolean over;
boolean press;
@@ -50,19 +49,19 @@ class Handle {
Handle(int ix, int iy, int il, int is, Handle[] o) {
x = ix;
y = iy;
length = il;
stretch = il;
size = is;
boxx = x+length - size/2;
boxx = x+stretch - size/2;
boxy = y - size/2;
others = o;
}
void update() {
boxx = x+length;
boxx = x+stretch;
boxy = y - size/2;
for(int i=0; i<others.length; i++) {
if(others[i].locked == true) {
for (int i=0; i<others.length; i++) {
if (others[i].locked == true) {
otherslocked = true;
break;
} else {
@@ -70,18 +69,18 @@ class Handle {
}
}
if(otherslocked == false) {
if (otherslocked == false) {
overEvent();
pressEvent();
}
if(press) {
length = lock(mouseX-width/2-size/2, 0, width/2-size-1);
if (press) {
stretch = lock(mouseX-width/2-size/2, 0, width/2-size-1);
}
}
void overEvent() {
if(overRect(boxx, boxy, size, size)) {
if (overRect(boxx, boxy, size, size)) {
over = true;
} else {
over = false;
@@ -89,7 +88,7 @@ class Handle {
}
void pressEvent() {
if(over && mousePressed || locked) {
if (over && mousePressed || locked) {
press = true;
locked = true;
} else {
@@ -102,11 +101,11 @@ class Handle {
}
void display() {
line(x, y, x+length, y);
line(x, y, x+stretch, y);
fill(255);
stroke(0);
rect(boxx, boxy, size, size);
if(over || press) {
if (over || press) {
line(boxx, boxy, boxx+size, boxy+size);
line(boxx, boxy+size, boxx+size, boxy);
}

View File

@@ -10,7 +10,7 @@
HScrollbar hs1, hs2; // Two scrollbars
PImage img1, img2; // Two image to load
PImage img1, img2; // Two images to load
void setup() {
size(640, 360);
@@ -74,21 +74,21 @@ class HScrollbar {
}
void update() {
if(overEvent()) {
if (overEvent()) {
over = true;
} else {
over = false;
}
if(mousePressed && over) {
if (mousePressed && over) {
locked = true;
}
if(!mousePressed) {
if (!mousePressed) {
locked = false;
}
if(locked) {
if (locked) {
newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
}
if(abs(newspos - spos) > 1) {
if (abs(newspos - spos) > 1) {
spos = spos + (newspos-spos)/loose;
}
}
@@ -98,7 +98,7 @@ class HScrollbar {
}
boolean overEvent() {
if(mouseX > xpos && mouseX < xpos+swidth &&
if (mouseX > xpos && mouseX < xpos+swidth &&
mouseY > ypos && mouseY < ypos+sheight) {
return true;
} else {
@@ -110,7 +110,7 @@ class HScrollbar {
noStroke();
fill(204);
rect(xpos, ypos, swidth, sheight);
if(over || locked) {
if (over || locked) {
fill(0, 0, 0);
} else {
fill(102, 102, 102);