AWT 组件上的半透明面板

发布于 2024-10-15 20:30:55 字数 197 浏览 2 评论 0原文

我有一个 AWT 组件(第 3 方库),我需要通过在其上显示半透明面板(在本例中 alpha 为 128)来遮盖它。有什么办法可以做到这一点吗?我查看了使用 Sun 的 AWTUtilities 类的“TransparentPanel”类,但它有一个限制,即无法显示 0 < 的像素。阿尔法< 255. 即使这不是一个很好的解决方案,我只是在寻找一些方法来做到这一点。

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). 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-10-22 20:30:55

也许带有半透明涂料的 GlassPane 可以解决这个问题。这是一个简单的例子:

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);
     }
}

Maybe a GlassPane with translucent paint can solve this. 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);
     }
}
梦幻之岛 2024-10-22 20:30:55

据我所知,你不能这样做。更接近的是创建 AWT 组件的屏幕捕获,同时在上面显示 swing 组件,并最终不时刷新屏幕捕获。这意味着本机组件并不真正存在,并且在屏幕捕获模式下无法使用(不响应鼠标单击和按键事件)。

这是 DJ NativeSwing 示例之一在嵌入式 Web 浏览器上覆盖具有 alpha 透明度的 Swing PNG 图像的做法。查看演示:http://djproject.sourceforge.net/ns

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. 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. Check the demo: http://djproject.sourceforge.net/ns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文