将半透明图像添加到JLayeredPane

时间:2017-01-02 05:18:06

标签: java image swing transparent layer

所以我正在制作一个stopmotion应用程序 - 我的网络摄像头的实时源是在JaayeredPane顶部(第10层)内的JPanel(带有setOpaque(false)) - 基本上,当我拍照时,我想将它添加到其中一个较低层,以便您在屏幕上显示以前拍摄的图片。以下是我现在尝试这样做的方法:

编辑:这是基于以下答案的代码 - 现在什么都不做,而不是像以前一样添加不透明图像 - 如果我将它添加到JPanel中,将JPanel添加到JLayeredPane,然后我得到的只是灰色

BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));              

g.drawImage(img2, 0, 0, null);
g.dispose(); 

ImageIcon imgIcon = new ImageIcon(new ImageIcon(img2).getImage().getScaledInstance(img2.getWidth(), img2.getHeight(), Image.SCALE_SMOOTH));
JLabel showPic = new JLabel(imgIcon);
showPic.setSize(layers.getSize());
showPic.setBounds(layers.getX() + 18, layers.getY(), img2.getWidth(), img2.getHeight());

layers.add(showPic, new Integer(1)); //layers is my JLayeredPane
layers.repaint();
layers.revalidate();

img是我刚从网络摄像头捕获的图片,我正在尝试将其设置为半透明,然后将其添加到JLabel。我怎样才能做到这一点?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

我不知道这是否能解决问题,但您正在寻找的图像是

<div class="East">
    <table class="exception_table">
        <tr>
            <td>DMM NAME</td>
            <td class="tdHideShow Bob">Bob</td>
            <td class="tdHideShow Bob">Bob</td>
            <td class= "tdHideShow Billy">Billy</td>

        </tr>
        <tr>
            <td>Sales</td>
            <td>$1</td>
            <td>$5</td>
            <td>$10</td>
        </tr>
        <tr>
            <td>Tenure</td>
            <td>2 years</td>
            <td>2 years</td>
            <td>1 year</td>
        </tr>


    </table>
    <table class="exception_table">
        <tr>
            <td>DMM NAME</td>
            <td class="tdHideShow Paul">Pal</td>
            <td class="tdHideShow Doug">Doug</td>
            <td class= "tdHideShow Joe">Joe</td>

        </tr>
        <tr>
            <td>Sales</td>
            <td>$5</td>
            <td>$6</td>
            <td>$70</td>
        </tr>
        <tr>
            <td>Tenure</td>
            <td>7 years</td>
            <td>9 years</td>
            <td>2 years </td>
        </tr>


    </table>
        <table class="exception_table">
                <tr>
                    <td>DMM NAME</td>
                    <td class="tdHideShow Tim">Tim</td>
                    <td class="tdHideShow Tim">Tim</td>
                    <td class= "tdHideShow Tim">Tim</td>

                </tr>
                <tr>
                    <td>Sales</td>
                    <td>$1</td>
                    <td>$5</td>
                    <td>$10</td>
                </tr>
                <tr>
                    <td>Tenure</td>
                    <td>1 years</td>
                    <td>1 years</td>
                    <td>1 years</td>
                </tr>


    </table>

</div>

</html>

java.awt.Transparency.TRANSLUCENT的值为3,对应于BufferedImage.TYPE_INT_ARGB_PRE,只有上帝才知道它在做什么。

使用BufferedImage.TYPE_INT_ARGB,您可以创建透明图像并应用复合材料等

您还需要更正此

BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

g.drawImage(img2, null, 0, 0);

-

继承人透明度如何对我有用:

我有两个图像bim和bim2,我在另一个上面绘制一个:

g.drawImage(img2, 0, 0, null);

然后我可以在JPanel Jframe或其他任何地方显示它。

我甚至可以创建标签:

BufferedImage bim=null, bim2=null;
try {
  bim=ImageIO.read(new File("...."));
  bim2=ImageIO.read(new File("...."));
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
int wc=bim.getWidth(), hc=bim.getHeight();

BufferedImage img2 = new BufferedImage(bim.getWidth(), bim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));              

g.drawImage(bim, 0, 0, null);
g.drawImage(bim2, 0, 0, wc, hc, null);