2

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

Mjall2
  • 253
  • 1
  • 9
  • 24

1 Answers1

7

Use a JToggleButton. More specifically, use the setIcon and setSelectedIcon methods. Using this approach, you'll avoid reinventing the wheel.

EXAMPLE -

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

final class JToggleButtonDemo {
    public static final void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
    private static final void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout()); // For presentation purposes only.
        final JToggleButton button = new JToggleButton(UIManager.getIcon("OptionPane.informationIcon"));
        button.setSelectedIcon(UIManager.getIcon("OptionPane.errorIcon"));
        frame.add(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

The toggle button in this example will show an information icon when unselected and an error icon when selected.

mre
  • 43,520
  • 33
  • 120
  • 170
  • I just tried using the setSelectedIcon method (I am already using the setIcon method) Now the image does not change but only gets highlighted. – Mjall2 Feb 27 '12 at 18:49