Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像不';不能在jar文件中加载_Java_Embedded Resource_Image Loading - Fatal编程技术网

Java 图像不';不能在jar文件中加载

Java 图像不';不能在jar文件中加载,java,embedded-resource,image-loading,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.platform

我开始做一个简单的游戏,如果我在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)
瓦片 抱歉,如果代码看起来很混乱,这是我第一次在stackoverflow上发布内容,所以我不习惯发布系统

我希望有人能帮助我。

使用


这将为打包在jar中的文件提供一个输入流。

一旦在jar中,它就是一个输入流。在这种情况下,必须通过
URL
而不是
File
访问资源。请参阅标签,了解形成
URL
的方法。请尝试
ImageIO.read(getClass().getResource(“/images/tileset\u terrain.png”))
顺便说一句-该代码存在很多问题。睡在EDT上,尝试调整小程序的大小,混合Swing和AWT,调用
getGraphpics()
。。无论您使用什么资源来学习Java,都不要使用它,而是使用Java教程。您学习Java的资源是什么?Andrew Thompson,这是我的源代码使用
getResource(…)
更安全,原因有二。1) 在
getResourceAsStream(…)
中,有关使用前导
/
的规则很迟钝(我看到人们为此争论了好几个小时)。2)返回的流无法重新定位。
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();
    }           
}
}