-2

So I have absolutely no clue why this doesn't work-- but the GUI doesn't respond to anything inside the getMove(GameState gameState) method.

Even with a sleep to pause it after I make the move-- it simply does not show anything. Any help would be great. I'm so lost.

    public class Engine extends Player {

    private GameState copy;

    public Engine(Color color, Direction direction) {
        super(color, direction);
    }

    public Move getMove(GameState gameState) {
        gameState.executeMove(new Move(4, 6, 4, 4));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Move(5, 6, 5, 4);
    }
}
thePanthurr
  • 104
  • 1
  • 9

1 Answers1

2

You're calling sleep within a Swing GUI on its event thread, something that will block the event thread, preventing it from doing its necessary activities such as drawing the GUI and interacting with the user, and this will put the GUI to sleep making it non-responsive. The solution is to never do this but to use a Swing Timer instead.

Side issue: add a call to the super.paintComponent(g); method within your override method, on its first line. This will allow your JPanel to do its house-keeping painting.

  • Ah. Very useful. Thank you. Is there any quick and elegant way to pause the gui painting with one of these timers? From the looks of it it doesn't seem like it. Thanks again though ^.^ Will mark as solved. – thePanthurr Mar 27 '17 at 04:53
  • @thePanthurr: The Timer ***is*** the quickest most elegant way to pause or cause simple Swing animations. They're not hard to use -- just check the tutorial and give it a try. – DontKnowMuchBut Getting Better Mar 27 '17 at 04:54
  • Well the thing thats tripping me up is that the gui is not running on periodic loop to repaint it. It is being repainted after a move is made-- therefore it seems awkward to use a Swing Timer. Unless I'm completely misunderstanding how they are supposed to be used. – thePanthurr Mar 27 '17 at 05:01