Changing opacity (transparency) of a BufferedImage

Is there a way to change the opacity of a BufferedImage ?Like making it 50% more transparent and such.
[116 byte] By [Sergejavaa] at [2007-10-3 3:50:56]
# 1

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class TransparentImage

{

private JPanel getContent(BufferedImage image)

{

JPanel panel = new JPanel(new GridLayout(1,0));

panel.setBackground(Color.pink);

panel.add(new JLabel(new ImageIcon(image)));

BufferedImage transparent = getImage(image, 0.5f);

panel.add(new JLabel(new ImageIcon(transparent)));

return panel;

}

private BufferedImage getImage(BufferedImage src, float alpha)

{

BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(),

BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = dest.createGraphics();

int rule = AlphaComposite.SRC_OVER;

AlphaComposite ac = AlphaComposite.getInstance(rule, alpha);

g2.setComposite(ac);

g2.drawImage(src, null, 0, 0);

g2.dispose();

return dest;

}

public static void main(String[] args) throws IOException

{

File file = new File("images/cougar.jpg");

BufferedImage bi = ImageIO.read(file);

TransparentImage test = new TransparentImage();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(bi));

f.pack();

f.setLocation(200,200);

f.setVisible(true);

}

}

74philipa at 2007-7-14 21:48:29 > top of Java-index,Security,Cryptography...
# 2
Thanks, Alphacomposite was what I needed.
Sergejavaa at 2007-7-14 21:48:29 > top of Java-index,Security,Cryptography...