简体   繁体   English

JPanel内部的半透明组件

[英]Translucent components inside JPanel

I have class MyPanel that extends from JPanel. 我有从Myanel扩展的类MyPanel。 MyPanel class has JLabel component which holds an icon. MyPanel类具有JLabel组件,其中包含一个图标。

My question is how can i paint/render this JLabel component to get translucent effect (see through icon) inside MyPanel class (not create xxxJLabel extends JLabel class and override paintComponents method). 我的问题是如何在MyPanel类中绘制/渲染此JLabel组件以获得半透明效果(请参阅图标)(不创建xxxJLabel扩展JLabel类并覆盖paintComponents方法)。

Thank you 谢谢

One way is to provide a translucent image to the JLabel . 一种方法是向JLabel提供半透明图像。 That might be done with a standard label, before setIcon() or similar is called, or alternately by extending JLabel and overriding the setIcon() method to do the same. 这可以在setIcon()或类似函数之前使用标准标签完成,或者通过扩展JLabel并覆盖setIcon()方法来执行相同操作。

EG of 2nd technique 第二种技术的EG

在此输入图像描述

Code

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class TransparentIcon {
    public static void main(String[] args) throws Exception {
        String imgURL =
            "http://www.gravatar.com/avatar/" +
            "a1ab0af4997654345d7a949877f8037e";
        final BufferedImage image = ImageIO.read(new URL(imgURL));
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ImageIcon icon = new ImageIcon(image);

                JPanel  p = new JPanel(new GridLayout(2,3));
                for (int ii=0; ii<6; ii++) {
                    TransparentLabel tl = new TransparentLabel();
                    tl.setOpacity((ii+1)/6f);
                    tl.setIcon(icon);
                    p.add(tl);
                }

                JOptionPane.showMessageDialog(null, p);
            }
        });
    }
}

class TransparentLabel extends JLabel {

    float opacity = 1f;

    public void setOpacity(float opacity) {
        this.opacity = opacity;
    }

    private Icon getTranslucentIcon(Icon icon) {

        if (icon!=null) {
            BufferedImage bi = new BufferedImage(
                icon.getIconWidth(),
                icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            AlphaComposite ac = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER,
                opacity);
            g.setComposite(ac);
            icon.paintIcon(this,g,0,0);
            g.dispose();

            return new ImageIcon(bi);
        } else {
            return null;
        }
    }

    public void setIcon(Icon icon) {
        super.setIcon( getTranslucentIcon(icon) );
    }
}

Update 更新

I just wonder how it can be done if i get Graphics of JLabel inside MyPanel class and change its visual appearance? 我只是想知道如果我在MyPanel类中获得了JLabel的图形并改变其视觉外观,它是如何完成的?

LabelRenderTest.java renders a JLabel to a BufferedImage so that it can be used for custom rendering inside the paintComponent(Graphics) method. LabelRenderTest.javaJLabel呈现给BufferedImage以便它可以用于paintComponent(Graphics)方法内的自定义呈现。

Note though: I don't quite understand what the advantage of the JLabel is in your use-case. 请注意:我不太了解JLabel在您的用例中的优势。 I was using it in that example in order to render HTML. 我在该示例中使用它来呈现HTML。 If I only had an image, I'd use the image directly (eg Graphics.drawImage(Image,int,int,ImageObserver) ) and never create the label. 如果我只有一个图像,我会直接使用图像(例如Graphics.drawImage(Image,int,int,ImageObserver) )并且永远不会创建标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM