这样的IM导入图像作为背景,由于某种原因它给我的使用方法:不知道为什么图像不显示任何
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
有人能帮助我吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PixelLegendsMain extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
}
public static void main(String[ ] args)throws Exception{
PixelLegendsMain plMain = new PixelLegendsMain();
arenaBuild arena = new arenaBuild();
plMain.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
plMain.add(arena);
plMain.setSize(600,460);;
plMain.setVisible(true);
plMain.setResizable(false);
plMain.setLocation(200, 200);
}
}
这是主类和这样的:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.io.*;
import javax.swing.Timer;
public class arenaBuild extends JPanel{
String picPath = "pictures/";
String[] fileName = {picPath+"stageBridge.png", picPath+"turret.png"};
ClassLoader cl = arenaBuild.class.getClassLoader();
URL imgURL[] = new URL[2];
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG;
public arenaBuild()throws Exception{
for (int x=0;x<2;x++){
imgURL[x]= cl.getResource(picPath+fileName[x]);
}
imgBG = tk.createImage(imgURL[0]);
}
public void paintComponent(Graphics g){
g.drawImage(imgBG,0,0,600,460,0,0,600,460, this);
}
}
Thjis就是即时调用在IM的图像的新本,所以我会很感激,如果有人可以解释为什么发生这种情况,并帮助。我修复它:D
尝试使用“/图片/”而不是“图片/”。并调用super.paintComponent(g) - 它不会解决你的直接问题,但将解决你的约束与其他问题 – MadProgrammer
看看这个例子。 http://ideone.com/DpZz12 –