轻量级组件中剪辑区域的处理与文档不匹配?
Handling of clip area in lightweight components does not match documentation?
java.awt.Graphics
的文档说明如下(我添加了一些 强调):
All rendering operations modify only pixels which lie within the area
bounded by the current clip, which is specified by a Shape
in user
space and is controlled by the program using the Graphics
object. This
user clip is transformed into device space and combined with the
device clip, which is defined by the visibility of windows and device
extents. The combination of the user clip and device clip defines the
composite clip, which determines the final clipping region. The user
clip cannot be modified by the rendering system to reflect the
resulting composite clip. The user clip can only be changed through
the setClip
or clipRect
methods.
此外,getClip()
方法的文档说:
Gets the current clipping area. This method returns the user clip,
which is independent of the clipping associated with device bounds and
window visibility. If no clip has previously been set, or if the clip
has been cleared using setClip(null)
, this method returns null
.
但是,下面的示例程序打印了一个非空裁剪区域:
import java.awt.*;
public class ClipTest
{
public static void main(String args[])
{
Frame f = new Frame();
f.setLayout(new BorderLayout());
f.setSize(300,200);
Component lightweight = new Component() {
public void paint(Graphics g)
{
System.out.println(g.getClip());
}
};
f.add(lightweight);
f.setVisible(true);
}
}
我是不是忽略了什么,或者这是规范或实现中的错误?
更新:
- 上面的代码没有使用Swing。它使用纯 AWT。我标记了问题 'swing' 因为这个 也 发生在 Swing 中,但它不是特定于 Swing 的。
- 此外,我不是在问 "why" 发生这种情况。我研究了源代码,我可以看到在某些情况下内部调用了 setClip()。但这似乎与 Javadocs 所说的不符(请参阅上面引用的部分)。因此我的问题是:这是规范中 或 实现中的错误,还是我只是遗漏了什么?
请注意,由于对实际问题的误解(我无法回答),此答案不正确。我暂时将其打开,也许其他人会发现它有用。
Component
calls setClip()
的getGraphics()
方法。
- 您可能已经知道,通常
paint(Graphics)
仅由负责重新绘制的 RepaintManager
调用。它在 paint()
. 之前多次调用 setClip()
因此,据我所知:不,文档没有错。如果 RepaintManager
或 Component
不会调用 setClip()
,那么 getClip()
确实会 return null
。您可以查看 RepaintManager
here.
的完整源代码
好的,答案似乎在 java.awt.Component.paint(Graphics)
的文档中,它说:
This method is called when the contents of the component should be
painted; such as when the component is first being shown or is damaged
and in need of repair. The clip rectangle in the Graphics
parameter is
set to the area which needs to be painted.
这是我遗漏的一点信息。
java.awt.Graphics
的文档说明如下(我添加了一些 强调):
All rendering operations modify only pixels which lie within the area bounded by the current clip, which is specified by a
Shape
in user space and is controlled by the program using theGraphics
object. This user clip is transformed into device space and combined with the device clip, which is defined by the visibility of windows and device extents. The combination of the user clip and device clip defines the composite clip, which determines the final clipping region. The user clip cannot be modified by the rendering system to reflect the resulting composite clip. The user clip can only be changed through thesetClip
orclipRect
methods.
此外,getClip()
方法的文档说:
Gets the current clipping area. This method returns the user clip, which is independent of the clipping associated with device bounds and window visibility. If no clip has previously been set, or if the clip has been cleared using
setClip(null)
, this method returnsnull
.
但是,下面的示例程序打印了一个非空裁剪区域:
import java.awt.*;
public class ClipTest
{
public static void main(String args[])
{
Frame f = new Frame();
f.setLayout(new BorderLayout());
f.setSize(300,200);
Component lightweight = new Component() {
public void paint(Graphics g)
{
System.out.println(g.getClip());
}
};
f.add(lightweight);
f.setVisible(true);
}
}
我是不是忽略了什么,或者这是规范或实现中的错误?
更新:
- 上面的代码没有使用Swing。它使用纯 AWT。我标记了问题 'swing' 因为这个 也 发生在 Swing 中,但它不是特定于 Swing 的。
- 此外,我不是在问 "why" 发生这种情况。我研究了源代码,我可以看到在某些情况下内部调用了 setClip()。但这似乎与 Javadocs 所说的不符(请参阅上面引用的部分)。因此我的问题是:这是规范中 或 实现中的错误,还是我只是遗漏了什么?
请注意,由于对实际问题的误解(我无法回答),此答案不正确。我暂时将其打开,也许其他人会发现它有用。
Component
callssetClip()
的getGraphics()
方法。- 您可能已经知道,通常
paint(Graphics)
仅由负责重新绘制的RepaintManager
调用。它在paint()
. 之前多次调用
setClip()
因此,据我所知:不,文档没有错。如果 RepaintManager
或 Component
不会调用 setClip()
,那么 getClip()
确实会 return null
。您可以查看 RepaintManager
here.
好的,答案似乎在 java.awt.Component.paint(Graphics)
的文档中,它说:
This method is called when the contents of the component should be painted; such as when the component is first being shown or is damaged and in need of repair. The clip rectangle in the
Graphics
parameter is set to the area which needs to be painted.
这是我遗漏的一点信息。