IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Need help in Java: How to match two pictures?

4 posters

Go down

Java Need help in Java: How to match two pictures?

Post by bit0223-sujon Thu Oct 28, 2010 5:07 pm

I have used gridLayout to make some buttons. In those buttons I have used ImageIcon and setIcon(icon) method to set image for that buttons.
Now how will match 2 pictures if those 2 pictures are same? I have use used icon1.equals(icon2) method but it is not working.


Last edited by LordAmit on Thu Oct 28, 2010 9:11 pm; edited 1 time in total (Reason for editing : Use icons, and use the programming language name in topic title please)

bit0223-sujon
Global Moderator-RC

Course(s) :
  • BIT

Posts : 98
Points : 218

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0122-Amit Thu Oct 28, 2010 9:10 pm

Sujon, please use the appropriate icon of topic when you are posting in the programming zone Very Happy
Also, use the keyword Java in your topic title, so that we can find it when we use the search functionality of this forum.

Now, helping you...
Instead of giving you a code that should help you, lets check the definition of equals method.

JavaDoc wrote:
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
As you can see, icon1 and icon2 are not same objects. Which is why it is saying that they are not same.

you should use button.getIcon() method instead, which returns the image it is holding.

Here is the explanatory code. I am posting the same code twice. Please tell me which one looks better.

version 1:

Code:
      ImageIcon icon1 = new ImageIcon("src/halum/lesson.gif");
      ImageIcon icon2 = new ImageIcon("src/halum/lesson1-1.jpg");
      
      JButton button1 = new JButton(icon1);
      JButton button2 = new JButton(icon2);
      JButton button3 = new JButton(icon1);
      
      if(icon1.equals(icon2)) {
         System.out.println("This line ain't gonna be shown dude. Because...");
      }else {
         System.out.println("They ain't same object you know :| ");
      }
      
      if (button1.getIcon()==button2.getIcon()) {
         System.out.println("This is a better method. but They ain't same.");
      }else {
         System.out.println("Not gonna be printed");
      }
      
      if(button1.getIcon() == button3.getIcon()) {
         System.out.println("this is true");
         System.out.println("So this is what you should use");
      }else {
         System.out.println("Do we really need this else?");
      }
      
      if (button1.getIcon().equals(button3.getIcon())) {
         System.out.println("This should work as well. As both buttons use same image icon");
      }

version 2:

BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0107-Toma Fri Oct 29, 2010 4:31 am

As far as I know, when you are checking whether the values of two objects are same then you should use equals() function, if you are checking whether two objects are holding same reference then you should use "=="

bit0223-sujon wrote:I have use used icon1.equals(icon2) method but it is not working.
As you are checking whether the icons of the two buttons is same, first you should get the icon of the buttons and then check it.

Using button1.getIcon() you can get the Icon of the button. Now we need the content of the icon or value inside the icon. If you are comparing the to images by their name(the parameter inside ImageIcon object) you can use toString() to compare.

Code:
button1.getIcon().toString().equals(button2.getIcon().toString())
This will check the name(the parameter inside ImageIcon object) of the image inside the icons are same or not.


Last edited by BIT0107-toma on Fri Oct 29, 2010 4:34 am; edited 1 time in total (Reason for editing : correction of information)
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0122-Amit Fri Oct 29, 2010 5:42 am

A very nice method explained by Toma Very Happy And effective as well.

BTW Neutral
As far as I know, when you are checking whether the values of two objects are same then you should use equals() function

After going through 6 websites, and then experimenting myself, I think I should say that there is a slight problem in your statement.

When you are referring to objects, unless overridden, the equals method actually compares the references of them. So, it is the same as "==".

Here is what javadoc says:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

But this is not applicable for String type.
As javadoc says,

The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

I hope you will not misunderstand me Smile I am not trying to criticize you, but rather sharing what I just learned after reading your statement and thinking about it. And for giving me opportunity of thinking, I thank you. rep++

Here is the code I used for experimenting:
Code:
package halum;

public class Testing {
   private int a;

   public Testing(int b) {
      // TODO Auto-generated constructor stub
      a=b;
   }

   public static void main(String[] args) {

      Testing testing = new Testing(1);
      Testing testing2 = testing;
      Testing testing3 = new Testing(1);

      System.out.println("Object Part");
      System.out.println(testing == testing2);
      System.out.println(testing == testing3);

      System.out.println(testing.equals(testing2));
      System.out.println(testing.equals(testing3));

      System.out.println("String part");
      String aString = "Hi";
      String bString = aString;
      String cString = "Hi";

      System.out.println(aString == bString);
      System.out.println(aString == cString);

      System.out.println(aString.equals(bString));
      System.out.println(aString.equals(cString));

      System.out.println(aString.compareTo(bString));
      System.out.println(aString.compareTo(cString));

   }

}

BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0107-Toma Sat Oct 30, 2010 1:15 am

BIT0122-Amit wrote:When you are referring to objects, unless overridden, the equals method actually compares the references of them. So, it is the same as "==".
I am not telling that you are completely wrong. But I will request you to read this.
It says:
It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.
Here, "it" means "equals()" function.
It also says:
the equals() method makes a == test first, it can be fairly fast when the objects are identical. It only compares the values if the two references are not identical.
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0122-Amit Sat Oct 30, 2010 1:30 am

That is one of the 6 pages I went through last night Very Happy

If I am not totally wrong, then I am partially wrong according to you. And the correction you referred is,

It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.

Perhaps you missed this (now bold) part though:

When you are referring to objects, unless overridden, the equals method actually compares the references of them. So, it is the same as "==".

Overridden, user-defined... I can not really see the difference.

Anyway, I only posted that because

when you are checking whether the values of two objects are same then you should use equals() function,
where you didn't mention the exception.

Again, we were here considering ImageIcon objects. so, if we invoke the equals method on them directly, how are they going to compare them? What are the values?
By size? By name? Both of the combination? or something else?

which is why, ImageIcon only inherited the "==" comparison in equals() method.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0107-Toma Sat Oct 30, 2010 1:44 am

BIT0122-Amit wrote:Again, we were here considering ImageIcon objects. so, if we invoke the equals method on them directly, how are they going to compare them? What are the values?
By size? By name? Both of the combination? or something else?
That's my point. That's why I did not wrote "button1.getIcon().equals(button2.getIcon())"

If Sujon is using two different object with same image then "==" is not going to work and the button.getIcon().toString() function returns the name of the images and compares their value
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0122-Amit Sat Oct 30, 2010 1:52 am

BIT0107-toma wrote:
That's my point. That's why I did not wrote "button1.getIcon().equals(button2.getIcon())"

If Sujon is using two different object with same image then "==" is not going to work and the button.getIcon().toString() function returns the name of the images and compares their value

That's where you got it wrong there Neutral

When you are using the getIcon method, it returns the icon object which was used in the Icon holding object. (which is, in this case, a JButton)

So, if you use the same icon object in two buttons, and then use

Code:
button1.getIcon() == button2.getIcon()


It will work.

Example?
Code:

      ImageIcon icon1 = new ImageIcon("src/halum/lesson1-0.jpg");
      ImageIcon icon2 = new ImageIcon("src/halum/lesson1-1.jpg");
      
      JButton button1 = new JButton(icon1);
      JButton button2 = new JButton(icon2);
      JButton button3 = new JButton(icon1);
      
      if(icon1.equals(icon2)) {
         System.out.println("This line ain't gonna be shown dude. Because...");
      }else {
         System.out.println("They ain't same object you know :| ");
      }
      
      if (button1.getIcon()==button2.getIcon()) {
         System.out.println("This is a better method. but They ain't same.");
      }else {
         System.out.println("Not gonna be printed");
      }
      
      if(button1.getIcon() == button3.getIcon()) {
         System.out.println("this is true");
         System.out.println("So this is what you should use");
      }else {
         System.out.println("Do we really need this else?");
      }
      
      if (button1.getIcon().equals(button3.getIcon())) {
         System.out.println("This should work as well. As both buttons use same image icon");
      }


Edit:

I think I mentioned that your method in your first reply was a nice one.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0112-Rokon Sat Oct 30, 2010 7:47 pm

oi pula duita image kemne match korte hoi, dekhai dilam na?
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0122-Amit Sat Oct 30, 2010 9:09 pm

bit0112-rokon wrote:oi pula duita image kemne match korte hoi, dekhai dilam na?


একটু কষ্ট করে বাংলাটা বাংলায় লিখুন। Smile
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by bit0223-sujon Sat Oct 30, 2010 9:33 pm

@ Amit vaia & Toma apu-- A lot of thanks for helping me. Both this topic helped me to solve the problem.
@ Amit vaia-- at the 1st post the code you written in 2 formats- the 2nd one looks better and more comfrotable.
@ Rokon vaia -Yes the problem is now solved Very Happy

bit0223-sujon
Global Moderator-RC

Course(s) :
  • BIT

Posts : 98
Points : 218

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by BIT0122-Amit Sat Oct 30, 2010 9:48 pm

Can you kindly change the topic icon from java to solved? It will help people understand that this problem is solved Smile

Also, do not forget to rep++ when you want to thank someone Smile It encourages to help even more!

It is easy :p Just press the + button.

And thanks for providing the feedback. It is good to know that the second format looks better.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Need help in Java: How to match two pictures?

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum