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 › How to store positions for images of an array
Page Index Toggle Pages: 1
How to store positions for images of an array (Read 2145 times)
How to store positions for images of an array
Dec 14th, 2009, 2:53am
 
Hi,
I found this great code from Daniel Shiffman to load 10 images, that are stored in an array. I would like to diplay the images on specified positions. Each image has an index-nr. in the array, that should correspond to a specified postion. Does anybody did tra this?

thanks!

frank

-------------

int imgCount = 10;
PImage[] imgs = new PImage[imgCount];
float imgW;

// Keeps track of loaded images (true or false)
boolean[] loadStates = new boolean[imgCount];

// For loading animation
float loaderX, loaderY, theta;

void setup() {
 size(800, 400);
 smooth();
 imgW = width/imgCount;

 // Load images asynchronously
 for (int i = 0; i < imgCount; i++){
   imgs[i] = requestImage("dublin"+i+".jpg");
 }
}

void draw(){
 background(0);
 
 // Start loading animation
 runLoaderAni();
 
 for (int i = 0; i < imgs.length; i++){
   // Check if individual images are fully loaded
   if ((imgs[i].width != 0) && (imgs[i].width != -1)){
     // As images are loaded set true in boolean array
     loadStates[i] = true;
   }
 }
 // When all images are loaded draw them to the screen
 if (checkLoadStates()){
   drawImages();
 }
}

void drawImages(){
 for (int i = 0; i < imgs.length; i++){
   image(imgs[i], width/10*i, 0, imgW, height/10);
   //image(imgs[i], width/10*i, 0, imgW, height);
 }
}

// Loading animation
void runLoaderAni(){
 // Only run when images are loading
 if (!checkLoadStates()){
   ellipse(loaderX, loaderY, 10, 10);
   loaderX += 2;
   loaderY = height/2 + sin(theta) * (height/2.5);
   theta += PI/22;
   // Reposition ellipse if it goes off the screen
   if (loaderX > width + 5){
     loaderX = -5;
   }
 }
}

// Return true when all images are loaded - no false values left in array
boolean checkLoadStates(){
 for (int i = 0; i < imgs.length; i++){
   if (loadStates[i] == false){
     return false;
   }
 }
 return true;
}







Re: How to store positions for images of an array
Reply #1 - Dec 14th, 2009, 4:24am
 
Add PVector[] imgPos = new PVector[imgCount];
and use this new array to store x/y coordinates of the images. Use it in the image() call to position them.
Re: How to store positions for images of an array
Reply #2 - Dec 14th, 2009, 4:25am
 
you can easily use a 2d array to store the x and y coordinate of every image/index

http://processing.org/learning/tutorials/2darray/

if this tutorial doesnt make it clear, ill come back to you. But give it a try Smiley
Re: How to store positions for images of an array
Reply #3 - Dec 25th, 2009, 1:54am
 
Cedric wrote on Dec 14th, 2009, 4:25am:
you can easily use a 2d array to store the x and y coordinate of every image/index

It is possible, but it is also crap. Please don't do this and please don't encourage others to do this. Someone (who knows what they are doing) can do this if they really want/need to, but it should not be encouraged for beginners as it is a bad habit that is better not learned.

(position[i][0], position[i][1])

is bad news.
Re: How to store positions for images of an array
Reply #4 - Dec 25th, 2009, 2:27am
 
So why is it?
Maybe an explaination would be useful here.
Re: How to store positions for images of an array
Reply #5 - Dec 25th, 2009, 7:28am
 
I think subpixel made the point quite well here.  Why use confusing 2d arrays when objects like PVector make things much more accessible/readable
Re: How to store positions for images of an array
Reply #6 - Dec 25th, 2009, 10:49am
 
ah k, i remember his threat. i understand his point but dont have a big problem with using a 2d array to store 10 coordinates. I started to do it that way before pvector was introduced. So thats a bad habit im still used to.  but its true and Philho is right, its probably easier for beginners so you should use a pvector instead although you probably wont use all the calculations you can do when using them.
Re: How to store positions for images of an array
Reply #7 - Dec 25th, 2009, 11:41pm
 
PhiLho  wrote on Dec 14th, 2009, 4:24am:
Add PVector[] imgPos = new PVector[imgCount];
and use this new array to store x/y coordinates of the images. Use it in the image() call to position them.


Further detail:

Code:

imgPos[0] = new PVector(20, 10); // first image location

void drawImages(){
for (int i = 0; i < imgs.length; i++){
// width and height taken from the image
  image(imgs[i], imgPos[i].x, imgPos[i].y);
}
}
Re: How to store positions for images of an array
Reply #8 - Feb 8th, 2010, 5:41am
 
Dear PhiLho,
I tried to implement the Vector solution, but always get thuis error:

---------------------------------------------------
Exception in thread "Animation Thread" java.lang.NullPointerException
     at _1_array_images2exp2.drawImages(_1_array_images2exp2.java:59)
     at _1_array_images2exp2.draw(_1_array_images2exp2.java:50)
     at processing.core.PApplet.handleDraw(PApplet.java:1425)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)

---------------------------------------------------
Here the code:

int imgCount = 10;
PImage[] imgs = new PImage[imgCount];
float imgW;
PVector[] imgPos = new PVector[imgCount];
// Keeps track of loaded images (true or false)
boolean[] loadStates = new boolean[imgCount];
// For loading animation
float loaderX, loaderY, theta;


void setup() {
 size(800, 600);
 smooth();
 frameRate(1);
 imgPos[0] = new PVector(20, 10); // first image location
 imgW = width/imgCount;  
  // Load images asynchronously
 for (int i = 0; i < imgCount; i++){
   imgs[i] = requestImage("dublin"+i+".jpg");
 }
}

void draw(){
 background(#FFFFFF);
 for (int i = 0; i < imgs.length; i++){
   // Check if individual images are fully loaded
   if ((imgs[i].width != 0) && (imgs[i].width != -1)){
     // As images are loaded set true in boolean array
     loadStates[i] = true;
   }
 }
 // When all images are loaded draw them to the screen
 if (checkLoadStates()){
   drawImages();
 
 }
}
void drawImages(){
for (int i = 0; i < imgs.length; i++){
   // width and height taken from the image
     image(imgs[i], imgPos[i].x, imgPos[i].y);
}
}
// Return true when all images are loaded - no false values left in array
boolean checkLoadStates(){
 for (int i = 0; i < imgs.length; i++){
   if (loadStates[i] == false){
     return false;
   }
 }
 return true;
}




THANKS one more time.-

fr


Re: How to store positions for images of an array
Reply #9 - Feb 8th, 2010, 7:48am
 
You have set only imgPos[0], so when you try to use imgPos[1] you get an NPE.
Re: How to store positions for images of an array
Reply #10 - Feb 8th, 2010, 9:00am
 
inst wrote on Dec 14th, 2009, 2:53am:
I would like to diplay the images on specified positions.

..which of course means you need to specify the positions!

Code:
  imgPos[0] = new PVector(???, ???); // 1st image location
imgPos[1] = new PVector(???, ???); // 2nd image location
imgPos[2] = new PVector(???, ???); // 3rd image location
imgPos[3] = new PVector(???, ???); // 4th image location
// ...
imgPos[9] = new PVector(???, ???); // 10th image location


where hopefully it is obvious that you need to fill in the '???' parts with the co-ordinates you want!

-spxl
Re: How to store positions for images of an array
Reply #11 - Feb 9th, 2010, 12:45am
 
Thanks Subpixel,
I  specified the positions and it worked. Actually this was the half way  of what, I wanted to do. My goal is to move  the pictures from the middle of the screen to the border of the screen. But each image should move in a different direction and with differnt steps.- The trigger for this movement action  comes by time control.

Here the relevant passage  with random, but here I can´t cotrol the movement:

void drawImages(){
 for (int i = 0; i < imgs.length; i++){
 float a=random(9);
 int r1 = int(a);
 float b=random(9);
 int r2 = int(b);
 image(imgs[i], imgPos[r1].x, imgPos[r2].y);
}



fr
Re: How to store positions for images of an array
Reply #12 - Feb 9th, 2010, 9:10pm
 
You need to go and do some reading / tutorials on how to program.

Having people answer every step of your problem is not going to help you.

See the Learning link in the navigation at the top of this page.

-spxl
Re: How to store positions for images of an array
Reply #13 - Feb 10th, 2010, 12:23am
 
Sorry for this. I´ve thought,  this is a forum and discussion area...

Where everybody could ask (even silly) questions, but no one has to answer (silly) questions.

fr


Re: How to store positions for images of an array
Reply #14 - Feb 10th, 2010, 1:07am
 
You can ask, but I suggest you could do better.

My recommendation is that you do (or at least read through) some tutorials, where you will learn basic ideas like how to move an object over time.

It is obvious you haven't done this, and the tutorials are there to teach you, which makes more sense than having people answer questions about basic concepts over and over.

The answer you seek is in the tutorials.

Read the tutorials.

-spxl

PS: The new drawImages() method you posted appears terribly confused. You evidently are reaching out to do something, but without some background knowledge (such as gained from the tutorials) you will remain hopelessly lost.
Page Index Toggle Pages: 1