Can someone please explain this to me :)

Experimenting with a new way to render images, trying to understand what notch did with his rendering which is quite popular for Java games now, I’m slightly confused on how this bit of code is working.


BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
			BufferedImage.TYPE_INT_ARGB);
int pixels[] = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

and then..
render(Graphics g){
 g.drawImage(image..etc)
}


How is editing pixels affecting image?

The pixels array holds the data for the image, so if you change the contents of the array you change the image.
For example;


BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
         BufferedImage.TYPE_INT_ARGB);
int pixels[] = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

for (int i=0; i<pixels.length; i++)
{
    pixels[i]=0xff000000;
}
and then..
render(Graphics g){
 g.drawImage(image..etc)
}

would make the image completely black.

I’m still confused, Image is independent. Then you are saying pixel array is equal to the contents of image so now pixels has the contents of Image however how does that change image’s contents.

I mean I would imagine this,
Image = pixels;

but we have so…
pixels = image is only setting the contents of pixels to image not the reverse right?

An image is made up of a pixel array

If you edit that pixel array, the outputted image changes.

Warning! Bad metaphor:

Say I take a window out of you house, smash it, and put it back. Your house still has a broken window, even though I didn’t smash it while it was on your house.

Right I understand that however I don’t understand how we are linking
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_ARGB);
int pixels[] = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
^ This is just copying this?^

^but how does editing this have any effect on ^
Since we are just copying the right to the left?

My only assumption is that this is working as pass by reference, I didn’t realize java allows pass by reference for arrays?
Scratch that right…java is only pass by value

That’s right, Java is call by value. But here the value is indeed the address of the array. It is quite a trick, because otherwise you would have to copy the complete array, which could be a huge amount of work and memory.
So yeah, it is a copy of the address pointing at the very same part of memory.

So the value is the address to the array. Which is a reference.
We give the address as a value to the new variable here:

int pixels[]                      =      ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

So actually this behaves like working with reference.

If you knew pointers from c or c++ it would be easyer to explain.

((DataBufferInt) image.getRaster().getDataBuffer()).getData() does not copy the data of the image, but returns the memory adres of this collection.
So int pixels[] is just an direct acces to the image data, its not an copy of the data.

I cant find an good tutorial to explain pointers easly, but just keep in mind int pixels[] is an direct pipeline to the image, not just an copy of some pixels.

Spot on

No need to confuse yourself with pointers, although they’re pretty much the same thing. Java does indeed pass by value. But since an array in java is an Object and since Objects are manipulated by reference, so are arrays (because they’re objects).

Meaning that you’re copying the reference into a new variable.

Just like:

Object obj1 = new Object();
Object obj2 = obj1;

// obj2 is not a copy of obj1, rather, it copied obj1's reference so that they both now refer to the same object

http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html

Arrays are objects in java. Objects are manipulated by reference, therefore you could say that you’re passing objects by reference - which is the same as passing by value because the reference is passed by value.

Basic types like short, int, long etc are not Objects. This can be confusing because arrays of these basic types like int[] and byte[] ARE objects. Which is why they’re passed by reference.

Whoops, I suspect you meant “Which is why they’re references (passed by value)”. :slight_smile:

Java has no PBR semantics at all. PBR would mean you could write [icode]void swap(Object a, Object b)[/icode], which is impossible in java (skullduggery with JNI or sun.misc.unsafe notwithstanding).

Ah, ok makes sense I wrote some tests to verify to further my understanding. Still manage to confuse myself sometimes even though I have a more than novice skill set in java lol.
And yes I know how C++ pointers work for the most part

Thanks for all the responses/help :slight_smile: