gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:54:17 25 4
gpt4 key购买 nike

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 的。
  • 此外,我并不是在问“为什么”会发生这种情况。我研究了源代码,我可以看到在某些情况下内部调用了 setClip()。但这似乎与 Javadocs 所说的不符(请参阅上面引用的部分)。因此我的问题是:这是规范中实现中的错误,还是我只是遗漏了什么?

最佳答案

好的,答案似乎在 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 - 轻量级组件中剪辑区域的处理与文档不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34335231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com