I assume you want to play cards that flip when pressed (but I'm not sure). Again, I recommend that you create your ImageIcons once and at the beginning of the program. Then you can easily compare if one icon matches the other using the equality method (...) or even in this situation the operator == . For example, look and run this code for an example of what I mean:
import java.awt.GridLayout; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; public class CardsDeck { public static final String RANKS = "a23456789tjqk"; public static final String SUITS = "cdhs"; public static final String CARDS_IMG_PATH = "http://math.hws.edu/javanotes/source/cards.png"; private static final int BACK_RANK = 2; private static final int BACK_SUIT = SUITS.length(); private static final String ICON = "icon"; private JPanel panel = new JPanel(); private List<ImageIcon> iconList = new ArrayList<ImageIcon>(); private ImageIcon cardBack; public CardsDeck() { try { URL imgUrl = new URL(CARDS_IMG_PATH); BufferedImage img = ImageIO.read(imgUrl); double cardWidth = (double) img.getWidth() / RANKS.length(); double cardHeight = (double) img.getHeight() / (SUITS.length() + 1); int w = (int) cardWidth; int h = (int) cardHeight; for (int rank = 0; rank < RANKS.length(); rank++) { for (int suit = 0; suit < SUITS.length(); suit++) { int x = (int) (rank * cardWidth); int y = (int) (suit * cardHeight); BufferedImage subImg = img.getSubimage(x, y, w, h); ImageIcon icon = new ImageIcon(subImg); iconList.add(icon); } } int x = (int) (BACK_RANK * cardWidth); int y = (int) (BACK_SUIT * cardHeight); BufferedImage subImg = img.getSubimage(x, y, w, h); cardBack = new ImageIcon(subImg); int hgap = 5; int vgap = hgap; panel.setLayout(new GridLayout(SUITS.length(), RANKS.length(), hgap, vgap)); panel.setBorder(BorderFactory.createEmptyBorder(vgap, hgap, vgap, hgap)); MouseListener mouseListener = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { JLabel label = (JLabel) e.getSource(); Icon currentIcon = label.getIcon(); if (currentIcon.equals(cardBack)) { Icon icon = (Icon) label.getClientProperty(ICON); label.setIcon(icon); } else { label.setIcon(cardBack); } } }; Collections.shuffle(iconList); for (int i = 0; i < iconList.size(); i++) { JLabel label = new JLabel(cardBack); label.putClientProperty(ICON, iconList.get(i)); label.addMouseListener(mouseListener); panel.add(label); } } catch (MalformedURLException e) { e.printStackTrace(); System.exit(-1); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } } private JComponent getPanel() { return panel; } private static void createAndShowGui() { CardsDeck cardsDeck = new CardsDeck(); JFrame frame = new JFrame("CardsDeck"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(cardsDeck.getPanel()); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
If you run it, it will display an array of 13 x 4 cards that you can click on them:

source share