getting source name of an image
Hi,
I made String Array and Random "r" variable:
Code:
static String[] elements={"pictures//helium.png", "pictures//hydrogen.png"};
Random r = new Random();
I used them to load random image:
Code:
image = ImageIO.read(this.getClass().getResource(elements[r.nextInt(elements.length)]));
My problem is that I want to do "if" statement which looks like that:
Code:
if(image "source" .equals("pictures//helium.png"){
}
So I don't know how to get the image "source"?
Can anybody help me?
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.
1 Attachment(s)
Re: getting source name of an image
Quote:
Originally Posted by
Norm
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.
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.
Attachment 2217
Re: getting source name of an image
Quote:
problem is to distinguish chemical elements which are falling because of random selection of paths to .png files
How does the program recognize what "chemical element" is falling?
If you have a Map of images and Strings, and can get the image, then you can use the Map to get the String.
Re: getting source name of an image
You mean to do:
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)??
I have never used HashMap before. I don't know how to use them in my game.
Could you give me an example?
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).
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:
Code:
g.drawImage(image224, x224, y224, 70, 20, this);
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.
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:
Code:
if( "image source" .equals("pictures//helium.png")) {
//so if we clicked on image that represents helium
...
point++
//you have a point
}
Thats why I need path. I want to know whether I clicked on "helium image" or not.
Maybe there is other way to count points?
Re: getting source name of an image
Do you know what image you clicked on?
If you have the reference to the image that was clicked on, use it with the Map to get the path to that image.