Java 找不到图像

Java 找不到图像,java,image,swing,Java,Image,Swing,我遇到了一个问题,无法通过Java找到图像。我的朋友和我正在做一个项目,我们做了完全相同的事情。我已经更改了图像位置的路径,甚至将图像拖放到Eclipse中。然而,我没有运气。这是我的密码: import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.In

我遇到了一个问题,无法通过Java找到图像。我的朋友和我正在做一个项目,我们做了完全相同的事情。我已经更改了图像位置的路径,甚至将图像拖放到Eclipse中。然而,我没有运气。这是我的密码:

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class MapArray {
    static JPanel[][] tiles = new JPanel[30][29];
    static String[][] images = new String[30][30];
    final static int SIZE = 30;
    static int place=0;

public MapArray(){

}

protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Map.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

public static void setMap(){

    try {
        String a = getFileContents("C:\\Users\\*****\\workspace\\Pokemon\\src\\map1.txt");
        for(int x=0; x<29; x++){
            for(int y=0; y<30; y++){
                images[x][y]=a.substring(0,a.indexOf(" "));
                a=a.substring(a.indexOf(" ")+1);
                System.out.println(images[x][y]);
            }
        }
    } catch (Exception e) {
        System.out.println("y u no work :(");
    }

}

public static String getFileContents(String fileName) throws Exception {
    File theFile = new File(fileName);
    byte[] bytes = new byte[(int) theFile.length()];
    InputStream in = new FileInputStream(theFile);
    int m = 0, n = 0;
    while (m < bytes.length) {
        n = in.read(bytes, m, bytes.length - m);
        m += n;
    }
    in.close();
    return new String(bytes); 
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            setMap();
            JFrame frame = new JFrame();
            frame.setLayout(new GridLayout(30, 29, 0, 0));
            for (int i = 0; i < 29; i++) {
                for (int j = 0; j < 29; j++) {
                    tiles[i][j] = new JPanel(new GridLayout());
                    tiles[i][j].add(new JLabel(
                            createImageIcon("C:\\Users\\*****\\workspace\\Pokemon\\src\\tile"+"-"+images[i][j]+".png")));
                    frame.add(tiles[i][j]);
                }
            }
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    });
}


}
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入java.awt.Image;
导入java.awt.Insets;
导入java.awt.Toolkit;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.InputStream;
导入java.util.Map;
导入javax.swing.ImageIcon;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类映射数组{
静态JPanel[][]tiles=新JPanel[30][29];
静态字符串[][]图像=新字符串[30][30];
最终静态整数大小=30;
静态整数位=0;
公共映射数组(){
}
受保护的静态图像图标createImageIcon(字符串路径){
java.net.URL imgURL=Map.class.getResource(路径);
如果(imgURL!=null){
返回新的图像图标(imgURL);
}否则{
System.err.println(“找不到文件:+path”);
返回null;
}
}
公共静态void setMap(){
试一试{
字符串a=getFileContents(“C:\\Users\\*****\\workspace\\Pokemon\\src\\map1.txt”);
对于(int x=0;x
有人能帮你找到相对路径吗

将文件放在
src
文件夹中,
MapArray.java
文件旁边

src/
 |-- MapArray.java
 |-- ...
 `-- map1.txt

map1.txt
将被移动到
bin
目录中,在
.class
文件旁边(默认情况下,bin/在Eclipse中隐藏,但这是设置类路径的地方)。稍后,您还需要确保将资源文件打包到
.jar

中,而不是发布一大堆代码并且不指定问题中的错误消息,您可以从一个简单的代码片段开始(我忽略导入,…因为我太懒了,无法启动IDE)


这是您发现/调试问题所需的内容。然后您可以开始将其转换为相对路径。

如果资源本身就是应用程序的一部分。(嵌入式应用程序资源)而不是为了写,它们应该添加到应用程序运行时类路径上的Jar中,并通过从
class.getResource()
获取的URL进行访问

URL urlToMap1 = this.getClass().getResource("/src/map1.txt");

您需要检查资源在Jar中的确切路径,并从Jar的根(
/
)引用它,然后是Jar中的路径(
src/map1.txt
).

你可以发布一些输出,让我们知道什么是有效的,什么是无效的吗?找不到文件:C:\Users\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu有些不同-你百分之百确定你的大小写正确吗?问题是我朋友的计算机上的图像仍然没有显示。我的朋友一切正常,但我找不到图像。这似乎仍然不能解决找不到图像的问题。我可以向你保证他们在正确的位置。我已经记录了确切的错误消息:“找不到文件:C:\Users\***\workspace\Pokemon\src\tile-24.png”并转到该位置,图像显示出来。您尝试过了吗?
createImageIcon(“./src/tile”+”-“+images[I][j]+.png”);
@chicagochichillin path=“map1.txt”。只是文件的基本名称,没有任何前面的目录。这意味着在
.class
文件旁边查找,无论它在哪里。噢,对不起,您需要更改
getFileContents(String path)
方法。在它内部调用
MapArray.class.getResourceAsStream(path)
并将流转换为
字符串
-示例。
src/
 |-- MapArray.java
 |-- ...
 `-- map1.txt
public static void main( String[] args ){
  File file = new File( "C:...");//with the path you use in your code
  System.out.println( file.exists() );
}
URL urlToMap1 = this.getClass().getResource("/src/map1.txt");