【发布时间】:2022-01-06 15:25:53
【问题描述】:
当我尝试在 Java Swing 中渲染 png 图像时,它会被截断/缩放。这是我的代码:
this.setSize(500, 500);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getGraphics().drawImage(getImage("IMAGE PATH"), 0, 0, this);
这是我的getImage 函数
public static Image getImage(String path) {
File file = new File(path);
try {
return ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我要渲染的图像是 500x500,就像我的框架一样,但它仍然出现错误。这也适用于我使用这样的功能:
Image image = getImage("Image Path");
assert image != null;
JLabel picLabel = new JLabel(new ImageIcon(image));
【问题讨论】:
标签: java image swing jframe javax.imageio