刺耳的图像不显示后
After jarring images do not show
public class LidPop extends Projectile
{
private int x;
private int y;
private int l = 3;
private int h = 18;
private int dx = -3;
private Image i;
public LidPop(int x, int y)
{
this.x = x;
this.y = y;
String rootPath = System.getProperty("user.dir");
String imgPath = rootPath + File.separator + "src/pics/lidPop.png" + File.separator;
try
{
i = ImageIO.read(new File(imgPath));
} catch (IOException ex)
{
System.out.println("not working");
}
}
@Override
public void paint(Graphics g)
{
g.drawImage(i, x, y, x+l, y+h, 0, 0, l, h, null);
}
当我 jar 代码和 运行 它时,加载的图像没有显示,但我知道它在那里,因为播放器仍然被它击中。有什么想法吗?
更新:
您不能使用 File
从 jar
文件直接加载文件。您可以使用 this.getClass().getClassLoader().getResourceAsStream("/src/pics/lidPop.png");
它将 returns InputStream
。
我假设 lidPop.png
在具有 src/pics/lidPop.png
路径的 jar 中。
修改 ImageIO.read()
方法以接受 InputStream
。
public class LidPop extends Projectile
{
private int x;
private int y;
private int l = 3;
private int h = 18;
private int dx = -3;
private Image i;
public LidPop(int x, int y)
{
this.x = x;
this.y = y;
String rootPath = System.getProperty("user.dir");
String imgPath = rootPath + File.separator + "src/pics/lidPop.png" + File.separator;
try
{
i = ImageIO.read(new File(imgPath));
} catch (IOException ex)
{
System.out.println("not working");
}
}
@Override
public void paint(Graphics g)
{
g.drawImage(i, x, y, x+l, y+h, 0, 0, l, h, null);
}
当我 jar 代码和 运行 它时,加载的图像没有显示,但我知道它在那里,因为播放器仍然被它击中。有什么想法吗?
更新:
您不能使用 File
从 jar
文件直接加载文件。您可以使用 this.getClass().getClassLoader().getResourceAsStream("/src/pics/lidPop.png");
它将 returns InputStream
。
我假设 lidPop.png
在具有 src/pics/lidPop.png
路径的 jar 中。
修改 ImageIO.read()
方法以接受 InputStream
。