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.

3
  • 9
    You aren't copying your arrays, you're just creating additional references to the exact same array. Commented Aug 28, 2018 at 17:22
  • wait, what? Can you explain that further please? Commented Aug 28, 2018 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. Commented Aug 28, 2018 at 17:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Edit: found my mistake, everything's great now. Thank you kind sir.
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.

2 Comments

Your array copy suggestion is not going to work, it is a 2d array.
Fuuu!, i miss that part, maybe fort answering quick, you are right, he just need to follow your link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.