1
我想加載圖像並設置爲背景,但我在selectionPanel方法「無法加載背景圖像」時出現錯誤。加載圖像設置背景時出錯
看來圖像處理出了問題,但我找不出什麼。
public class selectionPanel extends JPanel
{
//Variable with the name of our pic
private static final String path = "selbkgrnd.jpg";
private ImageIcon imageIcon;
private Dimension windowSize;
private int imageHeight;
private int imageWidth;
protected selectionPanel()
{
this.imageIcon = new ImageIcon("selbkgrnd.jpg"); //Save image to imageIcon
Image image = this.imageIcon.getImage();
this.imageWidth = image.getWidth(null);
this.imageHeight = image.getHeight(null);
if ((this.imageWidth == -1) || (this.imageHeight == -1)) //Check if background image is succesfully loaded
{
JOptionPane.showMessageDialog(JOptionPane.getDesktopPaneForComponent(this), "Could not load background image", "Fatal error!", 0);
System.exit(-1);
}
this.windowSize = new Dimension(this.imageWidth, this.imageHeight);
}
protected void startScreen()
{
setOpaque(false);
setSize(this.windowSize);
}
protected int getImageHeight()
{
return imageHeight;
}
protected int getImageWidth()
{
return imageWidth;
}
@Override
public void paintComponent(Graphics g)
{
g.drawImage(this.imageIcon.getImage(), 0, 0, null);
super.paintComponent(g);
}
}
該圖像位於源代碼文件夾內。我會嘗試你所說的。 – Manos 2013-04-06 16:05:37
謝謝它與完整路徑一起工作,但它不應該只與圖像名稱一起使用,如果它在源代碼文件夾內? – Manos 2013-04-06 16:14:19
@mkontakis:請參閱編輯。 – 2013-04-06 16:46:35