Timo Nentwig said:
And how do I set that one integer? There's no Raster.setPixel() that
accepts a single integer.
Raster is general and doesn't know directly about any specific format.
getDataBuffer() still is generic.
If you know that the pixels are stored in ints, then you can cast the
DataBuffer that comes out of Raster's getDataBuffer() to
DataBufferInt.
DataBufferInt has a getData(), that returns int[], at which point you
can access the pixels directly.
How about starting with a BufferedImage, which wraps all the
colorspace/format issues all in one package:
BufferedImage image = new BufferedImage(640, 480,
BufferedImage.TYPE_INT_ARGB);
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
for (int y = 0; y < 480; y++)
for (int x = 0; x < 640; x++)
pixels[y * 640 + x] = x * y;
....
I'm not sure, but the compiler has some nifty optimization features,
where something like this may actually not be any faster:
for (int y = 0; y < 480; y++)
{
int offset = y * 640;
for (int x = 0; x < 640; x++)
pixels[offset + x] = x * y;
}
On an exciting side note, Java's bytecode to machine code translator
is efficient enough that it can compete and sometimes outperform
optimized C++ code! So raster-hacking in Java can be far more
exciting as many people with presumptions about Java are willing to
admit. Discover for yourself.
Also, Java2D is one of *THE* cleanest, if not *THE* cleanest graphics
library ever written. You get a lot of flexibility, performance,
extendability in an amazing package. By the looks of it, it's better
than GDI+, and most definitely way better than GNU land's GTK/GTK++.