Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Loading images...  (Read 1594 times)
0 Members and 1 Guest are viewing this topic.
Offline thotved

Senior Newbie




Java games rock!


« Posted 2005-04-13 16:01:11 »

I'm having problems when I pack my game into a jar, it won't load images right.. Read somewhere that I had to use  class.getClassLoader().getResource()  or something to get it right? Also, should I use BufferedImage?(is it faster?)

I'm using a singleton-class "BildeButikk" (ImageShop in english) to load images.

Image   background = BildeButikk.get().getImage("b.png");


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  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
package superspillet;
import java.util.HashMap;
import javax.swing.ImageIcon;
import java.net.URL;
import java.awt.*;

/**
 * BildeButikk.java
 * Singleton for å hente bilder.
 */

public class BildeButikk
{
    private static BildeButikk      butikk = new BildeButikk();
    private HashMap                 bilder;
    public BildeButikk()
    {
        bilder = new HashMap();
    }

    public static BildeButikk get()
    {
        return butikk;
    }

    public void removeImage(String id)
    {
        //dispose??
       bilder.put(id,null);
    }

    public Image getImage(String id)
    {
       if(bilder.get(id)==null)
       {
           try
           {
               //Config.get.getPath("bilder") returns "data/bilder/"
              Image tmp = loadImage(Config.get().getPath("bilder") + id);
               bilder.put(id, tmp);
           }
           catch(Exception e)
           {
               System.out.println("Kunne ikkje laste bilde: " + id);
               System.exit(0);
           }
       }
       return (Image) bilder.get(id);
    }

    /**********************************
    * metode for å laste bilde
    * @param fileName Filnavnet på bilder som skal lastes.
    *              f.eks.  "bilder/b.gif"
    * @return Image et objekt av klassen Image
    **********************************/

    private Image loadImage(String fileName)
    {
        //URL newFileName = this.getClass().getClassLoader().getResource(fileName);
       Image xx = null;
        try
        {
            xx = new ImageIcon(fileName).getImage();
        }
        catch(Exception err)
        {
            System.out.println("Feil ved lasting av bilde.:" + err.getMessage());
            err.printStackTrace();
        }
        return xx;
    }
}
Offline Nimloth

Senior Newbie




Java games rock!


« Reply #1 - Posted 2005-04-13 17:32:50 »

Hey, i have the same Problem (-:

I have opened the "Create a valid URL Object"-Thread

I havn't found the solution yet, so i can't really help you...
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #2 - Posted 2005-04-13 18:12:38 »

You have to use

1  
2  
import java.io.BufferedInputStream;
import java.io.InputStreamReader;



1  
2  
3  
InputStreamReader fr = new InputStreamReader(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(ref))); 
        
BufferedReader br = new BufferedReader(fr);


then you will be able to read from a jar file,

btw "ref" is a string to you file location!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom
« Reply #3 - Posted 2005-04-13 18:21:42 »

hmmm, norwegian code Wink

The way to load images with ImageIO is as follows:

1  
2  
BufferedImage src = javax.imageio.ImageIO.read( 
                        new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(resourcePath)));

If you make the getImage function static then getClass() will not compile. Then you have to call getClass() on an instance on one of your classes. Call getClass() on a java.awt.Point and you might get the wront class loader. This might be what is tripping you up, Nimloth. Can also try Thread.currentThread().getClass().

resourcePath is the relative path to the image. Always use "/" as seperator and always start with a "/", even if it is in the root.

Make sure the image is in the correct place. Remember that path is case sensitive.

Disclaimer: I might be wrong about some of details.

Offline Nimloth

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-04-13 19:38:12 »

Thx for the Hint Tom, i use the Classloader of my resource class

[code]
public static URL getFileURL(String aFile) {
 String file = "/"+ aFile;
   System.out.println(file);
   return Resource.class.getClassLoader().getResource(file);
//        return getURL("file:"+aFile);
     }
[code]

The uncomment Version was earlier - this worked. The classloader still does not... Here the List of the things that i tried:

- The Jar File is on the same Directory Level as the Image (Resources) Directories are. I put that Directory in the classpath:

D:\projects\temp>java -classpath D:\projects\temp -jar sajotris.jar

- i repleaced "/" with "\"
- i placed the resources in the same Directory where the classfiles are

Java always can't find the resource
"/fonts/Blox2.ttf"

I tried everything - except the right (-:
Offline tom
« Reply #5 - Posted 2005-04-13 23:18:17 »

Have you tried without using executable jars? The manifest has a Class-Path attribute that might mess with your classpath. Try this instead:
D:\projects\temp>java -classpath .;sajotris.jar YourMainClassHere

Offline thotved

Senior Newbie




Java games rock!


« Reply #6 - Posted 2005-04-14 00:59:38 »

Can't get it to work.. I've tried anthing I can think of now..

Sad

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
    
private BufferedImage loadImage(String s)
    {
        BufferedImage src = null;
        try
        {
            BufferedInputStream inStream = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("/"+s));
            src = javax.imageio.ImageIO.read(inStream);
            //System.out.println("laster: "+s+" som er: "+src);
       }
        catch(Exception e)
        {
            System.out.println("feil ved lasting av bilde: "+s);
            e.printStackTrace();
            System.exit(0);
        }
        return src;
    }


The problem is that the src object is null.. According to javadoc the read() method in ImageIO returns null if no registered ImageReader claims to be able to read the stream... My guess is that it can't find the image?

Example of s: "data/bilder/ting/box1.gif"

I'm using JBuilder X,.. The file structure in the jar-file:
[SuperSpillet.jar]
-data
---bilder
-----ting
-------box1.gif
-superspillet
---BildeButikk
Offline Nimloth

Senior Newbie




Java games rock!


« Reply #7 - Posted 2005-04-14 07:29:00 »

@tom

I tried that. I tried your version, I extracted the jar and tried, but it doesn't work.
Offline thotved

Senior Newbie




Java games rock!


« Reply #8 - Posted 2005-04-17 12:00:26 »

I now know that my problem is classpath-related.. If I move the data folder to the folder called classes(this is where JBuilder put my compiled files) then it works with
getClass().getClassLoader().getResource(...).. But the problem is that when I press "rebuild", JBuilder deletes all files in the classes folder..  Sad I lost A LOT of my new graphics this way  Angry

So, I want to have the "data" folder in the project folder right next to "src" and "classes".. This works with jar if I add the following to the manifest:
Class-Path: .;superspillet;

But it doesn't work inside JBuilder, but I guess it will when I figure out how to change the classpath.. Anyone know how to do this? If I go to project -> project properties -> classpath
I see the classpath, but can't change it..
Offline thotved

Senior Newbie




Java games rock!


« Reply #9 - Posted 2005-04-17 13:17:34 »

finally it works  Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nimloth

Senior Newbie




Java games rock!


« Reply #10 - Posted 2005-04-17 14:03:05 »

Yes, it works finally. I found an article which says that the "Jar Classloader" won't take any -classpath command line Arguments.

Here the Article:

http://www-106.ibm.com/developerworks/java/library/j-onejar/
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.

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

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

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

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

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

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
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

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!