I am attempting to make a memory card matching game known as Concentration. So far I have 3 classes. Memory extends JFrame implements ActionListener
Board extends JPanel implements ActionListener
Cell extends JButton
I have so far achieved a window to pop up. Use a list to add pairs of types of Cells. Distribute all the Cells randomly across my Board. Display the back (an img) of all Cells (there are 24 Cells, 4 rows 6 columns). Now when I click on my card I get a white image. At the moment as a short term goal, all I am trying to achieve is that when I click on a Button, the appropriate image shows up on the button.
I implemented ActionPerformed in this way, in the class Board.
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Cell){
Cell temp = (Cell)e.getSource();
temp.setSelected(true);
if (temp.selected()){
int row = temp.getRow();
int column = temp.getColumn();
board[row][column].setIcon2();
}
}}
My set selected methods only serves to change the value of a boolean variable in the Cell class to true. This is my setIcon2 method in the class Cell.
public void setIcon2(){
ImageIcon x = new ImageIcon();
x = getImageIcon();
setIcon(x);
}
Here is the getImageIcon method in the class Cell.
private ImageIcon getImageIcon() {
int temp=0;
int id;
if (localSelected) {
id = getType();
String tempId = Integer.toString(id);
icons[temp] = new ImageIcon("img-" + tempId + ".jpg");
temp++;
return icons[temp];
} else {
id = IMAGE_NUMBER;
String strId = Integer.toString(id);
icons[id] = new ImageIcon("img-" + strId + ".jpg");
}
return icons[id];
}
There are no compilation errors or warnings of any sort. The getType method returns an integer variable associated to a value stored in my game board. (2D array of type Cell).
Tried to explain my predicament as clearly as possible, any sort of direction will be highly appreciated and valued. Thank you Mjall2