Old game developper not used to new APIs...

Hi all,

About 3 years ago I was working for Ubisoft building Java games. Back then (jdk 1.1.8), the fastest way to produce images with effects and all was to manipulate the image as an array of pixels (int[], ARGB) and to use a MemoryImageSource to produce the image.

Now I feel like building a game for the fun of it, but I'm lost in all the new stuff they added. First of all, I just wanted to have an image built on an array of pixels. So I tried the following code :

DirectColorModel model = new DirectColorModel(32,0x00ff0000,0x0000ff00,0x000000ff,0xff000000);

WritableRaster raster = model.createCompatibleWritableRaster(640, 480);

int[] array = new int[640 * 480];

for (int i = 0; i < array.length; i++)

array = 0x00ff0000;

raster.setPixels(0, 0, 640, 480, array);

BufferedImage img = new BufferedImage(model, raster, true, null);

panel.getGraphics().drawImage(img, 0, 0, this);

First, I get an ArrayIndexOutOfBounds on the setPixels() call. Why is that ? I mean, I specified the width and height to 640 * 480 but seems that the raster does not have the same widht and height ?

And second, if I use like setPixels(0,0,40,40,array), it displays some weird color scheme but in the array, I set all the pixels to 0x00ff0000 (red).

Could anyone help to get started ? I guess I am missing something here.

BTW, if anyone is interested in having a colloection of nice functions to manipulate arrays of pixels, I can send my old collection through e-mail.

Max, former game prog, but da** lost in all this...

[1614 byte] By [taipan] at [2007-9-27 19:14:38]
# 1
What projects did you work on? I'm just curious of course as I'm trying to become a game programmer. Did you ever work on the Game Boy Advance Hardware. That's my specialty!
KChalupa at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...
# 2

Hi, i had a look at the docs and it seems MemoryImageSource was built just for your needs.

In the docs it shows an example that does not need a WritableRaster,ColorModel, or BufferedImage. It just creates a Image from createImage(MemImgSrc) Creating Images like this also lets the vm attempt to hardware accelerate the image. Much better than using BufferedImages.

You mentioned pixel array functions, id really like to see what you've got! email harleyrana@telstra.com

Thanks. Harley.

harleyrana at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...
# 3
at the risk of turning this into a mailing list - I could also find a use for your int[] image manipulation stuff.perhaps you should post the code here, or even better, upload it somewhere and post a link.
Abuse at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...
# 4

> Could anyone help to get started ? I guess I am missing something here.

Useful API documentation? The whole area of Rasters seems to me to be thoroughly undocumented.

I don't know whether you've had a look at the source code for java.awt.GradientPaintContext (it's a package-private class). Rather than use setPixels(int, int, int, int, int[]), they use internal knowledge about the type of WritableRaster returned.

I think that if you want to understand what's going on, you'll have to wade through the source code (and it's not at all well commented).

Incidentally, what wierd colour scheme do you get? Is it a consistent block of colour, and if so can you paint your image onto a BufferedImage and use getRGB(int, int) to find out its value?

YATArchivist at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...
# 5

Hey, i wanted to see if it actually worked right, and it does.

public Image getImage(Component component){

int[] array = new int[640 * 480];

for (int i = 0; i < array.length; i++)

{

array = Color.red.getRGB();

}

Image image = component.createImage(new MemoryImageSource(640,480,array,0,640));

return image;

}

Also i think the ArrayOutofBounds error form raster.setPixels(0, 0, 640, 480, array) is because it takes an array[4] represent the color values to assign that pixel.

Hope that helps.

Harley.

harleyrana at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...
# 6
Hey you know i ment array = Color.red.getRGB();didn't you?
harleyrana at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...
# 7
My fault, i wasnt' puting it in the [code][i] was being deleted, duh!
harleyrana at 2007-7-6 21:44:52 > top of Java-index,Other Topics,Java Game Development...