0

I'm trying to write a logic in memory game that when I click on cards and they are not a pair (different ID), program should swap them back after 1s. If they are same, then leave them as they are.

The problem is that when I first click and the card appears, after second clicking on another (different) card it doesn't appear and swap the first card after 1s. someone knows why the second card does not appear after clicking?

Btw when the pair is correct, everything works fine, here is my fragment of the code responsible for that logic in listener:

final int copy = i;

 card2.addActionListener((e) -> {
            card2.setIcon(new ImageIcon(icons[copy].getAbsolutePath()));

            if(firstClick == null)
            {
                firstClick = (Card)e.getSource();
            }
            else
            {
                Card secondClick = (Card)e.getSource();
                if(firstClick.getID() != secondClick.getID())
                {
                    try
                    {
                        Thread.sleep(1000);
                    } catch (InterruptedException e1)
                    {
                        //e1.printStackTrace();
                    }
                    firstClick.setIcon(new ImageIcon(background.getAbsolutePath()));
                    secondClick.setIcon(new ImageIcon(background.getAbsolutePath()));
                    firstClick = null;
                }
                else
                    firstClick = null;
            }

        });
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Tami
  • 1
  • 1
  • 1
    Possible duplicate [SwingWorker, Thread.sleep(), or javax.swing.timer? I need to “insert a pause”](https://stackoverflow.com/questions/16292498/swingworker-thread-sleep-or-javax-swing-timer-i-need-to-insert-a-pause/16293498#16293498) – MadProgrammer May 25 '18 at 09:47
  • 1
    I would also (strongly) recommend having a look at [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to Use Swing Timers](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) to better understand why your code won't work and possible the preferred solution – MadProgrammer May 25 '18 at 09:48
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 25 '18 at 14:57

1 Answers1

0

While method actionPerformed is executing, the GUI cannot react to mouse and keyboard events, so basically your code is "freezing" your GUI for one second. I believe that the class javax.swing.Timer is what you need and at first glance it looks like the duplicate question that MadProgrammer referred to may help you.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Bu shouldn't card2.setIcon(new ImageIcon(icons[copy].getAbsolutePath())); be done first before freezing? So the user will first see the card. – Tami May 25 '18 at 12:16
  • I can only guess an answer based on the limited source code you have supplied. Is `Card` a subclass of `javax.swing.JLabel` or `javax.swing.JButton`? – Abra May 25 '18 at 15:42