Can't find image

I have a problem where I cannot find images via Java. My friend and I are working on a project and we did the same. I changed the paths to the location of the images and even dragged / dropped the images in Eclipse. However, I was out of luck. Here's my code:

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


}

      

Everything I've tried with setting the full image path doesn't work. Also, can anyone help with relative paths? My friend and I will be sharing codes across multiple computers, so relative paths that are not intended for where our workspace is located would be great. Thank!

+3


source to share


4 answers


// get resource of *your* class, instead of Java Map.class
MapArray.class.getResource(path);
...
String a = getFileContents("map1.txt"); // local path, not absolute

      

and place the file in a folder src

next to the file MapArray.java

.



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

      

map1.txt

will be moved to the directory bin

next to the file .class

(bin / is hidden in Eclipse by default, but where the classpath is set). Later, you will also want to make sure the resource file is packaged in .jar

.

+2


source


can anyone help with relative paths?



String a = getFileContents("./src/map1.txt");

      

+1


source


Instead of posting a whole bunch of code and not listing the error message you get in your question, you can start with a simple piece of code (I'm ignoring the imports ... as I'm too lazy to get my IDE up and running)

public static void main( String[] args ){
  File file = new File( "C:...");//with the path you use in your code
  System.out.println( file.exists() );
}

      

This is about finding / debugging your problem. Then you can start converting it to a relative path.

+1


source


If resources are an integral part of the application. (embedded applications resource), not writeable, they must be added to the Jar on the application's runtime classpath and accessible via the url obtained from Class.getResource()

. It will work something like this:

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

      

You will need to check the exact path in the Jar that the resource is in and reference it from the root of the Jar ( /

) and then the path to the Jar ( src/map1.txt

).

+1


source







All Articles