有没有办法只将特定的 JPanel 区域标记为不透明?
基本上我画了很多透明的 JPanel;分析显示大部分时间花费在 Component.paint() 上。可以从根本上对此进行优化,因为在大多数情况下,每个 JPanel 的实际非不透明区域非常小,例如边缘周围。
就目前而言, repainting() 组件将触发其所有父级的重新绘制,因为 RepaintManager 无法知道脏父级区域实际上是不透明的,并且会沿着组件层次结构向上移动。我正在考虑每当面板失效时对所有父级调用 markCompletelyClean()
并使用 addDirtyRegion()
自己管理脏区域。
但是,是否有一种更简洁的方法,仅将 JPanel 的特定矩形标记为不透明(或透明,无关紧要)?
Basically I'm drawing a lot of transparent JPanels; profiling shows that most time is spent in Component.paint(). It would be possible to optimize this quite radically since in most cases the real non-opaque area per JPanel is quite small, for example around the edges.
As it stands now, repainting() a component would trigger a repaint of all its parents, since the RepaintManager can't know the dirty parent area is in fact opaque and will go up the component hierarchy. I was thinking about calling markCompletelyClean()
on all parents whenever a panel is invalidated and managing the dirty regions myself with addDirtyRegion()
.
However, is there a cleaner approach of marking only specific rectangles of a JPanel as opaque (or transparent, doesn't matter)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使您使用 markCompletelyClean() 和 addDirtyRegion() 做了一些奇特的事情,我怀疑您是否会获得很大的性能优势。当 swing 进行重新绘制时,它会整理所有要绘制的脏区域,并使用所有脏区域的最小边界框开始重新绘制。因此,如果您将 JPanel 的周界标记为脏,那么 JPanel 的边界框与整个 JPanel 相同,这意味着您将以任何方式重新绘制整个事物。
Even if you do something fancy with markCompletelyClean() and addDirtyRegion(), I doubt you'll get much of a performance benefit. When swing goes for a repaint, it collates all of the dirty regions to paint and starts a repaint with the minimum bounding box of all of the dirty regions. Thus if you mark the perimeter of a JPanel as dirty, then the bounding box of the JPanel is the same as the entire JPanel, which means you'll be repainting the entire thing any way.
考虑使用
JLabel
(默认情况下非不透明)而不是JPanel
,然后您就可以(到JLabel
)添加任何JComponent
与JPanel
相同,但是您必须设置LayoutManager
,然后您就忘记了关心Opacity/Transparency
和 for详情 地区Consider using
JLabel
(non-opaque by default) instead ofJPanel
, then you are able (to theJLabel
) you can add anyJComponent
same as to theJPanel
, but you have to setLayoutManager
, then you forgot care aboutOpacity/Transparency
and for particulars Region(s)为什么不能定义自己的方法
myRepaint()
为所有边框调用多个方法?
还可以尝试使用图形的 ClipBounds 来仅重新绘制真正必要的部分。您可以将任何自定义形状设置为剪辑。
Why can't you define your own method
myRepaint()
calling multiplefor all the borders?
Also try to play with clipBounds of your Graphics to repaint only really necessary parts. You can set any custom Shape as clip.