This site requires JavaScript, please enable it in your browser!
Greenfoot back
fejfo
fejfo wrote ...

2015/3/27

is there anny easy way to find the sorce file of a greenfootImage?

fejfo fejfo

2015/3/27

#
with "sorce file" I mean the file that I gave with
new GreenfootImage("filename"); 
I know the isn't always one but maybe have it throw an exception if there is none. I know this is possible beacause if I printed the string version of the image it gave me this: Image file name: tree.png Image url: file:/C:/Users/myname/Documents/programing/greenfoot/money%20fight/images/tree.png greenfoot.GreenfootImage@a2d6c5
danpost danpost

2015/3/27

#
@myname (fejfo), looks like you found a way:
GreenfootImage image = new GreenfootImage("filename.png");
String toString = image.toString();
toString = toString.substring(toString.indexOf("url: file:/")+11, toString.length()-34);
System.out.println("'"+toString+"'");
fejfo fejfo

2015/3/27

#
what does
toString = toString.substring(toString.indexOf("url: file:/")+11, toString.length()-34);
do ?
Super_Hippo Super_Hippo

2015/3/27

#
danpost danpost

2015/3/27

#
Broken down, it might look like this:
int startChar = toString.indexOf("url: file:/")+11;
int stopChar = toString.length()-34;
toString = toString.substring(startChar, stopChar);
Line 1 determines where the substring "url: file:/" is location within the 'toString' string; adding 11 to that value puts us to the character after the indexed string. Line 2 gets the index 34 characters from the end of the 'toString' string, to before the "greenfoot.GreenfootImage@??????" part of the string. Line 3 reassigns 'toString' to the substring between those two indecis.
danpost danpost

2015/3/30

#
Super_Hippo wrote...
Java API String
You can also refer to the java tutorials on Strings, starting here.
davmac davmac

2015/3/30

#
There is no supported, future-proof way to determine the source image file for a GreenfootImage.
danpost danpost

2015/3/30

#
Was this just a curiosity question, or was there a specific reason for asking it? and if there was, why? that is, what is it you were hoping to accomplish once the acquired filepath/name was acquired?
You need to login to post a reply.