0


I am building a game and I am trying to create "New game" button.
In the game I have thread that delays computer's move so I can see it step by step by using thread.sleep.
My problem is that I can't make a New Game until this thread is over.
If I manually stop it, I will get InterruptedException.
What can I do in order to do it?

My game is placed on JPanel which is placed on JFrame.
I tought that if I dispose the JFrame and create a new one, it will destroy the JPanel and all it's content and create a new one all over. but it's not working either.

Any suggestions??
Thanks in advanced,
Ron.

Ron537
  • 990
  • 1
  • 9
  • 20
  • __DO NOT CALL `Thread.stop()`__ it is massively deprecated and can cause inconsistent objects. Read [here](http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html). – Mordechai Apr 04 '13 at 19:15

1 Answers1

0

For stopping a Thread, see: https://stackoverflow.com/a/8581317/1178781

The InterruptedException is thrown if you stop a thread while it is sleeping. Just wrap the sleep call in a try...catch (with the catch doing nothing) if you do not want to handle this exception.

See the Java API: http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)

Also see: How to programmatically close a JFrame

Community
  • 1
  • 1
justderb
  • 2,835
  • 1
  • 25
  • 38
  • He could have added a `throws` statement for the method, making it not have to surround in a try...catch – justderb Apr 04 '13 at 19:10
  • It is wrapped but is it ok that I am getting this exception?
    It won't cause any problems later?
    – Ron537 Apr 04 '13 at 19:10
  • Yes, because you are interrupting a thread while it is sleeping. As long as you are expecting this and doing stuff with the exception (or with your case, nothing at all) you are fine. – justderb Apr 04 '13 at 19:12
  • So just leave the catch empty? – Ron537 Apr 04 '13 at 19:15
  • Yes, unless you would like to log the error, or do something else when it happens (but since you are using it for testing, I'd say don't do anything) – justderb Apr 04 '13 at 19:16