[java] Fast color-switching

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites

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

Share on other sites
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.

Share on other sites
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

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632632
• Total Posts
3007524
• Who's Online (See full list)

There are no registered users currently online

×