我已经遍布StackOverflow尝试为此找到解决方案,但它只是踢我的屁股。我必须为类项目重制Space Invaders,我需要使用ImageIO加载图像(.gif文件)。
我的所有图像和声音文件都在源文件夹中,如下所示。
每当我使用new File(/SItop0.gif")
时,它都会给我一个IIOException。为什么我不能加载我的图像?
这是我的代码:
public BufferedImage getImage() {
try {
BufferedImage image = ImageIO.read(new File(/SItop0.gif")); // Line 30: this is the same for SITop, SIMiddle, and SIBottom. I'm just getting different images for each.
return image;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是我想要在面板上绘制的对象的类,下面的代码在我调用该方法的面板的类中:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 10; j++) {
if(i == 0) {
SITop invader = new SITop();
g.drawImage(invader.getImage(), 75+35*j, 80+25*i, null);
}
if(i == 1 || i == 2) {
SIMiddle invader = new SIMiddle();
g.drawImage(invader.getImage(), 75+35*j, 80+25*i, null);
}
if(i == 3 || i == 4) {
SIBottom invader = new SIBottom();
g.drawImage(invader.getImage(), 75+35*j, 80+25*i, null); // Line 42
}
}
}
}
这是我得到的错误:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at SIBottom.getImage(SIBottom.java:30)
at SIPanel.paintComponent(SIPanel.java:42)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
编辑:
我将行更改为BufferedImage image = ImageIO.read(new File("Assets/SItop0.gif"));
并更改了方法以覆盖paintComponent,现在它打开了框架和面板,但它似乎没有绘制任何东西。屏幕只是黑色。
编辑2:
将我的问题中的代码更改为与我现在使用的代码一致。