using resource files in a relocatable fashion - HELP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dorito
    New Member
    • Jun 2009
    • 18

    #1

    using resource files in a relocatable fashion - HELP

    I am building an app within NetBeans and I am trying to make my use of resource files relocatable, so when I run my app on a different system it still finds the resource files.

    My problem is that I use a graphics file xxx.PNG that I need to dynamically paint on a JPanel. I’ve set it up as follows (the application is called MiscDemoApp):

    Image xxxImage;
    static String resourcePath;

    org.jdesktop.ap plication.Resou rceMap resourceMap =
    MiscDemoApp.get Application().g etContext().get ResourceMap(Mis cDemoView.class );

    // the file path location for all the PNG files is stored in a Property field
    resourcePath = new String (resourceMap.ge tString("FileLo cation"));
    xxxImage=Toolki t.getDefaultToo lkit().getImage (resourcePath + "xxx.png");

    Then, within the resources I have:

    Source Packages /
    miscdemo.resour ces /
    MiscDemoView.pr operties /
    FileLocation=C:/Documents and Settings/Chris/My
    Documents/NetBeansProject s/MiscDemo/src/miscdemo/resources/

    I put xxx.PNG in the directory above (…/miscdemo/resources/xxx.PNG) and this runs OK on my WinXP system … it finds the PNG OK. When I take my JAR file over to a different PC running WinVista it does not appear to find the PNG. I copied over my entire NetBeans project, which resides in:

    c:\users\Chris\ My Documents\NetBe ansProjects\Mis cDemo

    … thus the “FileLocation” resource string is no longer valid on the other system - the file path is slightly different. The obvious answer is to make the location of the PNG relative somehow, and therefore system-independent.

    So the next thing I tried was (as suggested by an example on the Web):

    try {
    xxxImage = javax.imageio.I mageIO.read(Mis cDemoApp.class. getResource("sr c/miscdemo/resources/xxx.png"));
    } catch (java.io.IOExce ption e) {

    }

    … but this seems to return a NULL to the ImageIO.read() call.

    HELP – there has to be an easier way to use local resources. [I realize Java needs to accommodate the complexities of Web resources usage, etc. … but it seems quite complex in comparison to Visual Studio where I just needed to create a *.rc resource file and everything else was handled.

    [NOTE: in cases where I’ve needed simply to paint a static image, I’ve used code such as:

    logoLabel = new javax.swing.JLa bel();
    logoLabel.setIc on(resourceMap. getIcon("logoLa bel.icon"));
    logoLabel.setNa me("logoLabel") ;

    jPanel3.setLayo ut(null);
    jPanel3.add (logoLabel);
    logoLabel.setBo unds (10,10,190,190) ;

    … where the properties contain:
    logoLabel.icon= MiscLogoSmall.p ng

    This seems to work on both the WinXP and Vista systems (… and doesn’t seem to need a file path to locate the resource). So I’m looking for the same behavior with my xxxImage. I don't want to use a JLabel for it, I want to paint it as such:

    class GraphPanel extends javax.swing.JPa nel {
    @Override
    public void paintComponent( java.awt.Graphi cs g){
    super.paintComp onent(g);
    // paint the image
    g.drawImage (xxxImage, …
    ]



    Thanks for any help,
    Chris
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Originally posted by dorito
    Code:
    try {
         xxxImage = javax.imageio.ImageIO.read(MiscDemoApp.class.getResource("src/miscdemo/resources/xxx.png"));
    } catch (java.io.IOException e) {
    
    }
    … but this seems to return a NULL to the ImageIO.read() call.
    Seems to? If xxxImage actually does end up null the most common reason would be that the image (either file or jar archive entry) is not at the place you specified.

    You can get more information by (1) Printing the URL returned by getResource() to see where you are actually looking for the image and (2) Doing something in the catch block: at a minimum e.printStackTra ce().

    Comment

    Working...