java Graphics.clearRect 和 Graphics.drawRect 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5879884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between Graphics.clearRect and Graphics.drawRect?
提问by Bringer128
I found a rendering bug in some code and have found a workaround, but I would like to know why I am getting different behaviour. In the old code, the background would (sometimes) be rendered as white, despite while debugging getBackground()
would return the correct colour.
我在某些代码中发现了一个渲染错误并找到了解决方法,但我想知道为什么我会出现不同的行为。在旧代码中,背景会(有时)呈现为白色,尽管调试时getBackground()
会返回正确的颜色。
Old code:
旧代码:
@Override
public void paint(Graphics g) {
// Stuff
g.setColor(getBackground());
g.clearRect(0, 0, width, height); // Obviously wrong.
// More stuff
}
New code:
新代码:
@Override
public void paint(Graphics g) {
// Stuff
g.setColor(getBackground());
g.drawRect(0, 0, width, height); // Correct usage with 'setColor' call.
// More stuff
}
As I put in the code, it is obvious that setColor(getBackground())
has no effect on the clearRect(...)
call. Yet I would assume that calling clearRect(...)
and calling setColor(getBackground())
followed by drawRect(...)
would be semantically the same.
当我输入代码时,很明显这setColor(getBackground())
对clearRect(...)
调用没有影响。但是我会假设调用clearRect(...)
和调用setColor(getBackground())
后跟drawRect(...)
在语义上是相同的。
I have also considered the opaqueness property, but the parent lightweight components and ancestor heavyweight component all use the same background colour, and it is quite obvious that this component is the one with the incorrect behaviour (it is one of 8 of the same type of component owned by its parent - yet only the ones that get to this section of code have a problem).
我也考虑过 opaqueness 属性,但是父级轻量级组件和祖先级重量级组件都使用相同的背景颜色,很明显这个组件是行为不正确的那个(它是 8 个相同类型的组件之一)由其父级拥有的组件 - 但只有到达这部分代码的组件才有问题)。
I am using JDK 1.6.0_07 (for business reasons of course) if that helps.
如果有帮助,我正在使用 JDK 1.6.0_07(当然出于商业原因)。
回答by spot35
Here's the information from the JavaDocs -
这是来自 JavaDocs 的信息 -
Clears the specified rectangle by filling it with the background color of the current drawing surface. This operation does not use the current paint mode.
Beginning with Java 1.1, the background color of offscreen images may be system dependent. Applications should use setColor followed by fillRect to ensure that an offscreen image is cleared to a specific color.
通过用当前绘图表面的背景颜色填充指定的矩形来清除它。此操作不使用当前的绘制模式。
从 Java 1.1 开始,屏幕外图像的背景颜色可能取决于系统。应用程序应使用 setColor 后跟 fillRect 以确保将屏幕外图像清除为特定颜色。
As this implies, clearRect is system dependent and the value of getBackground() is not taken into account.
这意味着, clearRect 是系统相关的,并且不考虑 getBackground() 的值。
回答by E_X
The difference is this :
不同之处在于:
- if you use the graphics method fillRect() you can't erase the color by using drawRect() over the same recatngle specified in pixels.
- but if you use the graphics method fillRect() then clear it with clearRect() and after that drawRect(); you reach a satisfaction and a conclusion.
- 如果您使用图形方法 fillRect(),则无法通过在以像素为单位指定的相同 recatngle 上使用 drawRect() 来擦除颜色。
- 但是如果你使用图形方法 fillRect() 然后用 clearRect() 和之后的 drawRect() 清除它;你达到满意和结论。