Results 1 to 8 of 8
  1. #1
    pietr0 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default getting source name of an image

    Hi,

    I made String Array and Random "r" variable:

    Java Code:
    static String[] elements={"pictures//helium.png", "pictures//hydrogen.png"}; 
    
    Random r = new Random();
    I used them to load random image:

    Java 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:
    Java Code:
    if(image "source" .equals("pictures//helium.png"){
    
    }
    So I don't know how to get the image "source"?
    Can anybody help me?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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.

  3. #3
    pietr0 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: getting source name of an image

    Quote Originally Posted by Norm View Post
    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.

    getting source name of an image-game.png
    Last edited by pietr0; 12-09-2011 at 11:45 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: getting source name of an image

    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.

  5. #5
    pietr0 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: getting source name of an image

    You mean to do:

    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)??
    I have never used HashMap before. I don't know how to use them in my game.
    Could you give me an example?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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).

  7. #7
    pietr0 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default 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:

    Java 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:

    Java 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?
    Last edited by pietr0; 12-10-2011 at 06:09 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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.

Similar Threads

  1. Web : frame source, not page source
    By FunkyProg in forum New To Java
    Replies: 0
    Last Post: 03-30-2011, 12:49 AM
  2. python source to java source converter?
    By conor147 in forum New To Java
    Replies: 0
    Last Post: 01-15-2010, 05:14 AM
  3. Replies: 0
    Last Post: 01-29-2009, 10:43 AM
  4. Replies: 1
    Last Post: 11-28-2008, 06:27 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •