PDA

View Full Version : XCode Java Image Display - Help!




stadidas
Feb 27, 2006, 01:33 PM
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 (http://www.bluej.org) 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.



gekko513
Feb 27, 2006, 01:42 PM
Do you get the "Couldn't find file: someFile" error or does it just not show?

stadidas
Feb 27, 2006, 01:45 PM
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.

gekko513
Feb 27, 2006, 01:53 PM
System.out.println(System.getProperty("java.class.path"));

What is the runtime class path?

stadidas
Feb 27, 2006, 01:58 PM
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

gekko513
Feb 27, 2006, 02:09 PM
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"

stadidas
Feb 27, 2006, 02:15 PM
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.

gekko513
Feb 27, 2006, 02:26 PM
How about this? Or some variant of 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 {
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);

stadidas
Feb 27, 2006, 02:34 PM
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

gekko513
Feb 27, 2006, 02:37 PM
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
Sorry, System.getProperty(...) and also java.io.File at both places if you want to be consistent.

stadidas
Feb 27, 2006, 02:41 PM
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.

gekko513
Feb 27, 2006, 02:51 PM
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.
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.
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:

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.

stadidas
Feb 27, 2006, 02:57 PM
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.

gekko513
Feb 27, 2006, 03:02 PM
There's a slash missing between 'Resource' and ,money', but I don't know how to put it in.


System.out.println("Xcode loading resource: "+classPath.getPath()+"/"+path);
imgURL = Asset_Monitor.class.getResource(classPath.getPath()+"/"+path);

Thanks for helping me, I've not being doing Java for long.
No problem.

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

stadidas
Feb 27, 2006, 03:04 PM
Still won't find it, even though it's the correct path... I don't know what to do!

gekko513
Feb 27, 2006, 03:07 PM
Still won't find it, even though it's the correct path... I don't know what to do!
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?

stadidas
Feb 27, 2006, 03:26 PM
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:

http://www.matefeedkillrepeat.com/images/labelerror.png

I don't know what's causing that either.

gekko513
Feb 27, 2006, 03:41 PM
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?

stadidas
Feb 27, 2006, 03:44 PM
Yes! It works! I love you!

gekko513
Feb 27, 2006, 03:53 PM
Yes! It works! I love you!
Thanks. :D


"Format -> File Encoding -> Western" fixed the problem for me

stadidas
Feb 27, 2006, 04:06 PM
Thanks. :D


"Format -> File Encoding -> Western" fixed the problem for me
You are brilliant! Thank you so much!

stadidas
Feb 27, 2006, 05:27 PM
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?

gekko513
Feb 27, 2006, 05:38 PM
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.

stadidas
Feb 27, 2006, 05:45 PM
or you could try to give it another name and then change it to the new name in the settings and then clean/build.


What are you referring to by 'it'?

gekko513
Feb 27, 2006, 05:51 PM
What are you referring to by 'it'?
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.

stadidas
Feb 27, 2006, 05:53 PM
ok cool. Where are the setting I need to change to get it to look at a different icon?

stadidas
Feb 27, 2006, 06:30 PM
ok, I looked at the info.plist file, and I had used an underscore in the file name instead of a space. Silly me!
The only thing now is to get the icon to appear in the "About the application" window.

gekko513
Feb 27, 2006, 06:31 PM
ok, I looked at the info.plist file, and I had used an underscore in the file name instead of a space. Silly me!
The only thing now is to get the icon to appear in the "About the application" window.
I'm glad it worked out.

stadidas
Feb 27, 2006, 06:56 PM
Thank you for all of your help over the last few hours, you made this application possible!
I've compared the "About" box to that of an earlier version of the project whose icon does display in the about box, and I can't see why it isn't showing up. Another puzzle, probably for another time as I've been working for nearly nine hours.