Results 1 to 4 of 4
Thread: Picture Matching Game
- 08-08-2009, 06:23 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Picture Matching Game
I have to create a 4x4 picture matching game where the objective is for the player to find all matching pairs of pictures.
How do I store my 8 images in an array and setup a loop for it?
Java Code:class displayHandler implements ActionListener { public void actionPerformed(ActionEvent e) { for (int i = 0; i < cardButton.length; i++) { if (cardButton[i] == e.getSource()) { cardButton[i].setIcon(null); cardButton[i].setEnabled(false); counter++; if (counter == 3) { if (sameValues()) { cardButton[btnID[0]].setEnabled(false); cardButton[btnID[1]].setEnabled(false); } else { cardButton[btnID[0]].setEnabled(true); cardButton[btnID[0]].setIcon(null); cardButton[btnID[1]].setEnabled(true); cardButton[btnID[1]].setIcon(null); } counter = 1; } if (counter == 1) { btnID[0] = i; //btnValue[0] = gameList.get(i); } if (counter == 2) { btnID[1] = i; //btnValue[1] = gameList.get(i); } } } } }
-
What I've done in this situation in the past (and I'm not saying it's the best solution, but it's worked for me), is create a MemoryButton class that actually is a JPanel that holds both a JButton and a JLabel using a CardLayout. When the button is pressed, the cardlayout swaps the button for the label, displaying the label. Each MemoryButton also holds an ImageIcon and an int "value" field (not necessary, I suppose) and has a setIcon method that simply sets the image icon of the JLabel held by these objects and setValue and getValue methods.
I then create a 4 x 4 grid of MemoryButtons and create an ImageIcon[8] array for my 8 ImageIcons, and do this only once at the beginning of the program. I have a reset method in the main class that creates an ArrayList<Integer> called intList, adds pairs of numbers 1-8 to this list (16 numbers in all), and then uses Collections.shuffle(intList) to randomize the list. Then I iterate through the list assiging to my MemoryButtons values and the ImageIcon from that array position that corresponds to that value.
- 08-09-2009, 10:04 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
thanks but do you mind going into more detail?
as of now, i have already set all my 16 buttons to a blank image via a loop.
how do i add the 8 images to an array and loop through them?
I also need to declare a method to assign images to the buttons.
similar to :
except i need to do this to images instead of integers.Java Code:public void setArrayListText() { for (int i=0; i<2; i++) { for (int ii=1; ii<(cardButton.length/2)+1; ii++) { gameList.add(ii); } } }
-
My way would require you to re-write your program from scratch using the I've described framework that I've described:
pseudo-structure
Java Code:class MemoryButton holds a CardLayout object holds a JPanel mainPanel that uses this cardlayout holds a JButton that is initially displayed in the cardlayout holds a JLabel that displays an ImageIcon that is held "behind" the JButton by the cardlayout method setIcon that sets the icon of the JLabel method showLabel( boolean) if true it tells the cardlayout to show the label, if false, the button method addActionListener (ActionListener) allows other classes to add an actionlistener to the button. It's the class that holds the buttons that will decide if there's been a match (continue to show the label) or not (show the label for x seconds, then show the button).
Similar Threads
-
matching and getting xml data
By Juuno in forum Advanced JavaReplies: 6Last Post: 04-26-2009, 06:25 PM -
Regex - matching literal characters
By racha0601 in forum Advanced JavaReplies: 3Last Post: 04-07-2009, 11:25 PM -
Help with signature matching
By cachi in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks