setOpaque(真/假);爪哇
在 Java2D 中,当您使用 setOpaque 时,我对 true 和 false 的作用有点困惑。
例如,我知道在 Swing 中不透明意味着在绘制 Swing 时不会绘制组件后面的内容。或者这是倒退?是哪一个?
谢谢
In Java2D when you use setOpaque I am a little confused on what the true and false does.
For example I know that in Swing Opaque means that when painting Swing wont paint what is behind the component. Or is this backwards? Which one is it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对你的问题的简短回答是“不透明”在英语中被定义为完全不透明。因此,不透明组件是绘制整个矩形的组件,并且每个像素都不是半透明的。
然而,Swing 组件不透明 API 是那些设计错误并因此经常被误用的 API 之一。
需要理解的重要一点是,
isOpaque
是 Swing 系统和特定组件之间的契约。如果返回 true,则组件保证以非半透明方式绘制其矩形区域的每个像素。这个 API应该是抽象的,以迫使所有组件编写者考虑它。 Swing 的绘制系统使用isOpaque
API 来确定是否必须绘制给定组件所覆盖的区域对于与其重叠和位于其后面的组件< /strong>,包括组件的容器和祖先。如果组件对此 API 返回 true,则 Swing 系统可能会优化绘制,以便在调用特定组件的绘制方法之前不会绘制该区域中的任何内容。由于
isOpaque
的合同含义,APIsetOpaque
不应该存在,因为任何外部调用setOpaque< 实际上都是不正确的/code> 因为反过来,外部事物无法知道相关组件是否会(甚至可以)遵守它。相反,isOpaque 应该被每个具体组件覆盖,以返回它是否实际上是不透明的(考虑到其当前属性)。
因为
setOpaque
API 确实存在,许多组件错误地实现了它(这是可以理解的)来驱动它们是否绘制“背景”(例如 JLabel 和 JPanel)填充其背景颜色)。这样做的效果是给 API 用户留下这样的印象:setOpaque
驱动是否应该绘制背景,但事实并非如此。此外,如果您希望绘制具有半透明背景的 JLabel,则需要使用 alpha 值设置背景颜色,并执行
setOpaque(true)
,但它实际上并不是不透明的 - 它是 < em>半透明;为了使组件正确渲染,其后面的组件仍然需要绘制。这个问题在 Java 6 的新 Nimbus Look & 中得到了显着的暴露。感觉。有许多关于针对 Nimbus 提交的透明组件的错误报告(请参阅堆栈溢出问题 Java Nimbus带有透明文本字段的 LAF)。 Nimbus开发团队的回应是这样的:
因此,总而言之,您不应该使用
setOpaque
。如果您确实使用它,请记住一些外观和功能的组合。感觉和某些组件可能会做“令人惊讶”的事情。而且,到最后,实际上并没有正确的答案。The short answer to your question is that "opaque" is defined in English as completely non-transparent. Therefore an opaque component is one which paints its entire rectangle, and every pixel is not at all translucent to any degree.
However, the Swing component opacity API is one of those mis-designed and therefore often mis-used APIs.
What's important to understand is that
isOpaque
is a contract between the Swing system and a particular component. If it returns true, the component guarantees to non-translucently paint every pixel of its rectangular area. This API should have been abstract to force all component writers to consider it. TheisOpaque
API is used by Swing's painting system to determine whether the area covered by a given component must be painted for components which overlap it and which are behind it, including the component's container and ancestors. If a component returns true to this API, the Swing system may optimize painting to not paint anything in that area until invoking the specific component's paint method.Because of contractual implication of
isOpaque
, the APIsetOpaque
should not exist, since it is actually incorrect for anything external to callsetOpaque
since, in turn, the external thing can't know whether the component in question will (or even can) honor it. Instead, isOpaque should have been overridden by each concrete component to return whether it actually is, in fact, opaque given its current properties.Because the
setOpaque
API does exist, many components have mis-implemented it (quite understandably) to drive whether or not they will paint their "background" (for example JLabel and JPanel filling with their background color). The effect of this is to create an impression with users of the API to think thatsetOpaque
drives whether or not that background should paint, but it doesn't.Furthermore, if, say, you wish to paint a JLabel with a translucent background you need to set a background color with an alpha value, and do
setOpaque(true)
, but it's not actually opaque - it's translucent; the components behind it still need to paint in order for the component to render properly.This problem was exposed in a significant way with the Java 6's new Nimbus Look & Feel. There are numerous bug reports regarding transparent components filed against Nimbus (see stack overflow question Java Nimbus LAF with transparent text fields). The response of the Nimbus development team is this:
So, in summary, you should not use
setOpaque
. If you do use it, bear in mind that the combination of some Look & Feels and some components may do "surprising" things. And, in the end, there is actually no right answer.javadoc说:如果为 true,则组件将绘制其边界内的每个像素。否则,组件可能无法绘制其部分或全部像素,允许底层像素显示出来。
也尝试一下这个示例程序...
http://www.java2s.com/Code/JavaAPI/javax。 swing/JPanelsetOpaquebooleanisOpaque.htm
javadoc says : If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.
try this example program too...
http://www.java2s.com/Code/JavaAPI/javax.swing/JPanelsetOpaquebooleanisOpaque.htm
我认为还需要添加以下内容:
术语“opaque”在 Java 2D 和 Swing 中具有不同的含义。
在 Java 2D 中,不透明度是一个渲染概念。它是一个组合
alpha 值和
Composite
模式。这是一个学位正在绘制的像素颜色应与像素混合
值已经存在。比如我们画一个半透明的
现有椭圆形上的矩形。因此椭圆形是
部分可见。这个概念经常被比作光
通过玻璃或水。
在 Swing 中,不透明组件绘制其内部的每个像素
矩形边界。非不透明组件仅绘制一部分
它的像素或根本没有,允许其下面的像素
显示通过。设置不透明属性是出于效率原因;摇摆
不必绘制不透明组件后面的区域。
来源:Java 文档和 Filthy Rich Clients
在代码示例中,我们有两个组件。左侧的组件是一个面板,它使用 AlphaComposite 在椭圆形上绘制高度半透明的矩形。右侧的组件是标签。标签在大多数外观和感觉上都是透明的。我们重写标签的 isOpaque() 方法来设置灰色背景。
I think that the following also needs to be added:
The term opaque has different meanings in Java 2D and in Swing.
In Java 2D opacity is a rendering concept. It is a combination
of an alpha value and the
Composite
mode. It is a degree towhich the pixel colours being drawn should be blended with pixel
values already present. For instance, we draw a semi-transparent
rectangle over an existing oval shape. The oval is therefore
partially visible. This concept is often compared to light
going trough glass or water.
In Swing, an opaque component paints every pixel within its
rectangular bounds. A non-opaque component paints only a subset of
its pixels or none at all, allowing the pixels underneath it to
show through. The opaque property was set for efficiency reasons; Swing
does not have to paint areas behind opaque components.
Source: Java docs and Filthy Rich Clients
In the code example, we have two components. The component on the left is a panel which uses
AlphaComposite
to paint a highly translucent rectangle over an oval. The component on the right is a label. Labels are non-opaque in most look and feels. We overwrite the label'sisOpaque()
method to set a gray background.