如何从jlabel更改不透明度?

时间:2016-03-22 01:09:31

标签: jlabel opacity

为了更好地解释它,我想改变包含像%20这样的图像的jlabel的不透明度,这可能吗?

1 个答案:

答案 0 :(得分:0)

好吧,我正在调查,最后我做了,这是我的代码:

public static void main(String[] args){
    String format = "png";

    //We create the image to draw on it.

    Image img= new ImageIcon("Source").getImage();
    BufferedImage bi= new BufferedImage(img.getWidth(null), img.getHeight(null),BufferedImage.TYPE_INT_ARGB);

    // We drawing

    Graphics2D g2D = bi.createGraphics();
    AlphaComposite ac= AlphaComposite.getInstance(AlphaComposite.SRC_OVER,  0.2f);
            g2D.setComposite(ac);
            g2D.drawImage(img, 0, 0,img.getWidth(null),img.getHeight(null),null);
            g2D.dispose();
    // Write the image in the file .
    try {
        ImageIO.write(bi, format, new File("Source"));
    } catch (IOException e) {
        System.out.println("Write Error");

            }
}