Results 1 to 6 of 6
  1. #1
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default loading resources in an applet

    Hey all -

    So i converted a java project into an applet. It runs and works fine in the viewer running off my local HD. So i make and html page and upload the source up onto the server and it loads but none of my textures load. So I think to myself not a problem. I go through and I add in getImage(getCodeBase(), "serverfolder/imagetoload.png"); but i do not get an image. I output the path to make sure it is correct (getCodeBase().toString() + "serverfolder/imagetoload.png") and it is correct and the file is there, but i dont see any image loaded (skybox).

    I asked around and the only answer i get is that that is the prefered method to load stuff using a JApplet and to try either a staright URL or classPath load resource thing. Now the URL doesnt work, and I am trying the classPath now, but I want my resources in a seperate jar file, but I am having problems telling my project to look in that jar for the resources. I can do it just fine when i have the resources in the main jar file, but it makes it very bloated and Id rather not do that. Is there a way I can get my application to look in the "resource.jar" for all my images?

    Thanks In Advance,

    Gatts

  2. #2
    Zamppa is offline Member
    Join Date
    Feb 2009
    Location
    Finland
    Posts
    13
    Rep Power
    0

    Default

    It's possible, just takes a bit of tricking about...

    Have you added the resource file into the archive field of the applet tag (as in, <applet code="main.class" archive="main.jar resource.jar">)?

    If you use that you should be able to use the resource files as if the resource.jar was extracted to the same folder as main.jar.

  3. #3
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    Yeah i have the archive set. I use it for the jogl libraries and jme jars. The problem I am having is that in my JApplet project I am trying to the the classloader load resource thing and I dont know how to use that with an external resource.jar. When the resources are in my main japplet jar i can do the whole myapplet.class.getresource("resource.png") and it loads just fine. I'm not sure how to link the resource jar in my code now to get it to load. It is a netbeans project and i have inclueded the resource.jar as a library. I have done this with jmonkey and jogl and it has always worked. I am guessing it is how I created my resource.jar.

    I basically made an new project in netbeans called assets and for the source files all i did was created packages and added in the image files i needed. An example of the structure would be:

    textures.skybox would be one package and i would have 6 images there for the skybox and there would be another package textures.terrain and Id have various images for the world terrain. I do not have any classes or anything in the jar; all i have are assets in it. I dont know really how to set up a jar full of nothing but resources.

    So basically how would u set up a resource jar and include it in a netbeans proeject. Then how do you load the resources out of that resource jar code wise ?

    Thanks In Advance,

    Gatts79

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If the jar is in the applet's classpath then the System classloader should be able to find resources from their full package paths. The problem might be that particular class's classloaders aren't configured to search the whole classpath.

    Otherwise, you can hack it with a combination of java.net and java.util.jar that would certainly work.

  5. #5
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default Still Struggling with getting this to work

    So these are some things I've been trying. I was thinking that possibly it was the structure of my assets jar so I recreated it. I created a folder called assets and placed two folders in there; one called textures and the other called models. In the command line jar - cf assets.jar -C assets/ . and it created my assets.jar.

    In netbeans i went the Libraries in the project and added the assets.jar. The code I am using to create an Image which I then use for my skybox is as follows:

    URL imageLocation = getClass().getResource("assets/textures/imilogo.png");
    ImageIcon icon = new ImageIcon(imageLocation);
    Image image = icon.getImage();

    I have a break point set at imageLocation as i debug the Applet when it starts. imageLocation continues to return null. To make sure I was using getResource correctly i placed the images in the same package as the applet class and it still came up null. I've also tried the getImage(URL, file) and that too comes up null. I have run out of ideas on what to do.

    Now running it with local calls of loading from a specified HD location works. Basically a ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "assets/textures/logo.png"); works just fine. I change it to a static URL on the server and again the image i get from the icon is null.

    Could you explain more on what your talking about Orange?

    Thanks again for the suggestions,

    Gatts

  6. #6
    Zamppa is offline Member
    Join Date
    Feb 2009
    Location
    Finland
    Posts
    13
    Rep Power
    0

    Default

    Sorry for not giving more info earlier. I've actually been struggling with similar matter (plus, stubbornly trying to use ImageIO for this sort of thing in applets... Not recommended) ever since that and have learned a lot about this stuff, mostly by spectacular failures...

    Anyhow...

    In our application we use simple zip files for resources (some generated by server side java application, some manually updated created with any zip archiver when necessary). They don't need to be generated in as complicated way as you do, nor do they need to be defined as libraries (or at least we don't). When running locally you don't need to define the files as such, and on remote they need to be defined as I mentioned in my earlier posts.

    Now, one problem I've battled with most often is getting the right path and I'm guessing you might have the same issue here. Try using URL.toExternalForm() to find out where the application is trying to find the files from and see if you can find an error there.

    Also, I noticed that if your applet class is not in the root of your application jar file relative paths are big pain in the butt. Because of that we use absolute paths directly into the jar files. Sounds scary and I suppose it is a bit of a hack, but it works for us. The URL would be something like this:

    (sorry for the spaces, I'm not allowed to post urls)
    jar:ht tp://ww w.mypage.com/myappFolder/res/resource.zip!/assets/textures/imilogo.png

Similar Threads

  1. Protocol for loading resources from jar
    By SamSam in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2009, 07:14 PM
  2. Why is one applet loading but the other isnít?
    By spmchugh82 in forum Java Applets
    Replies: 1
    Last Post: 01-26-2009, 03:39 PM
  3. Loading resources generates warning messages
    By Namita Patil in forum New To Java
    Replies: 6
    Last Post: 07-03-2008, 02:04 PM
  4. Problem loading resources.
    By jimm1 in forum Advanced Java
    Replies: 6
    Last Post: 06-23-2008, 07:31 PM
  5. Java Applet loading bar
    By genette in forum Java Applets
    Replies: 2
    Last Post: 12-27-2007, 03:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •