XCode Java Image Display - Help!

Discussion in 'Mac Programming' started by stadidas, Feb 27, 2006.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    Hello everyone at MacRumors Forums.

    I have been having trouble with a little Java project I am working on.
    I have been using a Java learning environment called BlueJ to write small program, and have switched to XCode to make it a full Mac app.
    My trouble is, I had an image displaying in a JFrame in BlueJ, but I can't get it working in XCode.
    In my buildInterface() method (which is called in the constructor) I have this code:

    ImageIcon icon = createImageIcon("money.gif", "The app title icon.");
    top = new JLabel(icon);
    header.add(top);

    My createImageIcon method looks like this:

    protected static ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = Asset_Monitor.class.getResource(path);
    if (imgURL != null) {
    return new ImageIcon(path, description);
    } else {
    System.err.println("Couldn't find file: " + path);
    return null;
    }
    }

    The "money.gif" image file is located in the root directory of the project.
    As I said it displays fine in BlueJ but I can't get it working in XCode.
    Any help would be greatly appreciated.
     
  2. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #2
    Do you get the "Couldn't find file: someFile" error or does it just not show?
     
  3. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #3
    It does display the error in the log, yes.
    I've done a "get info" on money.gif, and it says its path is just money.gif, with it set to 'Relative to Enclosing Group'. 'Relative to Build Product' says the path is ../money.gif.
     
  4. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #4
    System.out.println(System.getProperty("java.class.path"));

    What is the runtime class path?
     
  5. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #5
    System.out.println(System.getProperty("java.class.path"));

    =

    /Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resources/Java/Asset Monitor.jar
     
  6. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #6
    What's the contents of the jar file?

    In terminal:

    jar tf "/Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resources/Java/Asset Monitor.jar"
     
  7. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #7
    META-INF/
    META-INF/MANIFEST.MF
    AboutBox$SymWindow.class
    AboutBox.class
    Asset_Monitor$1.class
    Asset_Monitor$10.class
    Asset_Monitor$2.class
    Asset_Monitor$3.class
    Asset_Monitor$4.class
    Asset_Monitor$5.class
    Asset_Monitor$6.class
    Asset_Monitor$7.class
    Asset_Monitor$8.class
    Asset_Monitor$9.class
    Asset_Monitor$clearActionClass.class
    Asset_Monitor$closeActionClass.class
    Asset_Monitor$copyActionClass.class
    Asset_Monitor$cutActionClass.class
    Asset_Monitor$newActionClass.class
    Asset_Monitor$openActionClass.class
    Asset_Monitor$pasteActionClass.class
    Asset_Monitor$saveActionClass.class
    Asset_Monitor$saveAsActionClass.class
    Asset_Monitor$selectAllActionClass.class
    Asset_Monitor$undoActionClass.class
    Asset_Monitor.class
    Asset_Monitorstrings.properties
    PrefPane$1.class
    PrefPane.class

    The .jar file is located in the application package in a folder called Java. The money.gif image is one folder back, in Resources, along with the application .icns file.
     
  8. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #8
    How about this? Or some variant of this.
    Code:
    protected static ImageIcon createImageIcon(String path, String description) {
      java.net.URL imgURL = Asset_Monitor.class.getResource(path);
      if (imgURL != null) {
        return new ImageIcon(path, description);
      } else {
        imgURL = Asset_Monitor.class.getResource("../"+path);
        if (imgURL != null) {
          return new ImageIcon("../"+path);
        } else {
          System.err.println("Couldn't find file: " + path);
          return null;
        }
      }
    }
    Or in the worst case:
    java.io.File classPath = new File(System.getResource("java.class.path"));
    classPath = classPath.getParentFile().getParentFile();
    imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
     
  9. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #9
    The first suggestion didn't work. For the second suggestion I put this:

    protected static ImageIcon createImageIcon(String path, String description) {
    java.io.File classPath = new File(System.getResource("java.class.path"));
    classPath = classPath.getParentFile().getParentFile();
    URL imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
    }

    I get a build error on the java.io.File line. I have imported java.io.File. Cannot resolve symbol : method getResource
     
  10. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #10
    Sorry, System.getProperty(...) and also java.io.File at both places if you want to be consistent.
     
  11. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #11
    ok thanks. What should the return statement be? Sorry, I've never seen that sort of code before, I'm not really sure what it's doing.
     
  12. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #12
    My intention was that you combined it with the original code. Basically it just gets the classpath "/Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resources/Java/Asset Monitor.jar", chops off "Java/Asset Monitor.jar" and then uses that to create the direct path to the image file.
    Code:
    protected static ImageIcon createImageIcon(String path, String description) {
      java.net.URL imgURL = Asset_Monitor.class.getResource(path);
      if (imgURL != null) {
        return new ImageIcon(path, description);
      } else {
        java.io.File classPath = new java.io.File(System.getProperty("java.class.path"));
        classPath = classPath.getParentFile().getParentFile();
        System.out.println("Xcode loading resource: "+classPath.getPath()+path);
        imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
        if (imgURL != null) {
          return new ImageIcon(imgURL, description);
        } else {
          System.err.println("Couldn't find file: " + path);
          return null;
        }
      }
    }
    but try this first:

    Code:
    protected static ImageIcon createImageIcon(String path, String description) {
      java.net.URL imgURL = Asset_Monitor.class.getResource(path);
      if (imgURL != null) {
        return new ImageIcon(path, description);
      } else {
        imgURL = Asset_Monitor.class.getResource("../../"+path);
        if (imgURL != null) {
          return new ImageIcon("../../"+path, description);
        } else {
          System.err.println("Couldn't find file: " + path);
          return null;
        }
      }
    }
    Edit: Finished removing obvious errors, but no guarantees.
     
  13. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #13
    The second thing you put didn't work.

    The first method couldn't find it but gave this:
    Xcode loading resource: /Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resourcesmoney.gif

    There's a slash missing between 'Resource' and ,money', but I don't know how to put it in.

    Thanks for helping me, I've not being doing Java for long.
     
  14. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #14
    Code:
        System.out.println("Xcode loading resource: "+classPath.getPath()+"/"+path);
        imgURL = Asset_Monitor.class.getResource(classPath.getPath()+"/"+path);
    No problem.

    I'm glad to help, but hopefully someone else will read this thread and have a much more elegant solution.
     
  15. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #15
    Still won't find it, even though it's the correct path... I don't know what to do!
     
  16. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #16
    Strange. My code had an error in it for a while. Are you using the latest edit with:
    return new ImageIcon(imgURL, description);
    in it?

    Also, does it still print the "Couldn't find file: someFile" error?
     
  17. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #17
    I am using the new version, and it does print the error.
    Sorry I've taken a while to reply, I'm also trying to bug fix CSS for IE... it's been a hard day.

    Also one other problem I have is that the JLabels which are set like this:

    label4 = new JLabel(" Money to spend per day: £" + perDay );
    labels.add(label4);

    look fine in BlueJ, but look like this in XCode:

    [​IMG]

    I don't know what's causing that either.
     
  18. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #18
    Code:
    protected static ImageIcon createImageIcon(String path, String description) {
      java.net.URL imgURL = Asset_Monitor.class.getResource(path);
      if (imgURL != null) {
        return new ImageIcon(path, description);
      } else {
        java.io.File imgFile = new java.io.File(System.getProperty("java.class.path"));
        imgFile = imgFile.getParentFile().getParentFile();
        imgFile = new java.io.File(imgFile, path);
        System.out.println("Xcode loading resource: "+imgFile.getPath());
        if (imgFile.exists()) {
          return new ImageIcon(imgFile.getPath(), description);
        } else {
          System.err.println("Couldn't find file: " + path);
          return null;
        }
      }
    }
    How about this then?
     
  19. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #19
    Yes! It works! I love you!
     
  20. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #20
    Thanks. :D


    "Format -> File Encoding -> Western" fixed the £ problem for me
     
  21. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #21
    You are brilliant! Thank you so much!
     
  22. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #22
    ok, now I have another problem.
    Not so big this time - I deleted the Asset_Manager.icns file, and have replaced it with another one of the same place, in the same location. The trouble is XCode is building the application with the default application icon, not the icon I have supplied. How can I get it to use the right icon?
     
  23. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #23
    I'm not entirely sure. How to set the app icon has always confused me a little bit. You could try to clean the project and then build it again, or you could try to give it another name and then change it to the new name in the settings and then clean/build.

    If all else fails, you can change the icon for the finished product using the Get Info in Finder and when you distribute the app in a .dmg, the icon will go with it.
     
  24. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #24
    What are you referring to by 'it'?
     
  25. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #25
    Oh sorry ...

    You could try to clean the project and then build it again, or you could try to give the icon file another name and then change the project settings to use the new name and then clean/build.
     

Share This Page