图像不会加载到jar文件中

时间:2014-01-02 15:40:47

标签: java embedded-resource image-loading

我开始研究一个简单的游戏,如果我从eclipse运行它可以正常工作 但是当我从jar文件运行它时,它给了我这个错误:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1291)
    at net.gijs.platformer.Tile.<init>(Tile.java:35)
    at net.gijs.platformer.Component.start(Component.java:47)
    at net.gijs.platformer.Component.main(Component.java:77)

这是我的代码

组件

package net.gijs.platformer;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Scanner;

import javax.swing.JFrame;

public class Component extends Applet implements Runnable
{   
    private static final long serialVersionUID = 1L;

    private static int pixelSize = 2;

    private Image screen;


public static Dimension size = new Dimension(700, 560);
public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);
public static String name = "2D Adventure ";
public static String version = "Dev1";

public static double sX = 0, sY = 0;
public static double dir = 0;

public static Level level;
public static Char character;

public static boolean running = false;
public static boolean isMoving = false;

public Component()
{
    setSize(size);
    setPreferredSize(size);

    addKeyListener(new Listening());
    setFocusable(true);
}

public void start()
{
    //Difining objects, etc.
    new Tile(); //Loading images, etc.

    character = new Char(Tile.tileSize/2, Tile.tileSize);
    level = new Level();

    //Starting gameloop.
    running = true;
    new Thread(this).start();
}

public void stop()
{
    running = false;
}

public static void main(String args[])
{
    Component component = new Component();

    JFrame frame = new JFrame();

    frame.add(component);
    frame.pack();

frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle(name + version);

component.start();
}

public void run()
{
    screen = createVolatileImage(pixel.width, pixel.height);

    // setting the name of your character
    Scanner scanner = new Scanner(System.in);

System.out.println("Hello, Whats your name?");
System.out.print("My name is ");
String name = scanner.nextLine();
System.out.println("Hello, "+ name);
System.out.println("Sorry, cant set your name right now, feature will be implemented soon!");

while(running)
{
tick();
render();   

try 
{
Thread.sleep(5);
} catch (Exception e) 
{
     e.printStackTrace();
}
}
}
private void tick() 
{
level.tick();
character.tick();

}

private void render() 
{
Graphics g = screen.getGraphics();

//Drawing things
g.setColor(new Color(100, 100, 249));
g.fillRect(0, 0, pixel.width, pixel.height);

    level.render(g);
    character.render(g);

    g = getGraphics();

    g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
    g.dispose();
}   
}

瓷砖

package net.gijs.platformer;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Tile {

public static int tileSize = 20;

public static int[] air = {-1, -1};
public static int[] earth = {0, 0};
public static int[] grass = {3, 0};
public static int[] rock = {2, 0};
public static int[] gravel = {1, 0};
public static int[] water = {4, 0};
public static int[] lava = {5, 0};

public static int[] characterright = {0, 18};
public static int[] characterleft = {0,19};
public static int[] characterstatic = {1,18};

public static int[] bubbles1 = {18,18};
public static int[] bubbles2 = {18,19};
public static int[] hearts1 = {17,18};
public static int[] hearts2 = {17,18};


public static BufferedImage tileset_terrain;

public Tile()
{
    try {
        Tile.tileset_terrain = ImageIO.read(new File("resources/images/tileset_terrain.png"));
    } catch (Exception e) 
    {
        e.printStackTrace();
    }           
}
}

抱歉,如果代码看起来很乱,这是我第一次在stackoverflow上发布一些内容,所以我不习惯发布系统。

我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:2)

使用http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29

这将为您提供包装在jar中的文件的输入流。