[java] Fast color-switching

Started by
2 comments, last by CodeMachine 17 years, 7 months ago
Hi I have a lot of circles. Inside each circle there is a smaller circle but in a different color. (The screen is filled up with circles). The colors are interpolated: red -> green -> black -> red Now I want to move this color-schema one step to get a nice effect. In asm you do this effectively by changing the color in the color-palette (each pixel is pointing to a color in the color-palette). This way we dont need to set a color of every pixel "by hand". How do I do this effectively in Java? I want it to be fast. Is there a way to access each pixel directly and change its color? Or is there an option like in asm, to change only the color in a palette? Kind Regards
Advertisement

You have a function to calculate the color based on x.


Paclaw
The people over at http://www.javagaming.org/forums/index.php might be able to answer this. I didn't see any way to alter the palette once it was created using java.awt.image.IndexColorModel.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Hi Jester

I solved it, I think.
Maybe there is a faster solution, but this one is fast enough for me :)

Im using, as you said, an IndexColorModel combined with MemoryImageSource.
I found "MemoryImageSource.newPixels(...)" to update the Image after changing the palette.

private byte[][] _pal = null;private byte[] _pxs = null; private Image _img = null;private IndexColorModel _icm = null;private MemoryImageSource _src = null; public myConstructor() { // Create source-data for image. _pal = new byte[3][128]; _pxs = new byte[800 * 800];  // Create / Update palette colors. updatePalette();  // Fill "_pixels" with palette-indexes. ... // Setup image. _icm = new IndexColorModel(8, 128, _pal[0], _pal[1], _pal[2]); _src = new MemoryImageSource(800, 800, _icm, _pxs, 0, 800); _src.setAnimated(true); _src.setFullBufferUpdates(true);  // Create image. _img = Toolkit.getDefaultToolkit().createImage(_src);} private void updatePalette() { // Update "_pal" colors. ...  // Update image. _src.newPixels(_pxs, new IndexColorModel(8, 128, _pal[0], _pal[1], _pal[2]), 0, 800);} public Image getImage() { return _img;}


Kind Regards

This topic is closed to new replies.

Advertisement