Java Swing-半透明组件 [英] Java Swing - Translucent Components

查看:372
本文介绍了Java Swing-半透明组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个有关半透明组件的问题,这些透明组件导致看似无法正确更新的奇特伪像。我收到的答案导致工件消失了,但要以半透明为代价。

I recently asked a question about translucent components causing odd artifacts from seemingly not updating properly. The answer I received caused the artifacts to go away, but at the cost of translucency.

解决方案是-对于每个半透明组件-也称为setOpaque(false)功能。这样,Swing知道需要重新绘制这些组件后面的背景。

The solution was to- for every translucent component- also call the setOpaque(false) function. This way, Swing knew that it needed to redraw the background behind those components.

但是,这是以我试图实现的半透明为代价的。

However, this came at the cost of the translucency that I was trying to achieve. It caused the components to become transparent instead.

前提是:我正在为聊天客户端设计GUI,并且功能请求是具有背景。通过遵循扩展JPanel类的代码段,我成功地完成了背景工作,但是随后我希望这些组件能够显示背景。设置其透明度后,会在不应该显示的位置显示已更新组件的残余。我来到这里解决了我的问题,但是现在我遇到了一个新问题。因此,我们就在这里。

The premise is this: I am designing the GUI for a chat client, and a feature request was to have a background. I successfully got the background working by following a code snippet for extending the JPanel class, but then I wanted the components to allow the background to show. After setting their translucency, remnants of updated components were being displayed where they shouldn't have been. I came here and got my problem solved, but now I've got a new problem. So here we are.

所以,这是我的猜测:

-调用setOpaque( false)函数来设置每个所需的组件,并且不设置半透明颜色不能实现我想要的效果。

-Calling the setOpaque(false) function for each desired component and NOT setting a translucent color does not achieve what I want.

-设置半透明颜色而不调用setOpaque(false)允许半透明

-Setting a translucent color and NOT calling setOpaque(false) allows the translucent background to show, but causes artifacts, putting me back at square one.

所以我需要一些透明的中间没有假象,而半透明的中间有假象。即,我想要一个没有伪像的半透明背景(不是完全透明)。

So I need some middle ground between transparent with no artifacts, and translucent with artifacts. Namely, I want a translucent background (not completely transparent) that has no artifacts.

似乎我需要重写JFrame使其重新绘制所有组件,无论不透明。除非有什么我想念的..这就是为什么我在这里!

It seems like I'm going to need to override the JFrame to cause it to repaint all its components, regardless of the opacity. Unless there's something I'm missing.. which is why I'm here!

谢谢!

(这是原始问题的链接,并提供图片供参考: Java Swing-半透明组件导致伪像

(Here's a link to the original question, with a picture for reference: Java Swing - Translucent Components causing Artifacts)

推荐答案

一种选择是覆盖组件并自己绘制背景:

One option would be to override the components and draw the background yourself:

class TranslucentLabel extends JLabel {
    public TranslucentLabel(String text) {
        super(text);
        setOpaque(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(255, 0, 0, 64));
        Insets insets = getInsets();
        g.fillRect(insets.left, insets.top, 
                getWidth() - insets.left - insets.right, 
                getHeight() - insets.top - insets.bottom);
        super.paintComponent(g);
    }
}

编辑:或者,您可以绘制半透明的背景色将子组件直接放到面板上,则不必重写组件:

Alternatively you could draw the translucent background colour for the child components directly onto the panel, then you would not have to override components:

class YourPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g.create();

        // Draw your background image here to g2d.

        g2d.setColor(new Color(255, 0, 0, 64));
        Area area = new Area();
        for (Component component : getComponents()) {
            area.add(new Area(component.getBounds()));
        }
        g2d.fill(area);
        g2d.dispose();
    }
}

此方法有一个缺点。如果组件中有真正透明的部分(例如圆形边框),则其整个背景将被着色。

There is a disadvantage to this approach. If there is a genuinely transparent part of a component (such as a rounded border), then its entire background will be coloured.

这篇关于Java Swing-半透明组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆