Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to change icon on JButton when clicked
    text
    copied!<p>I am attempting to make a memory card matching game known as Concentration. So far I have 3 classes. Memory extends JFrame implements ActionListener</p> <p>Board extends JPanel implements ActionListener</p> <p>Cell extends JButton</p> <p>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.</p> <p>I implemented ActionPerformed in this way, in the class Board.</p> <pre><code> 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(); } }} </code></pre> <p>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.</p> <pre><code>public void setIcon2(){ ImageIcon x = new ImageIcon(); x = getImageIcon(); setIcon(x); } </code></pre> <p>Here is the getImageIcon method in the class Cell.</p> <pre><code>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]; } </code></pre> <p>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).</p> <p>Tried to explain my predicament as clearly as possible, any sort of direction will be highly appreciated and valued. Thank you Mjall2</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload