java读取图片 去除alpha,我可以在java中从左到右进行图像alpha淡化吗?

本文介绍如何在Java中创建一个从左到右具有渐变透明度的图像。通过应用AlphaComposite的DST_IN模式,将一个带有线性渐变填充的遮罩图像与原始图像结合,达到从完全不透明到完全透明的过渡效果。
摘要由CSDN通过智能技术生成

I am making a game and want to have a single image 'fade' from left to right with the left part of the image having an alpha of 1.0 and the right having an alpha of 0.0. (note: I do not want it to be changing what it looks like over time, like fading in or out, but just fading from left to right and staying constant). An attempt to draw what I want the end result to look like is below:

lll lll ll ll l l l l l

lll lll ll ll l l l l l

lll lll ll ll l l l l l

lll lll ll ll l l l l l

lll lll ll ll l l l l l

lll lll ll ll l l l l l

Where the densities of the 'l's represent the alpha

I am currently using Buffered Images of TYPE_INT_RGB, and would like to keep that the same if possible.

Is there any built in java classes that can help me do this, or at least a (relatively easy) way to do this myself that I just can't figure out?

EDIT:

I don't want to have an opaque frame of any sort. I want to draw one BufferedImage (with alpha gradient) onto another BufferedImage.

解决方案

The basic idea is to apply a AlphaComposite mask over the original image which has been filled with a LinearGradientPaint

So, we start by loading the original image...

BufferedImage original = ImageIO.read(new File("/an/image/somewhere"));

We then create a masking image of the same size...

BufferedImage alphaMask = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);

We then fill the masking image with a LinearGradientPaint...

Graphics2D g2d = alphaMask.createGraphics();

LinearGradientPaint lgp = new LinearGradientPaint(

new Point(0, 0),

new Point(alphaMask.getWidth(), 0),

new float[]{0, 1},

new Color[]{new Color(0, 0, 0, 255), new Color(0, 0, 0 , 0)});

g2d.setPaint(lgp);

g2d.fillRect(0, 0, alphaMask.getWidth(), alphaMask.getHeight());

g2d.dispose();

What's important here is, we don't actually care about the physical color, only it's alpha property, as this will determine how the two images are masked together...

Then, we apply the mask...

BufferedImage faded = applyMask(original, alphaMask, AlphaComposite.DST_IN);

Which actually calls this utility method...

public static BufferedImage applyMask(BufferedImage sourceImage, BufferedImage maskImage, int method) {

BufferedImage maskedImage = null;

if (sourceImage != null) {

int width = maskImage.getWidth();

int height = maskImage.getHeight();

maskedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D mg = maskedImage.createGraphics();

int x = (width - sourceImage.getWidth()) / 2;

int y = (height - sourceImage.getHeight()) / 2;

mg.drawImage(sourceImage, x, y, null);

mg.setComposite(AlphaComposite.getInstance(method));

mg.drawImage(maskImage, 0, 0, null);

mg.dispose();

}

return maskedImage;

}

This basically uses a "destination in" AlphaComposite to apply the mask onto the original image, which results in...

(original on the left, alpha on the right)

dnr2Z.jpg

And just to proof the point, I changed the background color of the frame's content pane to RED

7CDlg.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值