JPanel 的绘制方法在被菜单项或工具提示遮挡时表现异常。

huangapple go评论63阅读模式
英文:

JPanel paint method misbehaves when obscured by menu items or tool tips

问题

基本上,当一个 JPanel 调用了 paintComponent 方法并应用了 AffineTransform,而同时又被诸如 JMenuItemJToolTip 实例这样的其他组件遮挡时,会对它产生某种变换效果,从而导致畸变(创建未绘制的间隙)。

可以通过以下方式实现这种效果:

  1. 使用 JSplitPane 来存储面板。
  2. 创建一个动画器(我使用了线程方法),不断重绘这个面板。
  3. 覆盖 paintComponent 方法并在其中使用 setTransform( new_transform )
  4. 添加带有足够长的工具提示的 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:

  1. using JSplitPane to store panel
  2. creating an animator (I've used Thread method) that constantly repaints this panel
  3. Overriding the paintComponent method and using setTransform( new_transform ) inside it.
  4. 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.

huangapple
  • 本文由 发表于 2020年7月25日 17:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63086919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定