Click to See Complete Forum and Search --> : jakarta-ant & java - where are my icons?


nanode
12-13-2000, 11:33 AM
I'm using 'ant' as a build tool for a project. I'm very happy with it so far, it compiles and places classes in the correct destination. It also copies some .jpg and .gif files that I'm using for icons - however those icons are NOT visible at runtime. I'm fairly sure my build.xml is ok:


<target name="images" depends="prepare">
<copydir src="${src.dir}" dest="${lib.dir}">
<include name="**/*.gif" />
<include name="**/*.jpg" />
</copydir>

</target>

<target name="compile" depends="images" >
<javac srcdir="${src.dir}" destdir="${lib.dir}"
classpath="${lib.dir}"
debug="off" deprecation="on" optimize="on">
</javac>

</target>



It does in fact cp over the files to 'lib' before it compiles.

this is how my code is loading images:


ImageIcon radio = new ImageIcon("../images/radio.jpg");
ImageIcon check = new ImageIcon("../images/check.jpg");


Now my java class knows that it can find the images in a parallel dir. called 'images'. The compiled class has the same relative path as the source.

Any ideas why my images don't show?

Dru Lee Parsec
12-14-2000, 02:26 AM
Are you running it from a jar file? If so then you need to use something like this


public ImageIcon getJarImages(String imageName)
{
InputStream in = null;
byte[] b = null;
int size = 0;

in = getClass().getResourceAsStream("/"+imageName);
try
{
size = in.available();
b = new byte[size];
in.read(b);
}
catch(IOException e)
{
}
return new ImageIcon(Toolkit.getDefaultToolkit().createImage( b));
}


You would use this method like this:

private ImageIcon fileNewIcon = getJarImages("com/tapper/images/new.gif");
private ImageIcon fileOpenIcon = getJarImages("com/tapper/images/open.gif");


BTW, when I say "runningit" I mean the program , not ant. It's a very common issue that your icons show up when running out of the com tree but not when running from a jar. If the issue is occuring when you're running from the com tree as well then you're not pointing to the right directory for your icons. This method is a good way to access icons in any case because whenyou do move to running from a jar file it will continue to work.

[This message has been edited by Dru Lee Parsec (edited 14 December 2000).]

nanode
12-14-2000, 12:30 PM
Dru Lee,

Issue resolved:

ImageIcon button = new ImageIcon(Toolkit.getDefaultToolkit()
.getImage(getClass().getResource("../images/button.jpg")));


That resolved the issue when running from com tree and the jar.

Thanks for the tip.

BTW: one of our senior guys thinks I'm trying to undermind their build process by using ant. http://www.linuxnewbie.org/ubb/smile.gif

if(buildTool != make) {
employee.setAnarachist(true);
}


They can do whatever they want with my code, but while I'm developing and testing, I'm using ant.

Dru Lee Parsec
12-15-2000, 03:08 PM
Re: Ant. You make me proud! http://www.linuxnewbie.org/ubb/smile.gif

I was the anarchist at Union Bank. I demanded such wildly outside thinking ideas such as: version control and cross-platform build tools.

Imagine that! Does that sound like me?

Re: icons

Exactly! Notice how both of our solutions use getResource? That's the key to getting and icon from a jar file.

nanode
12-15-2000, 06:00 PM
Dru Lee:

I recall a story you shared once about writing your own Hashtable not knowing that such a thing existed before. It's an awesome feeling to discover on your own, you did something that many people had to be taught.

BTW: please send me an email w/ your mailing address - thanks