Anyone ever done this with images in a jar

I

Ike

What do people do when they must access images within a jar?

Do they hard-code the names of these images and their locations within the
jar?

And what then, if there are hundreds of images?

Please reserve your response unless you;ve actually done this; I've gone
down some very tangential avenues with peoples ideas on this, but what I
really need here is some solid help by someone who has actually done this
kind of thing. Thanks, Ike
 
B

BarryNL

Ike said:
What do people do when they must access images within a jar?

Do they hard-code the names of these images and their locations within the
jar?

And what then, if there are hundreds of images?

Please reserve your response unless you;ve actually done this; I've gone
down some very tangential avenues with peoples ideas on this, but what I
really need here is some solid help by someone who has actually done this
kind of thing. Thanks, Ike

Well, you have to get the name somehow, and if you can't generate it
then you have to hardcode it. If you can generate it, great! Do
something like:

getClass().getClassLoader().getResource(
"graphics/img" + getColor() + getType() + ".jpg");

might load images like "imgBlackQueen.jpg", "imgWhiteRook.jpg" etc.
 
S

Sudsy

Ike said:
What do people do when they must access images within a jar?

Do they hard-code the names of these images and their locations within the
jar?

And what then, if there are hundreds of images?

Ike,
I had to face a comparable situation when handling images for
an e-commerce site. There could easily be thousands of images
(possibly more than one per product) and I wanted to limit the
number of files per directory. I developed the following simple
code:

/**
* Get the path to a file. The name includes subdirectories.
* @param val the numeric value
* @param modulus the modulus to split on
* @param prefix the filename prefix
* @param suffix the filename suffix
* @return the complete path to the file, including subdirectories
*/
public static String getPathToFile( int val, int modulus,
String prefix, String suffix ) {
String value = Integer.toString( val );
while( ( value.length() % modulus ) != 0 )
value = "0" + value;
String response = "";
for( int i = 0; i < value.length(); i += modulus ) {
if( response.length() > 0 )
response += "/";
if( i == ( value.length() - modulus ) )
response += prefix;
response += value.substring( i, i + modulus );
}
return( response + suffix );
}

I use it like this:

String path = "/directory/" + Utils.getPathToFile( productId, 2, "img",
".html" );

If productId is 12345 then I end up with:

/directory/01/23/img45.html

I like to use a modulus of 2 as that will give me a maximum of 100
image files and 100 subdirectories in each directory. I like to keep
to under 255 entries in a directory due to limitations on some
systems.
If you don't have that limit then you can use a larger modulus. Using
3 as the modulus in the example above generates this file name:

/directory/012/img345.html

As always, YMMV
 
A

Andrew Thompson

| What do people do when they must access images within a jar?

Ask for advice, and follow it.
As I told you roughly 19 days ago.
http://www.google.com/groups?th=1c4e1564ff9b4409&seekm=btvj8u$m6m$1@n
ews.btv.ibm.com#link7

| Do they hard-code the names of these images and their locations within
the
| jar?

There is no need to hard code them.
Despite what you might be told.

| Please reserve your response unless you;ve actually done this; I've gone
| down some very tangential avenues with peoples ideas on this,

You did not question me that time Ike?

You also ducked out of a conversation
with me later, saying..
"I gave up on it all and just put it all in the
same jar as the applet. Got move on to other
things here. THanks anyhow. -Ike"....

I have spent a deal of time attempting to
help you Ike, yet you fail to get the simple
things and continually re-ask them. It is
very frustrating trying to help you.

Anyway, I could point you to links like this..
http://www.physci.org/codes/ziplet.jsp?zip=/PhySci.jar&dflt=Ziplet.java
that points straight to an applet reading
..java source straight out of the PhySci.jar
file, I specify the Ziplet.java file as the default,
but you will notice on the left that there is a
tree of all the files.

I do not hard code that, that entire tree
is read STRAIGHT OUT OF THE FILE.

Am I getting though yet?

That code in the RH window is the java
source of the Applet that you are looking at,
which calls in other PhyScu classes.

So, that applet is _proof_ that you do not
need to hard code any filenames, but can
enumerate them....

[ Shhhheeesh! ]
 
I

Ike

Thank you! God, Ive been going nuts with this; part of my problem is that I
am callling "across directories" within the jar, rather than "below" where I
am in it.
 
I

Ike

Andrew,

Yeah, well, I'm guilty of working on too many things simultaneously, and
getting all tripped up - missing things which should be obvious, but which
break working code. I hadnt realised I was enumerating corresponding files
on the local drive as opposed to the jar for one, trying to call across
'directories' in a jar, etc. etc. Moral--> It's a bad idea to try to work on
numerous things simultaneously especially when in a time pinch.

I appreciate your help nevertheless, -Ike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top