0

I'm currently following a series on Java game development from scratch. I understand most java and oop concepts but have very little experience when dealing with graphics and hardware acceleration.

The lines of code that I am questioning are:

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

The BufferedImage "image" variable is always the variable being drawn to the screen through a render method. Usually something like:

 public void render() {
    BufferStrategy bs = this.getBufferStrategy;
    if(bs == null) { this.createBufferStrategy(3); return; }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();
 }

I understand the array of pixels contains every pixel within the BufferedImage, however, it seems as though everytime that array is filled with values it directly effects the contents of the "image" variable. There is never a method used to copy the pixels array values into the image.

Are these varibales actually linked in such a way? Does the use of:

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

create an automated link between the image and the array being created in the code above? Maybe I am going cray and just missed something but I have reviewed the code several times and not once is the "image" varibale manipulated after it's initial creation. (besides being rendered to the screen of course.) It's always the array "pixels" just being filled with different values that causes the change in the rendered image.

Some insight on this would be wonderful. Thank you in advance!

1
  • 1
    Basically, when you ask of an array of pixels you have direct access to the image data. So any time you change the array, it's changed immediately... Commented Nov 29, 2013 at 20:10

2 Answers 2

1

Why don't you call

image.getData()

instead of

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

image.getData() returns a copy of the Raster. getRaster() returns a WriteableRaster with the ability to modify pixels. I'm guessing but getRaster() probably returns a child of the image Raster and therefore is writeable if you modify the array. Try image.getData() to see if it works. If not, post back here and I'll take a closer look.

I looked into this further. The source code that comes with the JDK shows that image.getRaster().getDataBuffer().getData() returns the source data array. image.getData() indeed returns a copy. If the image is modified, the data in getData() will not be modified.

You can call getPixels on the returned Raster:

public int[] getPixels(int x,
              int y,
              int w,
              int h,
              int[] iArray)

Returns an int array containing all samples for a rectangle of pixels, one sample per array element. An ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds. However, explicit bounds checking is not guaranteed.

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

7 Comments

image.getData() does return a Raster. It does not, however, seem to allow me to store the data it returns as an array of integers. Verbatim the code processes as: image.getRaster() // returns a raster of the image .getDataBuffer () // returns the buffer object .getData() // returns the data within the raster as an array Then casting it to a DataBufferInt insures the data stored in the returned is if tye DataBufferInt - a subclass of DataBuffer. Hence the new private "int[]" pixels portion.
Call getPixels() on the returned raster to get an int[] array of pixels. See my new edit in my answer.
I don't understand the new edits to your comment. Can you please clarify? You can get an int[] out of the Raster by calling getPixels() as I posted above.
`` private int[] pixels = img.getData().getPixels(0, 0, WIDTH, HEIGHT, null); `` I am givin an error "The method getPixels(int, int, int, int, int[]) is ambiguous for the type Raster" In other words I think it's saying it wants to insure the raster is of type DataBuffer whether it be DataBufferInt or DataBufferDouble or so have you. That's the reason for the extended piece of code in my original question. Do you follow? I know I am confusing even myself.
Weird. I'll look into it.
|
0

Use int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); instead of image.getData(). image.getData() just returns a copy of the image data where image.getRaster() returns the original pixel array allowing to actually write pixels to it.

Comments

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.