简体   繁体   English

图片未加载到jar文件中

[英]Image doesn't load in jar file

I started working on a simple game, which if I run it from eclipse works fine. 我开始研究一个简单的游戏,如果我从eclipse运行它,效果很好。
But when I run it from a jar file it gives me this error: 但是,当我从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)

And this is my code 这是我的代码

Component 零件

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();
}   
}

Tile

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();
    }           
}
}

sorry if the code looks messy, this is the first time i post something on stackoverflow so im not used to the posting system. 抱歉,如果代码看起来凌乱,这是我第一次在stackoverflow上发布内容,因此我不习惯发布系统。

I hope someone can help me. 我希望有一个人可以帮助我。

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

That will give you an input stream to the file packaged inside the jar. 这将为您提供打包到jar中的文件的输入流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM