gpt4 book ai didi

java - 防止 RootPane 重绘 GlassPane 重绘

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:17 32 4
gpt4 key购买 nike

这个问题类似于Paint on a glass pane without repainting other components 。但我不知道如何应用那里提供的解决方案。

我的问题是我们有一个非常复杂的 RootPane,有很多组件,并且重新绘制它的成本很高。我们还有一个在 JFrame 的 GlassPane 上运行的动画。我注意到动画的每个刻度都会按照应有的方式重新绘制动画,但也会导致底层 RootPane 上的所有内容也被重新绘制。

我尝试使用各种代码覆盖 RootPane 的 Paint() 方法,但它总是会导致 RootPane 上的组件被删除并出现大量闪烁,可能是因为子组件在动画期间更新时尝试重新绘制自身:

pnlContent = new JRootPane() {
@Override
public void paint(Graphics g) {
OurGlassPaneWithAnimation glass = ...
if (glass != null && glass.animation != null && glass.animation.isAlive()) {
//Animation running, do something cool so root pane still looks good without repainting out the wazoo
} else { super.paint(g); }
}
};

也许将动画放在 GlassPane 中是个坏主意?我们考虑过将其更改为位于居中的 JPanel 中。或者有没有一种好方法可以使用 GlassPane 来实现此目的,并将对背景 RootPane 的重绘保持在最低限度?

最佳答案

@MadProgrammer 感谢您的建议,使用 repaint(rect) 而不是仅仅使用 repaint() 解决了问题。现在我们可以将动画 fps 调高到我们想要的程度,并且不会明显影响其后面的 RootPane 的加载时间。这是代码片段。

while (keepOnAnimating) {
BufferedImage bi = vFrames.elementAt(0);
if (bi != null) {
// This only repaints area of image rect
// Prevents lots of repaints from happening in rootpane behind glasspane.
int x = getWidth() / 2 - bi.getWidth() / 2;
int y = getHeight() / 2 - bi.getHeight() / 2;
jc.repaint(x, y, bi.getWidth(), bi.getHeight());
} else {
jc.repaint();
}
...
}

关于java - 防止 RootPane 重绘 GlassPane 重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784969/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com