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);
}
当我震动代码并运行它时,加载的图像没有显示,但我知道它在那里,因为播放器仍然被它击中。有什么想法吗?
更新:
您无法使用 File
直接从jar
文件加载文件。您可以使用this.getClass().getClassLoader().getResourceAsStream("/src/pics/lidPop.png");
它将返回InputStream
.
我假设lidPop.png
在src/pics/lidPop.png
路径的罐子里。
修改ImageIO.read()
方法以接受InputStream
。