这个问题类似于Paint on a glass pane without repainting other components。但我不知道如何应用那里提供的解决方案。
我的问题是,我们有一个非常复杂的RootPane,许多组件,重新绘制它是昂贵的。我们还在JFrame的GlassPane上运行了一个动画。我已经注意到,动画中的每一个滴答都在按其应有的方式重新绘制动画,但同时也会导致底层RootPane上的所有内容也被重新绘制。
我尝试用不同的代码重写RootPane的try ()方法,但是它总是会导致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的操作保持在最低限度的好方法?
发布于 2015-04-22 16:20:36
@MadProgrammer感谢你建议重新油漆(Rect)而不是仅仅重新油漆()解决了这个问题。现在,我们可以将动画up提高到我们想要的高度,并且它不会明显地影响后面的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();
}
...
}https://stackoverflow.com/questions/29784969
复制相似问题