We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi,
I have this program which displays images in an array, but I need to convert them into a 2D array. This is for loading a single array, i need to create a 2d array of images at the same place,
so i have two arrays of two different sets of images, that i need to display at the same place.
PImage [][] movEyes1 = new PImage[24] [17] ;/
how can i create this into a 2d array? Will something like this work
`` for (int i=0; i< movEyes1.length; i++)`
for (int j=0; i< movEyes1.length; i++)
{
PImage img = loadImage( i +"h.jpg","c.jpg");// rebecca
PImage img1 = loadImage( j+"c.jpg");// rebecca
movEyes1[i] [j] = img.get(130, 170, 310, 100); //Get a portion of the loaded image with displaying it
}``
in setup and then in draw i write
`// have to make 2d arrays, for mov, with three arrays to call at random
int idx = (int)map(mouseX, 0, 2*width, 0.0, movEyes1.length -1);
image(movEyes1[i], 0, 0, movEyes1[idx].width, movEyes1[idx].height -1);
`
Can someone help? Thanks
Answers
You might want to format your code
Select, hit ctrl-o
In the 2nd for-loop you want to use only j (and not a mix of j and i)
also j<movEyes[i].length
Now you load 2 images but you use only 1 ...?
Ah sorry, stupid error, I am confused on what to put in the draw function?
Is for a single array, I want the same for both arrays . I want to draw both arrays one after the other, looped ? how would i do that?
thanks!
Hm..... not sure why you need a 2D Array at all
Anyway, you can just use mouseY and apply it to idy (the way you do now for idx)
then use idx and idy together to access one image in the 2D array
i get an error which says the global variable width doesn't exist? which is weird because
int idx = (int)map(mouseX, 0, 2*width, 0.0, mEyes1.length -1); image(mEyes1[idx], 0, 0, mEyes1[idx].width, mEyes1[idx].height -1);
There are two separate arrays with a set of images in each. I want to call one array after the other. So it cycles through one array and then jumps to the other.
thanks!
Are we still talking 2D arrays? Because they are not separated
Why
mEyes1[idx][idy].width
I guess that's the width that's causing the error - because you try to access a 2D arrray as if it were 1D....
Oh it works, but displays only one image. I think declaring
PImage [][] movEyes1 = new PImage[24] [17] ;/
is wrong?because I assume this is a matrice? Because the values I put in are the number of images I have in that array?
No this line is ok
If you have 24*17 images
But as I said your loading process is a bit surprising
Try to println idx and idy if they come out right
The values are 0, because its only displaying one image. I'm sorry, I am new at this so I might be very wrong. I need it to cycle through the 1st array with 24 images, then the second array with 17 images. One after another, in the same position.
Atm you are using the mouse pos
When you want to use automatic switch from image to image like in a Grid (showing one image at a time) :
increase idx by 1 in draw() until 24 is reached then say idy++; and idx=0;
This is like when you read you start a new line
Repeat while idy<17
etc.
there is a Tutorial on 2D arrays. A 2D array is a grid (like a Game of chess board)
You have 24 x 17 images!!!!!! So over 300 images
Not 2 arrays with 24 + 17 images. As you said.
You need to get the difference.
A grid with the sides 24 and 17 long.
(You can make a 2D array with only 2 rows, the first 24 long and the second 17 but thats different from what you have now)
that is a typical GRID example
it has no images but just shows how to display a grid (or what it is)
above a grid which automatically shows the 24*17 elements one after another
this shows an array that contains 2 arrays of different length
(it's a 2D array but not in the common sense (which would be a full grid))
Right so I kind of solved it, so array is compiled into one and i just call it in setup. And i added frameNum, to loop it. I actually have images which are gifs and i wanted to display them one after the other. You were right, i had to remove mouse . Thank you so MUCH!
Not sure the for loop in draw is needed!