Results 1 to 10 of 10
  1. #1
    panic's Avatar
    panic is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default how to compare icons

    hi all!
    i was wondering how could i compare two icons,i tried just if(icon1==icon2)
    but it havent worked:( soo,plz help me!!:o

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    have you tried
    Java Code:
    if (icon1.equals(icon2))

  3. #3
    panic's Avatar
    panic is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    yes ,it also returns only false

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by panic View Post
    yes ,it also returns only false
    Not surprised since I don't think ImageIcon (assuming that's what you're comparing) overrides Object.equals()...so it would just be comparing whether they are the same reference.

    If you want to know whether the icons look the same you'll probably have to find another way than simply comparing the objects that do the drawing. Probably some sort of status thing...it all depends what you're actually doing, of course.

  5. #5
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    One method would be to keep a key for each icon you create then compare the keys.

    ie
    Java Code:
    Map<ImageIcon,Integer> imageMap;
    
    if( imageMap.get( image1 ).equals( imageMap.get (image2 ) ) )
    Mr. Beans

  6. #6
    panic's Avatar
    panic is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    the Map method seems good, and i tried to wrote that, but when i wrote that, there is an Exception, it says Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

  7. #7
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Can you post the code?

    Also please note the code I posted won't compile. You would have to create an instance of a class which implements the Map interface. I simply wrote it for informational purposes.

    Referencing the imageMap without initializing it will result in a NullPointerException

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Tolls View Post
    Not surprised since I don't think ImageIcon (assuming that's what you're comparing) overrides Object.equals()...so it would just be comparing whether they are the same reference.
    OP: Perhaps you could say what you mean by two icons being equal?

    Classes have an equals() method that defines something reasonable for equality of instances. Reasonable, but not sacred. You are free to implement any other reasonable notion of equality: but it would help to say what you mean. (loaded from the same file/url, having the same pixel values, something else...)

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    If you are comparing the ImageIcons by file, you could just compare the Strings returned from toString(). If comparing by size, compare getHeight and getWidth. However, there are so many other ways to compare them... there is a reason ImageIcon does not override .equals
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    panic's Avatar
    panic is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    thnx,a lot! toString() worked!:)

    Mr. Beans i have initialized the imageMap but didnt created the instance of a class that implements a Map interface,actually i havent understood what that means,!
    thnx to all ,you helped me!!

Similar Threads

  1. Putting clickable icons on an image
    By szakee in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2009, 10:25 AM
  2. is there anyone how can help me with image icons please?
    By xbox_nutter in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 10:26 AM
  3. [SOLVED] Icons path
    By janpol1 in forum Eclipse
    Replies: 1
    Last Post: 03-03-2009, 12:31 PM
  4. How do I display a JList with icons and text?
    By aneesahamedaa in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 08:49 AM
  5. Moving icons on your desktop
    By Leprechaun in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:07 AM

Posting Permissions

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