Java Graphics2D method for drawing an image where the pixel alpha values are used but the colour values are replaced with a given colour

NickD :

I’ve been reading the API for Graphics2D and have seen examples of all the available composite modes (that are similar to photoshop blend modes) but I can’t see a way to draw a source image to a target buffered image In a colour that I have specified, for example my source image is a white opaque circle on a fully transparent background, how do I draw using this to a buffer so a coloured circle is drawn.

I would prefer not to construct an intermediate image for performance reasons, is this possible with the api?

EDIT: I have added an image that hopefully helps to show the operation I am trying to describe. This is a common way to draw sprites in open GL etc and I am just wondering how to use the Graphics2D API to do the same thing.

enter image description here

Mark Jeronimus :

Is is possible using the API but you have to write your own ImageProducer subclass similar to FilteredImageSource but with two input images instead of one. But because of that the end result will require more lines of code than a manual implementation and won't be any more efficient. Alternatively you can use the existing FilteredImageSource and write an ImageFilter subclass that wraps the 2nd image and does the hard work.

Poke me if you decide you want to go with any of these routes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=146483&siteId=1