英文:
JPanel paint method misbehaves when obscured by menu items or tool tips
问题
基本上,当一个 JPanel
调用了 paintComponent
方法并应用了 AffineTransform
,而同时又被诸如 JMenuItem
和 JToolTip
实例这样的其他组件遮挡时,会对它产生某种变换效果,从而导致畸变(创建未绘制的间隙)。
可以通过以下方式实现这种效果:
- 使用
JSplitPane
来存储面板。 - 创建一个动画器(我使用了线程方法),不断重绘这个面板。
- 覆盖
paintComponent
方法并在其中使用setTransform( new_transform )
。 - 添加带有足够长的工具提示的
JMenuBar
,以遮挡该面板。
英文:
Basically, when a JPanel
whose paintComponent
method overridden and applied AffineTransform
, obscured by other components like JMenuItem
and JToolTip
instances, some kind of transformation applies to it and causing distortion (creating gaps that do not painted).
This effect can be achieved by:
- using
JSplitPane
to store panel - creating an animator (I've used Thread method) that constantly repaints this panel
- Overriding the
paintComponent
method and usingsetTransform( new_transform )
inside it. - Adding
JMenuBar
with tool tips that long enough to obscure the panel.
答案1
得分: 1
Cause of the problem: 摆动组件使用自己的图形对象,并对其应用了 AffineTransform。每当我们使用 graphics.setTransform( new_one )
时,这将擦除先前的变换,有时可能会创建失真。这种方法只应在恢复原始变换时使用!(来源)
解决方案: 与其直接应用变换,不如逐步进行,就像这样:
AffineTransform af = graphics.getTransform();
af.translate(yours.getTranslateX(), yours.getTranslateY());
af.scale(..);
... // 如果您使用其他变换,如旋转、倾斜等,请将它们也添加进来
graphics.setTransform(af);
注意:始终恢复原始变换。最好的方法是创建图形对象的副本,然后在使用后将其销毁。
英文:
Cause of the problem: Swing components uses their own graphics object that has AffineTransform applied to it. Whenever we used graphics.setTransform( new_one )
this will erase the previous transform and this may create distortions, sometimes. This method should only be used when restoring the original transformation! (source)
Solution: Instead of applying transform directly as explained, do it step by step like:
AffineTransform af = graphics.getTransform();
af.translate(yours.getTranslateX(), yours.getTranslateY());
af.scale(..);
... // if you use other transforms like rotation, shearing etc. add them as well
graphics.setTransform(af);
Note: Always restore the original transform. Best way to do it is creating a copy of graphics object and dispose it afterwards.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论