Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to get the right Path?  (Read 555 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-02-26 22:58:35 »

I've been attempting to get a map loader working for the past hour or so now and it looks as if it should work but I can't seem to figure out how to get the proper path to load my TestMap.png resource. I checked around a bit and it seems as if I should be using an InputStream but the InputStream doesn't have .getWidth() or .getHeight() so I couldn't use it; after that I tried a few different paths but none of them seemed to work.

The resource I'm attempting to load is in C:\Users\MyName\Java Projects\Sword\src\Resources\Maps and the class trying to load it is within C:\Users\MyName\Java Projects\Sword\src\Functions\ I want this to be able to work after I've compiled everything into a .jar file later on as well so I'm not sure how to get this working now and for later when the path changed when it's within the .jar file. If anyone knows how to do this, please do tell.

Image loading code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
package Functions;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class MapLoader
{
    public MapLoader()
    {
    }
   
    public int[][] loadMap(String x) //Requires the map's file name. Don't include the extension.
   {
        int[][] map;

        BufferedImage bufferedImage = null;
        try
        {
            String y = "/Resources/Maps/" +x+ ".png";
            bufferedImage = ImageIO.read(new File(y));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        int[] pixels = new int[width * height];
        bufferedImage.getRGB(0, 0, width, height, pixels, 0, width);
       
        map = new int[width][height];
        //this channels the 1 dimensional pixel[] into the two dimensional map[][]
       for(int row = 0; row < height; row++)
        {
            for(int column = 0; column < width; column++)
            {
                map[column][row] = pixels[row * width + column];
            }
        }
       
        return map;
    }
}


I'm basing this all off of nhmllr's code from this thread: http://www.java-gaming.org/topics/how-to-make-maps-in-a-simple-2d-game/28841/view.html#msg263301
Offline jonjava
« Reply #1 - Posted 2013-02-27 00:50:20 »

use the ClassLoader to find the resource you're looking for as an URL.

1  
2  
URL url = this.getClass().getClassLoader().getResource(String name);
BufferedImage bimg = ImageIO.read( url );


An URL is a fancy class that tries to represents your resource for you in various ways (OS independandtly) and the ClassLoader is a fancy thing that tries to locate that resource based on its name also in nifty ways (relatively, OS independantly). IIRC.

Offline jonjava
« Reply #2 - Posted 2013-02-27 00:57:46 »

So since the calling class is in '/Functions' and the resource is in '/Resources' and they're both in '/src' which is in the class path I think the ClassLoader should be able to find it by simply its name so [x + ".png"] in this case or if not then specify it as ["/Resources/" + x + ".png"].

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-02-27 01:21:06 »

I just re-wrote a few parts to see if I could get it working with a URL; for some reason url is always null even though I specify the path to it. Have I written this incorrectly?

URL statement:
1  
URL url = this.getClass().getClassLoader().getResource("/Resources/Maps/"+x+".png");



Full Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
package Functions;

import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class MapLoader
{
    public MapLoader()
    {
    }
   
    public int[][] loadMap(String x) //Requires the map's file name. Don't include the extension.
   {
        int[][] map;
       
       
        URL url = this.getClass().getClassLoader().getResource("/Resources/Maps/"+x+".png");
        System.out.println(url);
        BufferedImage bufferedImage = null;
        try
        {
            bufferedImage = ImageIO.read(url);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        int[] pixels = new int[width * height];
        bufferedImage.getRGB(0, 0, width, height, pixels, 0, width);
       
        map = new int[width][height];
        //this channels the 1 dimensional pixel[] into the two dimensional map[][]
       for(int row = 0; row < height; row++)
        {
            for(int column = 0; column < width; column++)
            {
                map[column][row] = pixels[row * width + column];
            }
        }
       
        return map;
    }
}
Offline jonjava
« Reply #4 - Posted 2013-02-27 01:24:04 »

Try taking the first "/" out of the path in getResource("/Resources/Maps/"+x+".png");

-> getResource("Resources/Maps/"+x+".png");

Offline tyeeeee1
« Reply #5 - Posted 2013-02-27 01:25:39 »

Try taking the first "/" out of the path in getResource("/Resources/Maps/"+x+".png");

-> getResource("Resources/Maps/"+x+".png");

That seems to have done it; thanks for all the help!
Offline jonjava
« Reply #6 - Posted 2013-02-27 01:44:08 »

The "trick" here was that beginning the file name with a forward slash "/" will make the ClassLoader look for the resource as if it's an absolute path, essentially skipping the magic we're looking for.


http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)


http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#findResource(java.lang.String)

http://mindprod.com/jgloss/getresourceasstream.html

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!