We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, how is it going?
Im doing an android app for making pixelArt. Im using 2 16x16 matrixes of a Pixel object. Basicaly, inside the object there is: int x, y, tam; color cor; boolean pixel = false;
x and y are normal position values, tam is the size of the square, cor is the color of that pixel, the boolan is because when its true it draws using a universal brush color, when false it draws using a universal background color.
So, what Im trying to implement is a button to copy matrix 1 to matrix 2 (remembering there are two 16x16 matrixes of this object). It only shows 1 matrix at a time, and the idea is to make 2 frames animations, so I need this feature so the user does not have to make the same draw two times just to change one small detail wich is gonna be animated.
I have tried some different ways and whats happening is that it does copy, but after copying they stay linked, and what ever change I draw in one is made instantly in the other. Here`s what Ive tried:
Matrix2 = Matrix1;
or
for (int i = 0; i < 16; i++){ for (int j = 0; j < 16; j++){ matrix2[i][j].pixel = matrix1[i][j].pixel; matrix2[i][j].cor = matrix1[i][j].cor; }
or
arrayCopy(Matrix1, Matrix2);
all of them give the same result, instantly copies the matrix to the other, but when I change something and go back to the other, it has also changed the other, even without pressing to copy again.
I didnt paste the code because its too big, its already working for android and only this feature missing, posting the code would maybe distract from the problem (too many variables and other stuff unrelated to this problem) but if necessary Ill post. We can face this as a simple problem, when you use this comands above seems like it does not only copy the values between the matrixes but it link them and future changes affect one another.
Answers
I tought this could be confuse so I have prepared a test, heres the code
after copying using m2 = 1 or arrayCopy(m1, m2); when i press the mouse both matrixes are affected by the increment of the X value, and it shouldnt be that way, should it?
using the for loop nothing even happens.. What am I missing?
thanks in advance for trying to understand this messy discussion. Bad english and noob skillzzzz
=(
The for-loop would work. But you overwrite the values in draw(), so you never see that the values have actually been copied. Try to move that part to mousePressed() for example.
To copy a matrix you need to understand what this statement means -
int[][] m1 = new int[2][2];
What is
m1
?Those not familiar with the inner workings of Java would say it is a '2x2 array of integers' but in reality it is not.
In Java
m1
is an object reference to a 2D array of integers, it is NOT the actual array. In fact the 2D array could be any size we like. For instance after these statementsm1
is still an object reference to a 2D array of integers, but the array is now 20x10So now back to your code
So after lines 1 and 2 we have 2 object references
m1
andm2
both of which reference a separate 2D array of integersbut then we have line 4. Here we copy the object reference m1 into the object reference m2 so we have
Now both
m1
andm2
refer to the same array, so changingm1
also changesm2
and vice-versa.Of course it begs the question what happened to the second array? Since there is no reference to the array then it will be deleted from memory by Java's garbage collector.
The moral of the story is that to copy an array you must copy the array elements.
https://forum.processing.org/two/discussion/12044/how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array
for loop works, thanks a lot for the answers and explanations