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 › Putting image widths into an array
Page Index Toggle Pages: 1
Putting image widths into an array (Read 674 times)
Putting image widths into an array
Mar 31st, 2010, 7:32pm
 
I'm trying to put the widths and heights of randomly generated images into arrays (one each for width and height).  It's a very small scale program, but I'm having trouble using "img.width" to my advantage.  I keep getting that "expecting DOT" error!  Not including separate functions and irrelevant arrays, here is what I have:

Code:
int imageWidths[] = {};
 int imageHeights[] = {};
 
 int howMany = int(random(1,6));
 
 for(int i = 0; i <= howMany; i++){  //For-loop for drawing images; depends on "howMany"
   int xpoint = int(random(0,width));  //Choose x
   int ypoint = int(random(0,height)); //Choose y
   int whichImage = int(random(0,5)); //Choose which index of the array to take the image from
   PImage thisImage = imageArray[whichImage]; //Chosen image turns into a variable
   int[] imageWidths = (imageWidths, thisImage.width);
   int[] imageHeights = (imageHeights, thisImage.height);
   drawImage(thisImage, xpoint, ypoint);  //Draw the image!

Re: Putting image widths into an array
Reply #1 - Apr 1st, 2010, 1:28am
 
Arrays in Java (Processing) don't work like that...
For the record, if you re-declare int[] imageWidths for example, you create a new variable, local to the for loop, shadowing the outer imageWidths and "dying" on the next iteration of the loop.

In Processing, you can expand an array with:
imageWidths = append(imageWidths, thisImage.width);
imageHeights = append(imageHeights, thisImage.height);
Page Index Toggle Pages: 1