• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to compare two cards of JButtons with ImageIcons in java?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how to do this and where to place it in the code, so I posted most of the code below. I'm trying to either make two cards disabled if they match after being pressed, or flip back over if they do not match. Additionally, the icons being random and trying to match them is where it gets extremely confusing for me.



 
Marshal
Posts: 59773
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Where are you comparing the classes? I presume the images are things like 3♠ or K♥ and similar. You haven't show how you are comparing them. You shouldn't be comparing things in the display anyway, so I presume you have a Card class with an image. There is an example of that sort of thing in the Java┬« Language Specification.
I'm afraid I don't like to see display component classes subclassed; you don't need a subclass of a frame, but you can use it as a local variable in the constructor. Also call setVisible last. Also don't try creating one listener class for all the buttons. The exit button should have a different listener from all the others.
Don't call System.exit. It might work all right in a simple application, but in a complicated app you can do serious damage by stopping execution suddenly. Call dispose() on the frame instead. You may wish to save the state of the game, which you would have to do before exiting the app; that might be impossible if you ise System.exit.
 
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The posted code has several compiler errors.  Please fix the errors and post code that compiles so it can be executed for testing.
 
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have 10 jpegs, and 20 icons pointing to these 10 jpegs (if I interprete your code correctly). So, if card1 and card2 are selected, you should be able to simply check if card1.getIcon() == card2.getIcon().
 
Bartender
Posts: 619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Card class should have an equals(Card c) method that compares card c to the current card.
You should not compare images; the Card class should have variables that specify the suit and the number, and the equals method just checks whether those two variables are the same for both cards.
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be the case if we have cards as in a poker game.The "cards" here are simple JButtons, that are equal when the icons are equal.
 
Johnn Smithfeeld
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted the MCVE

 
Norm Radder
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any questions about the MCVE code?  What was the purpose of posting it?
What is the loop starting on line 25 supposed to be doing?

The loop at line 36 could be merged with the loop at line 41.  As a button is added to the panel, its listener could be added.
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I got your latest code (the MCVE) up and running, where is the comparison part? You should keep two variables, namely the first and the second button clicked, and when the second button is clicked, then compare the icons of the two. Then start a Timer, for two seconds say, and when it fires, take the necessary action (removing the buttons if the guess was correct, or resetting the icons if not correct, et cetera).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!