2

I'm not very good at programming but right now, I need to do program a little Java application and I'm running into a weird problem. I've been trying to solve this for hours now.

Here's the problem: I'm saving a small amount of my screen (20x20) to my program. I do this by looping through every pixel, saving it's RGB into an array via Java.awt.robot. With the following function, the program should save the copied image to 3 arrays (R, G and B) before getting the new area of the screen (I want to compare them later and look for changes). The one damn thing: The old arrays in which I save the data before overwriting the main arrays are always overwriting without me telling them to.

private void fillArrayData(){
    oldDataR = dataR;   <----- The problem is here. These arrays are now overwritten with the
    oldDataG = dataG;   <----- current data, just before I write stuff to dataR, G and B.
    oldDataB = dataB;   <----- As you see, I don't modify oldDataR, G, B later on.

    scanArea.x = MouseInfo.getPointerInfo().getLocation().x;
    scanArea.y = MouseInfo.getPointerInfo().getLocation().y;
    for(int i = 0; i<scanSize; i++){
        for(int n = 0; n<scanSize; n++){
            dataR[i][n] = (rbt.getPixelColor(scanArea.x+i, scanArea.y+n)).getRed();
            dataG[i][n] = (rbt.getPixelColor(scanArea.x+i, scanArea.y+n)).getGreen();
            dataB[i][n] = (rbt.getPixelColor(scanArea.x+i, scanArea.y+n)).getBlue();
        }
    }
}

Even though I never access oldDataR, oldDataG and oldDataB later on, it everytime is equal to the dataR, dataG and dataB after this void finishes. That doesn't make sense as I'm writing new data to the three main arrays (dataR, dataG, dataB) AFTER I saved them to the oldData-Arrays. And yes, I made sure that the data which is received by rbt.getPixelColor is not the same as before.

Please help me, I'm really frustrated by now but need to keep going.

Joey
  • 207
  • 4
  • 11
  • 9
    You aren't copying your arrays, you're just creating additional references to the exact same array. – CollinD Aug 28 '18 at 17:22
  • wait, what? Can you explain that further please? – Joey Aug 28 '18 at 17:24
  • `oldDataR-G-B` will get a reference to `dataR-G-B`, meaning that, any changes made to `dataR-G-B` will also be reflected in `olddataR-G-B`. What you want is a deep copy of the array values, where the value themselves are copied, not the references. – user3170251 Aug 28 '18 at 17:34

2 Answers2

5

That is happening because oldDataR (and the rest) is just another variable pointing to the same array, if you want to keep the old values in the array separately, and modify the original one, you need to copy it.

This post can be helpful for copying two-dimensional arrays: copy a 2d array in java

khachik
  • 28,112
  • 9
  • 59
  • 94
  • Thanks, that makes sense. It still isn't working though, now my old RGB is always 0 for every single value but I'm too confused right now to tell the reason. – Joey Aug 28 '18 at 18:11
  • Edit: found my mistake, everything's great now. Thank you kind sir. – Joey Aug 28 '18 at 18:18
2

You are passing your arrays as references, so, in the end it is the same array in the pointers of the two variables.

Have you tried array copy?

System.arraycopy()

or

Arrays.copyOf()

Your code would look like:

oldDataR = Arrays.copyOf(dataR);

Edit

I missed the multi dimension of the arrays, just follow @khachik link suggestion so you can handle the two dimensions.

rekiem87
  • 1,565
  • 18
  • 33