最小化窗口时图像丢失
我已将图像添加到画布,然后在面板上显示该画布。但是,当我最小化然后最大化窗口时,图像从面板中消失。我该如何解决这个问题?以下是代码:
public class CloseCanvas extends Canvas{
private static final long serialVersionUID = 2L;
@Override
public void paint(Graphics g) {
setSize(new Dimension(30,22));
BufferedImage image = null;
try {
image = ImageIO.read(new File("res/close.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
g.drawImage(image, 0, 0, null);
}
}
I have added an image to a Canvas and then display that canvas on Panel. But when I minimize and then maximize the Window, the image disappears from the Panel. How can I solve this? Following is the code:
public class CloseCanvas extends Canvas{
private static final long serialVersionUID = 2L;
@Override
public void paint(Graphics g) {
setSize(new Dimension(30,22));
BufferedImage image = null;
try {
image = ImageIO.read(new File("res/close.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
g.drawImage(image, 0, 0, null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这结合了 Fredrik 和 mKorbel 的建议,以及一些与当前问题无关的其他提示。
This incorporates the advice of Fredrik and mKorbel, plus a few other tips unrelated to the immediate problem.
我建议您从绘制方法中移出图像加载。它看起来相当静态,对于画布的每次重新绘制,图像都会重新加载,这种情况会发生很多很多次,并且会在事件分派线程上发生。
I'd suggest you move out the image loading from the paint method. It seems quite static and for every repaint of the Canvas the image will be reloaded which happens many, many times and that will happen on the event dispatch thread.