背景在 JPanel 重绘



我有一个有多个层的程序,在底层我有一个JPanel,我放了一个背景图像。最重要的是,我有一个JLayeredPane,它有一些可拖动的组件。我的问题是,当用户上传背景图像时,可拖动组件确实滞后,我的猜测是因为在拖动时重新绘制背景图像。我的问题是,是否无论如何都要确保图像不会一直重新绘制? 我绘制图像的代码如下所示:

if (this.getLoadedBackgroundImage() != null) {
try {
BufferedImage bufferedImage = ImageIO.read(this.getLoadedBackgroundImage());
Image bImage = bufferedImage.getScaledInstance(this.getWidth() - 5 - jPanel6.getWidth() -5, this.getHeight() - jPanel2.getHeight() - jPanel3.getHeight() - tabPanel.getHeight(), Image.SCALE_FAST);
graphics2d.drawImage(bImage, 5, jPanel2.getHeight() + tabPanel.getHeight()+5, null);
} catch (IOException ex) {
Logger.getLogger(MainViewLayeredPane.class.getName()).log(Level.SEVERE, null, ex);
}
}

不要使用paintComponent方法加载图像或调整图像大小,这些都是昂贵的操作,而是预缓存它并只绘制缓存的版本

最新更新