问题描述
我在Java中使用setClip遇到麻烦.我有一个扩展JPanel的类.在该类中,我重写了paintComponent方法.我的paintComponent方法看起来像这样:
I'm having some troubles with setClip in Java. I have a class that extends JPanel. Within that class I have overridden the paintComponent method. My paintComponent method looks something like this:
paintComponent {
//draw some lines here
Rectangle whole = g2.getClipBounds();//g2 is my Graphics2D object
Rectangle part = <some rectangle that is a part of the whole paintable area>;
g2.setClip(part);
//draw some more stuff here
g2.setClip(whole);
}
我看到的问题是剪切区域中的区域似乎被重复绘制.例如,如果我告诉它要绘画,它就可以绘画.但是然后,如果我切换窗口或以其他方式使它再次绘制相同的内容,则不会清除剪切区域,而其余部分则不会清除.这样一来,在裁剪区域上的绘画将比其余可绘画区域更大胆.
The problem that I'm seeing is that the area in the clipped region seems to be painted repeatedly. For example, if I tell it to paint, it paints just fine. But then, if I switch windows or somehow else cause it to paint the same thing again, the clipped region isn't cleared while the rest is. This results in the painting on the clipped region to appear bolder than the rest of the paintable area.
我想我在setClip的工作方式中缺少了一些东西.
I imagine that I'm missing something in how setClip works.
任何建议将不胜感激.预先感谢您的帮助.
Any suggestions would be much appreciated. Thanks in advance for any help.
推荐答案
按照汤姆的建议,从旧对象创建新的Graphics对象对我来说很成功.
Creating a new Graphics object from the old one did the trick for me, as adviced by Tom.
这篇关于Java setClip似乎会重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!