Can I display an alpha image from left to right in java?

I am making a game and I want one image to "disappear" from left to right with the left side of the image having alpha 1.0 and alpha 0.0 on the right. (note: I do not want him to change how he looks with time, for example, attenuation or output, but just disappearing from left to right and constant). Trying to do what I want the end result to look like this:

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 densities "l" represent alpha

I am currently using TYPE_INT_RGB Buffered Images and would like to keep this if possible.

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


EDIT: I don't want to have an opaque border. I want to draw one BufferedImage (with alpha gradient) on another BufferedImage.

0
source share
1 answer

The main idea is to apply a mask AlphaCompositeto the original image that was filled withLinearGradientPaint

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

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

Then we create a masking image of the same size ...

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

Then we fill in the masking image with 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 is important here, we do not really care about physical color, only its alpha property, as this will determine how both images are masked together ...

Then we apply the mask ...

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

What 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;

}

" " AlphaComposite , ...

( , )

Alpha

RED

enter image description here

+5

Source: https://habr.com/ru/post/1669123/


All Articles