[SOLVED] Java issue with referencing

So i’m trying to draw the pixels of a Bitmap to the screen with a modified colour. What i want, is for the image to be drawn with an offset colour but to not change the actual pixels stored for that image. Currently im having issues with this.

Here’s my method


public void renderWithColourOffset(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col1[],int col2[]){
		Bitmap b0 = new Bitmap(b);
		
		for(int i=0;i<b0.pixels.length;i++){
			for(int c=0;c<col1.length;c++){
				if(b0.pixels[i]==col1[c]){
					b0.pixels[i] = col2[c];
				}
			}
		}
		
		render(b0,xp,yp,xFlip,yFlip);
	}

The issue is that when it does this it changes to pixels in the original Bitmap parsed to the function so it wont work whenever the function is called again.

This seems to be an issue with java referencing but i’ve tried a lot of things and simply can’t get it to work :S

Oh and by the way, Merry Christmas :smiley:

Which library are you using? Could you show the Bitmap class

I’m using default Canvas with graphics stored in an RGB pixel array. The Bitmap class stores a pixel array, a width and a height

Here’s the full class: http://pastebin.com/sE8PcwKb

Line 15. Arrays are objects. You are simply referring to the same array as the Bitmap’s you passed in. You should instead copy the array: [icode]this.pixels = Arrays.copyOf(b.pixels, b.pixels.length);[/icode] (Arrays is a class in java.util package)

I suggest you learn more about Java and OOP before going into graphics.

I understand what you’re saying and i realize this should work. But it doesn’t, it still assign the pixels to the original Bitmap parsed

Your code doesn’t work, you’re right. If you replaced line 15 with my suggestion it should work. Please re-read my post…

EDIT: Just noticed you also need to replace line 29 too. You may need to replace line 22 only if you don’t want to modify the original BufferedImage.

It’s not needed to create a Bitmap class. You can simply use the Java2D.


public static BufferedImage mask(BufferedImage bimg, int[] cols1, int[] cols2){
    for (int y=0; y<bimg.getHeight(); y++){
         for (int x=0; x<bimg.getWidth(); x++){
              int col = bimg.getRGB(x, y);
              for (int i=0; i<cols1.length; i++){
                   if (col==cols1[i]){
                       bimg.setRGB(x, y, cols2[i]);
                       break;
                   }
              }
         }
    }
    return bimg;
}

Ahhh…

It wasn’t an issue with referencing, it was an issue with the array of bytes that i used to store data for each sprite :slight_smile:

Thanks anyway