使用Alpha通道在PNG上叠加透明色

我正在寻找一种用Java绘制PNG图像(带有alpha通道)的方法,该图像是灰度颜色,然后覆盖透明颜色,比如绿色,75%不透明度,在它上面改变颜色。 要清楚,所得到的图像将是在其顶部放置透明色的直接结果,并且不会是任何特殊类型的混合的结果。

例如,我会绘制以下图像:

然后在图像顶部叠加RGB颜色(102,255,0,alpha):

在图像上绘制颜色的方法需要使得它不会干扰屏幕上的其他图像。 这与Adobe Photoshop的颜色叠加function类似。 两个图像可以具有单独的颜色叠加,但是单独的叠加不会相互冲突。

感谢leonbloy,我相信使用“SRC_OVER”的AlphaComposit可以解决这个问题,然后我可以使用一种方法将结果保存为新的BufferedImage,以防止叠加影响屏幕上的任何其他图像对象。

我会在测试时发布结果。

我能够使用AlphaComposite,Graphics2D和BufferedImage来获得所需的效果。

@Override public void paintComponent(Graphics g) { BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D gbi = result.createGraphics(); BufferedImage x = null; try { x = ImageIO.read(getClass().getResource("/resources/someimage.png")); } catch (IOException ex) { Logger.getLogger(CanvasPanel.class.getName()).log(Level.SEVERE, null, ex); } gbi.drawImage(x, 0, 0, this); gbi.setColor(selectedColor); gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.75f)); gbi.fillRect(0, 0, width, height); g.drawImage(result, 0, 0, this); }