We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Rotating Array of images
Page Index Toggle Pages: 1
Rotating Array of images (Read 546 times)
Rotating Array of images
Nov 20th, 2008, 6:52am
 
new at this,
i pulled this code to rotate a series of rectangles on random sizes, widths, heights, etc. but i'd like to replace the rectangles by different images...can't it out

here's your typical code for rotating 20 rectangles:

Rotater[] rotaters;

void setup() {
 size(200,200);
 rotaters = new Rotater [20];
 for (int i = 0; i < rotaters.length; i++) {
   rotaters[i] = new Rotater(random(width), random(height), random(-0.1,0.1), random(48));
 }
}

void draw () {
 background(255);
 for (int i = 0; i < rotaters.length; i ++) {
   rotaters[i].spin();
   rotaters[i].display();
 }
}

class Rotater {
 float x,y;
 float theta;
 float speed;
 float w;
 
 Rotater (float tempX, float tempY, float tempSpeed, float tempW) {
   x = tempX;
   y = tempY;
   theta = 0;
   speed = tempSpeed;
   w = tempW;
 }
 
 void spin() {
   theta += speed;
 }
 
 void display() {
   rectMode(CENTER);
   stroke(0);
   fill(0,100);
   pushMatrix();
   translate(x,y);
   rotate(theta);
   rect(0,0,w,w);
   popMatrix();
 }
}


need images in there
Re: Rotating Array of images
Reply #1 - Nov 20th, 2008, 4:05pm
 
Nice. It would be improved with smooth() after the size() line.

It doesn't take much to replace the rectangles with images:
Code:
Rotater[] rotaters;
// I use an absolute path for convenience (I don't save!),
// just put the image(s) in the data dir of your sketch
String IMAGE_PATH = "D:/_PhiLhoSoft/Processing/icon.png";

void setup() {
size(200,200);
smooth();
imageMode(CENTER);
rotaters = new Rotater[20];
for (int i = 0; i < rotaters.length; i++) {
// You can have a different image per rotater!
rotaters[i] = new Rotater(random(width), random(height),
random(-0.1,0.1), random(48), IMAGE_PATH);
}
}

void draw () {
background(255);
for (int i = 0; i < rotaters.length; i ++) {
rotaters[i].spin();
rotaters[i].display();
}
}

class Rotater {
float x,y;
float theta;
float speed;
float w;
PImage icon;

Rotater (float tempX, float tempY, float tempSpeed, float tempW, String imageName) {
x = tempX;
y = tempY;
theta = 0;
speed = tempSpeed;
w = tempW;
icon = loadImage(imageName);
}

void spin() {
theta += speed;
}

void display() {
rectMode(CENTER);
stroke(0);
fill(0,100);
pushMatrix();
translate(x,y);
rotate(theta);
// rect(0,0,w,w);
image(icon, 0, 0, w, w);
popMatrix();
}
}
Re: Rotating Array of images
Reply #2 - Nov 20th, 2008, 5:55pm
 
it's about to work, but at the section:

Rotater (float tempX, float tempY, float tempSpeed, float tempW, String imageName)

when i run it, this always pops up:

"expecting IDENT, found "imageName" "

i don't know if i wrote the directory wrong or what?
heres my entire code with your suggestions:


Rotater[] rotaters;
String IMAGE_PATH = "/Volumes/Storage/student/Desktop/sketch_081120a/naiia1.jpg";


void setup() {
 size(200,200);
 smooth();
 imageMode(CENTER);
 rotaters = new Rotater[01];
 for (int i = 0; i < rotaters.length; i++) {
   rotaters[i] = new Rotater(random(width),random(height),random(-0.1,0.1),random(48),IMAGE_PATH);
 }
 
}

void draw() {
 background(255);
 for (int i = 0; i < rotaters.length; i++) {
   rotaters[i].spin();
   rotaters[i].display();
 }
 
}



class Rotater {
 float x,y;
 float theta;
 float speed;
 float w;
 PImage icon;
 
 Rotater(float tempX, float tempY, float tempSpeed, float tempW, String "naiia1.jpg") {
   x = tempX;
   y = tempY;
   theta = 0;
   speed = tempSpeed;
   w = tempW;
   icon = loadImage(naiia1.jpg);
 }
 
 void spin() {
   theta += speed;
 }
 
 void display() {
   rectMode(CENTER);
   stroke(0);
   fill(0,100);
   pushMatrix();
   translate(x,y);
   rotate(theta);
   rect(0,0,w,w);
   image(icon,0,0,w,w);
   popMatrix();
 }
}
Re: Rotating Array of images
Reply #3 - Nov 20th, 2008, 8:02pm
 
I don't see

Rotater (float tempX, float tempY, float tempSpeed, float tempW, String imageName)

but

Rotater(float tempX, float tempY, float tempSpeed, float tempW, String "naiia1.jpg")

which is obviously wrong...
Page Index Toggle Pages: 1