Results 1 to 8 of 8
Thread: getting source name of an image
- 12-09-2011, 09:14 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
getting source name of an image
Hi,
I made String Array and Random "r" variable:
I used them to load random image:Java Code:static String[] elements={"pictures//helium.png", "pictures//hydrogen.png"}; Random r = new Random();
My problem is that I want to do "if" statement which looks like that:Java Code:image = ImageIO.read(this.getClass().getResource(elements[r.nextInt(elements.length)]));
So I don't know how to get the image "source"?Java Code:if(image "source" .equals("pictures//helium.png"){ }
Can anybody help me?
- 12-09-2011, 11:02 PM #2
Re: getting source name of an image
Break the compound statement up into simple parts and save the String used in the getResource() method so you can use it later.
You could put the String and the image in a Map and use one to get the other.
- 12-09-2011, 11:38 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: getting source name of an image
I don't know whether this would be useful in my program.
I have to make a simple game. In this game chemical elements are falling and you have to click on them and write symbol.
For example helium.png is falling, you click on it and write "he".
My real problem is to distinguish chemical elements which are falling because of random selection of paths to .png files (in String [] elements).
That's how my game looks for now.
Last edited by pietr0; 12-09-2011 at 11:45 PM.
- 12-10-2011, 02:17 AM #4
Re: getting source name of an image
How does the program recognize what "chemical element" is falling?problem is to distinguish chemical elements which are falling because of random selection of paths to .png files
If you have a Map of images and Strings, and can get the image, then you can use the Map to get the String.
- 12-10-2011, 04:46 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: getting source name of an image
You mean to do:
I have never used HashMap before. I don't know how to use them in my game.Java Code:Map<String, Image> map = new HashMap<String, Image>(); //and then put every single image to my map map.put("pictures//hel.png", image = ImageIO.read(this.getClass().getResource("pictures//hel.png"))); // and the same with other chemical elements (122)??
Could you give me an example?
- 12-10-2011, 05:22 PM #6
Re: getting source name of an image
Did you try your example? That looks like the right way to put values into a map.
The order of the objects may be reversed. Which do you want to be the key and which the value?
The key is the object that you are using to look up the value with.
I thought you had the image(the key) and wanted to get the "path to the image" (the value).
- 12-10-2011, 05:45 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: getting source name of an image
Maybe my description was not clear.
To gain a point in my game you have to click on randomly generated image of chemical element and write its symbol.
If every of 112 chemical elements had it's own image (image1.png, image2.png, ... and so on) I would have to use 224 variables to draw appropriate images.
Like in paint() method:
That's why I decided to create only 5 images that can take randomly generated paths to .png files. Now I have only 5 variables (x, y) to draw an image.Java Code:g.drawImage(image224, x224, y224, 70, 20, this);
In my game to gain a point you have to click on image (chemical element) and write its symbol, as I said.
So I am supposed to write code like that:
Thats why I need path. I want to know whether I clicked on "helium image" or not.Java Code:if( "image source" .equals("pictures//helium.png")) { //so if we clicked on image that represents helium ... point++ //you have a point }
Maybe there is other way to count points?Last edited by pietr0; 12-10-2011 at 06:09 PM.
- 12-10-2011, 05:49 PM #8
Similar Threads
-
Web : frame source, not page source
By FunkyProg in forum New To JavaReplies: 0Last Post: 03-30-2011, 12:49 AM -
python source to java source converter?
By conor147 in forum New To JavaReplies: 0Last Post: 01-15-2010, 05:14 AM -
Same source file but different source folders for different build configurations?
By m3anman in forum EclipseReplies: 0Last Post: 01-29-2009, 10:43 AM -
MavenJava - browse source code of all open source projects online
By jirkacelak in forum Java SoftwareReplies: 1Last Post: 11-28-2008, 06:27 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks