简体   繁体   English

AWT组件上的半透明面板

[英]Translucent panel over AWT component

I have an AWT component (3rd party library), and I need to mask it by showing a translucent panel over it (alpha of 128 in this case). 我有一个AWT组件(第三方库),我需要通过在其上显示一个半透明面板(在这种情况下为128,alpha)来掩盖它。 Is there any way at all to do this? 有没有办法做到这一点? I looked at the "TransparentPanel" class that uses Sun's AWTUtilities class, but that has a limitation of not being able to show pixels with 0 < alpha < 255. Even if it's not a pretty solution, I'm just looking for some way to do this. 我看了使用Sun的AWTUtilities类的“ TransparentPanel”类,但是它的局限性是无法显示0 <alpha <255的像素。即使这不是一个很好的解决方案,我也只是在寻找某种方式做这个。

Maybe a GlassPane with translucent paint can solve this. 也许带有半透明涂料的GlassPane可以解决此问题。 Here's a simple example: 这是一个简单的例子:

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GlassFrame extends JComponent
{
    public GlassFrame()
    {
        super();
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g.create();

        Paint p = new GradientPaint(
                0, 0, new Color(200,180,180,200), //Select RGB and Alpha values
                getWidth(), 0, new Color(0,0,0,0)
            );
        g2.setPaint(p);
        g2.fillRect(0, 0, getWidth(), getHeight());

        g2.dispose();
    }

    public static void main(String args[])
    {
        JFrame jf = new JFrame("Simple test");
        jf.add(new JPanel());
        GlassFrame g = new GlassFrame();

        jf.setSize(300,300);
        jf.setVisible(true);

        jf.setGlassPane(g);
        g.setVisible(true);
     }
}

As far as I know, you cannot do this. 据我所知,您不能这样做。 What comes closer is to create a screen capture of the AWT component while a swing component is to be shown above, and eventually refresh the screen capture now and then. 更接近的是创建一个AWT组件的屏幕截图,同时在上方显示一个swing组件,并最终不时刷新屏幕截图。 That means the native component is not really there and cannot be used while in screen capture mode (does not respond to mouse clicks and key events). 这意味着本地组件并不真正存在,并且不能在屏幕捕获模式下使用(不响应鼠标单击和按键事件)。

This is what one of DJ NativeSwing example does to overlay a Swing PNG image with alpha transparency above an embedded web browser. 这是DJ NativeSwing示例的一个操作,该操作将具有alpha透明性的Swing PNG图像叠加在嵌入式Web浏览器上方。 Check the demo: http://djproject.sourceforge.net/ns 检查演示: http : //djproject.sourceforge.net/ns

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

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