Sign in to follow this  
CodeMachine

[java] Fast color-switching

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this