Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can someone please explain this to me :)  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
Offline JayTech

Junior Member


Medals: 1



« Posted 2013-01-19 06:00:04 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
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?
Offline SimonH
« Reply #1 - Posted 2013-01-19 06:11:32 »

The pixels array holds the data for the image, so if you change the contents of the array you change the image.
For example;
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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.

People make games and games make people
Offline JayTech

Junior Member


Medals: 1



« Reply #2 - Posted 2013-01-19 06:17:29 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 214
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #3 - Posted 2013-01-19 06:23:27 »

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.

Offline JayTech

Junior Member


Medals: 1



« Reply #4 - Posted 2013-01-19 06:26:53 »

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
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #5 - Posted 2013-01-19 09:54:32 »

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:
1  
int pixels[]                      =      ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

So actually this behaves like working with reference.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #6 - Posted 2013-01-19 10:19:20 »

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.
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #7 - Posted 2013-01-19 17:50:09 »

Spot on

- Jonas
Offline jonjava
« Reply #8 - Posted 2013-01-19 20:15:02 »

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:

1  
2  
3  
4  
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

Offline jonjava
« Reply #9 - Posted 2013-01-19 20:24:29 »

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

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)". Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 200



« Reply #10 - Posted 2013-01-19 21:03:48 »

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)". Smiley

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

Junior Member


Medals: 1



« Reply #11 - Posted 2013-01-19 22:55:36 »

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 Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!