Java setClip seems to redraw

不想你离开。 提交于 2019-12-11 12:36:28

问题


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.

I imagine that I'm missing something in how setClip works.

Any suggestions would be much appreciated. Thanks in advance for any help.


回答1:


Creating a new Graphics object from the old one did the trick for me, as adviced by Tom.



来源:https://stackoverflow.com/questions/1527282/java-setclip-seems-to-redraw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!