XCode, Java, drawing images

Discussion in 'Mac Programming' started by chrisS, Sep 21, 2007.

  1. chrisS
    Expand Collapse
    macrumors member

    Joined:
    Sep 21, 2007
    #1
    I am just getting started with XCode/Java and wanted to create a simple app that displays an image. I created a new "Java Swing Application" project called SwingTest and added the following code to the SwingTest constructor just before the setSize and setVisible calls. Nothing displays and I am trying to figure out why. I added the image to the project by dragging and dropping it into the Resources directory of the project and I can see it inside the contents of the built application package at /Contents/Resources/sample.jpg but it won't display. Any ideas are welcome, thanks.

    Attempt #1:

    ImageIcon pIcon = new ImageIcon("sample.jpg");
    JLabel imgLabel = new JLabel(pIcon);
    this.add(imgLabel);

    Attempt #2:

    String imgName = "sample.jpg";
    java.net.URL imgURL = getClass().getResource(imgName);
    Toolkit tk = Toolkit.getDefaultToolkit();
    MediaTracker m = new MediaTracker(this);
    Image img = tk.getImage(imgURL);
    ImageIcon i = new ImageIcon(img);
    JLabel imgLabel = new JLabel(i);
     
  2. wrldwzrd89
    Expand Collapse
    macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    Your code looks just fine. The problem is that your application's working directory isn't set to be inside the application package - hence why the application can't find the image and thus displays nothing. I believe this is an option somewhere in the project build settings.

    DISCLAIMER: I am new to XCode too. I normally use Eclipse for Java development because, when I try to use XCode I get warnings about some imported classes being deprecated, and I can't, for the life of me, figure out why. :eek:
     
  3. toddburch
    Expand Collapse
    macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    Are you adding these elements to a JPanel or JFrame somewhere else that you didn't show?

    Todd

    (EDIT - I didn't go through the hoop of creating the same test app, and doing that might have answered this question.)
     
  4. wrldwzrd89
    Expand Collapse
    macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    If the OP's Java class extends JFrame then this shouldn't be an issue, unless the OP forgot to set the visible property to true...
     
  5. chrisS
    Expand Collapse
    thread starter macrumors member

    Joined:
    Sep 21, 2007
    #5
    I am adding to main class which extends JFrame. I tried changing the Edit Active Executable under the Project menu, specifically the "Set the working directory to:", but none of the choices make a difference.
     
  6. chrisS
    Expand Collapse
    thread starter macrumors member

    Joined:
    Sep 21, 2007
    #6
    If I add a "System.out.println(System.getProperty("java.class.path"));", I get two directories ...

    /Users/chris/SwingTest/build/Debug/SwingTest.app/Contents/Resources/Java/SwingTest.jar:
    /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibility/14compatibility.jar

    How do I use just the first directory ?
     
  7. wrldwzrd89
    Expand Collapse
    macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #7
    Is your JFrame showing up on screen, and you're just not getting any image to appear inside it? If that's the case, try using an absolute path (starting with a /, such as /Applications/YourApp.app/Contents/Resources/image.jpg ) and see if that gets the image to load. If it does, that confirms that the problem is a working directory issue. If it still doesn't load, there is some other problem - one possible cause is your JLabel call. I usually make JLabels that contain images the following way:
    Code:
    JLabel blah = new JLabel("",ii)
    where ii is an ImageIcon. The "" makes the label have no text.
     
  8. chrisS
    Expand Collapse
    thread starter macrumors member

    Joined:
    Sep 21, 2007
    #8
    Ok, I copied the image to the desktop and changed code to ...

    ImageIcon ii = new ImageIcon("/Users/chris/Desktop/sample.jpg");
    JLabel imgLabel = new JLabel(ii);
    imgLabel.setVisible(true);
    this.add(imgLabel);

    The window still appears, but still has the default striped background, nothing else.
     
  9. chrisS
    Expand Collapse
    thread starter macrumors member

    Joined:
    Sep 21, 2007
    #9
    Found the answer...

    I changed ...

    this.getContentPane().setLayout(null);

    to ...

    this.getContentPane().setLayout(new GridLayout(0, 1));

    and it works. The top line was generated for my Xcode project by Xcode, weird.
     

Share This Page