Cannot Find Image

Go To StackoverFlow.com

3

I'm having a problem where I can't find images through Java. My friend and I are working on a project and we've done the exact same things. I've changed the paths to the location of the images and even dragged/dropped the images into Eclipse. However, I've had no 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 putting in the full image path doesn't work. Also, would anyone be able to help with relative paths? My friend and I will be sharing code between multiple computers so relative paths that aren't dedicated on where our workspace is located would be great. Thanks!

2012-04-05 16:43
by chicagochillin
could you maybe post some output so we have some idea what does and doesn't work - KevinDTimm 2012-04-05 16:48
Couldn't find file: C:\Users___\workspace\Pokemon\src\tile-24.pn - chicagochillin 2012-04-05 16:50
That's just one example of it. It can read from the text file but it can't find the images that I'm 100% are in that exact location - chicagochillin 2012-04-05 16:51
Since this worked yesterday (http://stackoverflow.com/questions/10003610/gridlayout-removing-padding-between-jpanels) something is different - are you 100% sure that you have the capitalization correct - KevinDTimm 2012-04-05 17:02
The problem is that that worked on my friend's computer. The images still didn't show up on mine. Everything works fine for my friend but the images can't be found for me - chicagochillin 2012-04-05 17:03


2

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

and put the file to your src folder, next to the MapArray.java file.

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

map1.txt will be moved into bin directory, next to .class file (bin/ is hidden in Eclipse by default, but that's where the classpath is set). Later you'll also want to make sure that the resource file is packaged into .jar.

2012-04-05 17:20
by Bartosz Moczulski
path = C:\Users\__\workspace\Pokemon\bin - chicagochillin 2012-04-05 17:30
path = "map1.txt". Just the base name of the file, without any preceding directories. It means look next to the .class file, wherever it is - Bartosz Moczulski 2012-04-05 17:34
Where would that line of code go - chicagochillin 2012-04-05 17:47
Oh, sorry, you'll need to change getFileContents(String path) method. Inside it call MapArray.class.getResourceAsStream(path) and convert the stream into a String - here's a sample how - Bartosz Moczulski 2012-04-05 18:02


1

would anyone be able to help with relative paths?

String a = getFileContents("./src/map1.txt");
2012-04-05 16:46
by Eng.Fouad
That still doesn't seem to fix the problem with the images not being found. I can assure you that they're in the right locations. I've taken the exact error message: "Couldn't find file: C:\Users\ **\workspace\Pokemon\src\tile-24.png" and gone to that location and the image shows up - chicagochillin 2012-04-05 16:48
Have you tried? createImageIcon("./src/tile"+"-"+images[i][j]+".png");Eng.Fouad 2012-04-05 16:50
Yes. Unfortunately that doesn't work either - chicagochillin 2012-04-05 16:54
@chicagochillin worksmKorbel 2012-04-05 17:47


1

Instead of posting a a whole bunch of code and not specifying the error message you get in your question, you could start with a simple code snippet (I neglect imports, ... since I am too lazy to fire up my IDE)

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 what you need to discover/debug your problem. Then you can start on converting it to a relative path.

2012-04-05 17:59
by Robin


1

If the resources are inherently part of the app. (an embedded application resource) and not for write, they should be added to a Jar on the application's run-time class-path and accessed via URL obtained from Class.getResource(). It would work something like:

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

You'd need to check the exact path in the Jar that resource ends up at, and reference it from the root of the Jar (/) then the path within the Jar (src/map1.txt).

2012-04-05 18:44
by Andrew Thompson
Ads