Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:45:53+00:00 2026-06-01T12:45:53+00:00

I’m having a problem where I can’t find images through Java. My friend and

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:45:55+00:00Added an answer on June 1, 2026 at 12:45 pm
    // 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.