有没有办法只将特定的 JPanel 区域标记为不透明?

发布于 2024-12-16 19:13:51 字数 362 浏览 1 评论 0原文

基本上我画了很多透明的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独﹏钓一江月 2024-12-23 19:13:51

即使您使用 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.

笑红尘 2024-12-23 19:13:51

考虑使用 JLabel (默认情况下非不透明)而不是 JPanel,然后您就可以(到 JLabel)添加任何 JComponentJPanel 相同,但是您必须设置 LayoutManager,然后您就忘记了关心 Opacity/Transparency 和 for详情 地区

Consider using JLabel (non-opaque by default) instead of JPanel, then you are able (to the JLabel) you can add any JComponent same as to the JPanel, but you have to set LayoutManager, then you forgot care about Opacity/Transparency and for particulars Region(s)

初见 2024-12-23 19:13:51

为什么不能定义自己的方法 myRepaint()

public void repaint(long tm, int x, int y, int width, int height)

为所有边框调用多个方法?

还可以尝试使用图形的 ClipBounds 来仅重新绘制真正必要的部分。您可以将任何自定义形状设置为剪辑。

Why can't you define your own method myRepaint() calling multiple

public void repaint(long tm, int x, int y, int width, int height)

for 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文