title: Java Matching Game
body: <p>I am writing a simple GUI matching game using classes that derive from JButton. I have created a grid of buttons. I have a Card class that contains the color of the card and it "flips" card over by changing the background color. I have created Card1 and Card2 in a list that match each other in their background color. I am having a hard time when I am trying to match my card and also when I am trying to delay the cards from turning back over. After two cards have been selected, there is a 1 second delay and then the cards are supposed to turn back over, but whenever i add that code the second card never displays it color. I am using Netbeans as by IDE if that makes a difference</p>
  
  <p>Here is my Card.java</p>
  
  <pre><code>package mygui;
  
  import java.awt.Color;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Random;
  import javax.swing.JButton;
  
  
  interface FlipListener
  {
      void flipped(Card card);
  }
  
  public class Card extends JButton{
      private List&lt;FlipListener&gt; listeners = new ArrayList&lt;FlipListener&gt;();
      private Color backColor;
      public final Color originalColor;
  
      public void AddListener(FlipListener listener)
      {
          listeners.add(listener);
      }
  
      public Card(Color color)
      {
          originalColor = getBackground();
          backColor = color;
          //Click action Listener
          addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  if(getBackground().equals(backColor))
                  {
                      setBackground(originalColor);                    
                  }
                  else
                  {
                      setBackground(backColor);
  
                  }
  
                  for(FlipListener flipListener: listeners)
                  {
                      flipListener.flipped((Card)e.getSource()); 
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>Here is my FormMain.java</p>
  
  <pre><code>package mygui;
  
  import java.awt.Color;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  import java.util.Random;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.swing.JButton;
  
  
  public class FormMain extends javax.swing.JFrame{
      //Creates the form and adds the cards
      public FormMain()
      {
          setSize(1000, 750);
  
          myCards = new ArrayList&lt;Card&gt;();
          for(int i = 0; i &lt; 18; i++)
          {
              Random r = new Random();
              Color color = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
              Card card1 = new Card(color);
              Card card2 = new Card(color);
              card1.setText("Card1");
              card2.setText("Card2");
              myCards.add(card1);
              myCards.add(card2);
  
  
              card1.AddListener((card)-&gt;{
                  if(selectedCard1 == null)
                  {
                      selectedCard1 = card;
                      selectedCard1.setText("Card 1 selectedCard1");
                  }
                  else if(selectedCard2 == null)
                  {
                      selectedCard2 = card;
                      selectedCard2.setText("Card 1 selectedCard2");
  
                      //check for match with selectedCard1
                      if(selectedCard1.getBackground() == selectedCard2.getBackground())
                      {
                          selectedCard1.setText("Clicked SC1");
                          selectedCard2.setText("Clicked SC2");
                        Color black = new Color(0,0,0); 
                        card1.setBackground(black);
                        card2.setBackground(black);
  
                      }
  //                    if(selectedCard1 == card1) //works only one way
  //                        //I am selecting myself
  //                    {
  //                        Color black = new Color(0,0,0); 
  //                      //card2.setBackground(black);
  //                      card1.setBackground(black);
  //                      
  //                    }
  
                      try {
                          //delay for 1 sec
                          Thread.currentThread().sleep(1000);
                      } catch (InterruptedException ex) {
                          Logger.getLogger(FormMain.class.getName()).log(Level.SEVERE, null, ex);
                      }
  
                      //flip the cards back over
  
                      selectedCard1.setBackground(card1.originalColor);
                      selectedCard2.setBackground(card1.originalColor);
                          selectedCard1 = null;
                          selectedCard2 = null;
  
                  }
              });
  
               card2.AddListener((card)-&gt;{
                  if(selectedCard1 == null)
                  {
                      selectedCard1 = card;
                      selectedCard1.setText("cArd 2 selected card 1");
                  }
                  else if(selectedCard2 == null)
                  {
                      selectedCard2 = card;
                      selectedCard2.setText("Card 2 selected card 2");
  
                      //check for match with selectedCard1
                     if(selectedCard1.getBackground() == selectedCard2.getBackground())
                      {
                          Color black = new Color(0,0,0); 
                        card1.setBackground(black);
                        card2.setBackground(black);
                      }
  
  
                      try {
                          //delay for 1 sec
                          Thread.currentThread().sleep(1000);
                      } catch (InterruptedException ex) {
                          Logger.getLogger(FormMain.class.getName()).log(Level.SEVERE, null, ex);
                      }
  
                     // flip the cards back over
                      selectedCard1.setBackground(card2.originalColor);
                      selectedCard2.setBackground(card2.originalColor);
                          selectedCard1 = null;
                          selectedCard2 = null;
                  }
              });
  
          }
          Collections.shuffle(myCards);
          for (Card card : myCards)
          {
              add(card);
          }
  
  
          GridLayout layout = new GridLayout(6,6);
          getContentPane().setLayout(layout);
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      }
  
      public static void main(String[] args)
      {
          java.awt.EventQueue.invokeLater(new Runnable(){
  
              @Override
              public void run() {
                  new FormMain().setVisible(true);
              }
  
          });
  
  
  
  
  
  }
  
      private ArrayList&lt;Card&gt; myCards;
      Card selectedCard1;
      Card selectedCard2;
  
  }
  </code></pre>