• Advertisement
Sign in to follow this  

[java] Fast color-switching

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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.

// 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);

// 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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement