Page 1 of 1

[Java] Pausing a while loop

Posted: Fri 24 Apr, 2009 1:08 am
by Wesley
Okay, I have a game called Pig I've been working on. It's a dice game that has human players and computer players. When a computer player goes, it simply rolls the dice until it is told to stop (like if it loses the round). I want to set a delay for each time the computer rolls the dice so it doesn't simply go 10 times before passing the dice.

I've tried Thread.sleep(delay), but then the GUI doesn't update the information (like the current roll on the dice). I conducted an experiment and found that if I use a JOptionPane to make the pause, the GUI updates perfectly.

I want to make the program produce the same results, but without the JOptionPane being present.

Any ideas?

Oh, I also tried using a Timer, but it produced the same results as the Thread.sleep().

Here's the code for the computer:

Code: Select all

if (comp) {
	while (!pass) {
		if (computerScore+roundScore>=GOAL) {
			status = name+" has won the game!";
			update();
			end=true;
		} else {
			if (computerScore+roundScore>=humanScores+TURNOVER) {
				status = "The "+name+" has turned over the dice by default.";
				update();
				JOptionPane.showMessageDialog(null, status);
				status = "";
				pass=true;
			} else {
				rollDice();
				if (losePoints()) {
					computerScore = 0;
					roundScore = 0;
					pass=true;
				} else
					if (loseRound()) {
						roundScore = 0;
						pass=true;
					} else
						roundScore += points;
				//I want to use a Timer here, instead of JOptionPane.
				update();
				JOptionPane.showMessageDialog(null, name+" has rolled: "+toString());
				System.out.println(toString());
			}
		}
	}
}

Re: [Java] Pausing a while loop

Posted: Fri 24 Apr, 2009 9:06 pm
by darkstone knight
<---- yava noob

from another thread:
King Harold wrote:Same thing probably, but maybe you could just put a Sleep in it somewhere, that would help.. Just sleep for 1 milisecond or something that's already a whole lot (seeing as most instructions take a fraction of a nanosecond..)
solution: use sleep(delay)
?

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 12:41 am
by Wesley
darkstone knight wrote:solution: use sleep(delay)
Ugh, I already explained that using a Thread won't work properly. Something is up with the graphics, they won't redraw when using sleep(delay).

Any other ideas?

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 7:54 am
by darkstone knight
use a small delay (20ms) and use that repeatly

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 10:11 am
by benryves
It seems that you are pausing the UI thread, hence it locking up during the pause. Pause in a separate thread to the UI one and all should be good.

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 3:15 pm
by Wesley
benryves wrote:It seems that you are pausing the UI thread, hence it locking up during the pause. Pause in a separate thread to the UI one and all should be good.
How do I pause in a separate thread? I don't know anything about threads. This project I'm working on is totally on my free time, not an assignment. Hence, I'm not learning (or haven't learned), in the classroom, about a lot of the things I'm implementing in the program.

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 3:58 pm
by benryves
I don't know what assignments have to do with anything?

I am not familiar with Java's class library, I'm afraid. Program execution happens on threads, where only one thing can happen at a time, which is why sleeping the UI thread causes your app to freeze.

The Timer class looks ideal as that uses a background thread to schedule events.

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 9:06 pm
by Wesley
Oh, I was just explaining that I don't understand the thread or UI information you were throwing out.

Hm... I also tried a Timer, but it didn't work. Maybe asking this question at the Sun forums would help :idea:.

Re: [Java] Pausing a while loop

Posted: Sat 25 Apr, 2009 10:47 pm
by benryves
I suppose so. :( Sorry I couldn't be of any more help!

Re: [Java] Pausing a while loop

Posted: Sat 02 May, 2009 11:11 pm
by Wesley
Okay, I understand the threads more now. And I asked some questions on the Sun forums. Sadly, the people there aren't very nice and are VERY impatient. Worst of all, they couldn't answer my question. I wish I could speak with a Sun developer...

Anyway, I'm trying to figure out how to use Thread.sleep(delay) on all threads at once; or, at least, to target certain threads, so the function doesn't lock up the GUI thread only.

Ugh...

Re: [Java] Pausing a while loop

Posted: Sun 03 May, 2009 12:57 am
by benryves
You have to call Thread.sleep(delay) inside the thread you wish to sleep, as it only works on the currently executing thread.

The computer's turn is a sequence of operations. When it's the computer's turn to play, create a thread that runs this sequence of operations (roll dice, pause, make another turn &c - I don't know how the game works, sorry). I assume that at the moment when the computer plays the code runs directly alongside the code that handles the user interface - you need to take this game code and shunt it out into its own thread.

Re: [Java] Pausing a while loop

Posted: Sun 03 May, 2009 1:24 am
by Wesley
Ben, you are such a great help!

My only problem is trying to place the Thread.sleep(delay) method. I have been trying to place it in different places and have been testing, but the way my program works, I think I will have to edit it quite a bit just so I can get the delay to work properly.

Thanks again! I will work on it some more.