Results 1 to 5 of 5
Thread: Comparing ImageIcons
- 03-10-2013, 01:38 AM #1
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Comparing ImageIcons
I am trying to make a matching game in java where the user flips over two images the program either disables those buttons or if the user gets it wrong the program resets the buttons.
I have most of the program working I am just having issues with comparing the images.
What I have right now is not working. I have also tried:Java Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; import static java.util.Collections.*; import javax.swing.ImageIcon.*; public class MatchingGame extends JFrame implements ActionListener { private JButton exitButton, replayButton; private JButton[] gameButton = new JButton[36]; private ImageIcon[] ImageArray; private ImageIcon[] images2; private int counter = 0; private int[] buttonID = new int[2]; private ImageIcon[] buttonValue = new ImageIcon[2]; public MatchingGame() { setLayout(new BorderLayout()); init(); panel(); setImages(); setTitle("MathingGame"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(900, 900); setVisible(true); } public void init() { for (int i = 0; i < gameButton.length; i++) { gameButton[i] = new JButton(); gameButton[i].addActionListener(this); } exitButton = new JButton("Exit"); exitButton.addActionListener(this); replayButton = new JButton("Replay"); replayButton.addActionListener(this); } public void panel() { Panel gamePanel = new Panel(); gamePanel.setLayout(new GridLayout(6, 6)); for (int i = 0; i < gameButton.length; i++) { gamePanel.add(gameButton[i]); } Panel buttonPanel = new Panel(); buttonPanel.add(replayButton); buttonPanel.add(exitButton); buttonPanel.setLayout(new GridLayout(1, 0)); add(gamePanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); } public void setImages(){ ImageIcon Aquaman = new ImageIcon ("Aquaman.jpg"); ImageIcon Batman = new ImageIcon ("Batman.jpg"); ImageIcon CaptainAmerica = new ImageIcon ("CaptainAmerica.jpg"); ImageIcon Deadpool = new ImageIcon ("Deadpool.jpg"); ImageIcon FantasticFour = new ImageIcon ("FantasticFour.jpg"); ImageIcon Flash = new ImageIcon ("Flash.jpg"); ImageIcon GreenArrow = new ImageIcon ("GreenArrow.jpg"); ImageIcon GreenLantern = new ImageIcon ("GreenLantern.jpg"); ImageIcon Hawkeye = new ImageIcon ("Hawkeye.jpg"); ImageIcon Ironman = new ImageIcon ("Ironman.jpg"); ImageIcon Punisher = new ImageIcon ("Punisher.jpg"); ImageIcon RedTornado = new ImageIcon ("RedTornado.jpg"); ImageIcon Robin = new ImageIcon ("Robin.jpg"); ImageIcon Spiderman = new ImageIcon ("Spiderman.jpg"); ImageIcon Superman = new ImageIcon ("Superman.jpg"); ImageIcon TheAvengers = new ImageIcon ("TheAvengers.jpg"); ImageIcon Thor = new ImageIcon ("Thor.jpg"); ImageIcon Wonderwoman = new ImageIcon ("Wonderwoman.jpg"); ImageIcon[] images = new ImageIcon[36]; images[0] = Aquaman; images[1] = Batman; images[2] = CaptainAmerica; images[3] = Deadpool; images[4] = FantasticFour; images[5] = Flash; images[6] = GreenArrow; images[7] = GreenLantern; images[8] = Hawkeye; images[9] = Ironman; images[10] = Punisher; images[11] = RedTornado; images[12] = Robin; images[13] = Spiderman; images[14] = Superman; images[15] = TheAvengers; images[16] = Thor; images[17] = Wonderwoman; images[18] = Aquaman; images[19] = Batman; images[20] = CaptainAmerica; images[21] = Deadpool; images[22] = FantasticFour; images[23] = Flash; images[24] = GreenArrow; images[25] = GreenLantern; images[26] = Hawkeye; images[27] = Ironman; images[28] = Punisher; images[29] = RedTornado; images[30] = Robin; images[31] = Spiderman; images[32] = Superman; images[33] = TheAvengers; images[34] = Thor; images[35] = Wonderwoman; java.util.List myList = Arrays.asList (images); Collections.shuffle(myList); images2 = new ImageIcon [myList.size()]; myList.toArray(images2); } public boolean sameValues() { if (buttonValue[0] == buttonValue[1]) { return true; } return false; } public void actionPerformed(ActionEvent e) { for (int i = 0; i < gameButton.length; i++) { if (gameButton[i] == e.getSource()) { gameButton[i].setIcon(images2[i]); gameButton[i].setEnabled(false); counter++; if (counter == 3) { if (sameValues()) { gameButton[buttonID[0]].setEnabled(false); gameButton[buttonID[1]].setEnabled(false); } else { gameButton[buttonID[0]].setEnabled(true); gameButton[buttonID[0]].setIcon(null); gameButton[buttonID[1]].setEnabled(true); gameButton[buttonID[1]].setIcon(null); } counter = 1; } if (counter == 1) { buttonID[0] = i; buttonValue[1]= images2[i]; } if (counter == 2) { buttonID[1] = i; buttonValue[1]= images2[i]; } } } if (exitButton == e.getSource()) { System.exit(0); } if (replayButton == e.getSource()) { setImages(); } } public static void main (String[] args){ new MatchingGame(); } }
Please help me.Java Code:public boolean sameValues() { if (buttonValue[0].toString().equals(buttonValue[1].toString())){ return true } return false; }
- 03-10-2013, 02:26 AM #2
Re: Comparing ImageIcons
How about comparing the references to the icons shown by the two buttons? JButton has a method that returns a reference to the icon it is showing.
If you don't understand my response, don't ignore it, ask a question.
- 03-10-2013, 02:35 AM #3
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
- 03-10-2013, 02:45 AM #4
Re: Comparing ImageIcons
Get the references to the icons for each button and use the == operator to see if they are the same.
If you don't understand my response, don't ignore it, ask a question.
- 03-10-2013, 04:56 AM #5
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
How to set an ImageIcons position variable while using an absolute layout?
By Alpa in forum AWT / SwingReplies: 3Last Post: 01-31-2012, 12:01 AM -
comparing Graphs and Comparing Matrix
By jetnor in forum New To JavaReplies: 0Last Post: 03-27-2011, 01:40 AM -
[SOLVED] I'm having problems using JButtons with ImageIcons and Actions. Any suggest
By Singing Boyo in forum New To JavaReplies: 1Last Post: 03-09-2009, 03:18 AM -
Problems with ImageIcons
By The_L in forum AWT / SwingReplies: 2Last Post: 10-24-2008, 02:04 AM -
comparing
By Feng in forum New To JavaReplies: 2Last Post: 11-23-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks