轻量级组件中剪辑区域的处理与文档不匹配?

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

我是不是忽略了什么,或者这是规范或实现中的错误?

更新:

请注意,由于对实际问题的误解(我无法回答),此答案正确。我暂时将其打开,也许其他人会发现它有用。


  1. Componentcalls setClip()getGraphics()方法。
  2. 您可能已经知道,通常 paint(Graphics) 仅由负责重新绘制的 RepaintManager 调用。它在 paint().
  3. 之前多次调用 setClip()

因此,据我所知:,文档没有错。如果 RepaintManagerComponent 不会调用 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.

这是我遗漏的一点信息。