I'm doing a graphical interface to test different path finding algorithms.
Here is just a small test "algorithm" that I'm using to build up the
graphical methods I need. It starts from the start point and just moves
straight to the right until it reaches the end of the area (int[][]
squares).
I want to separate the path finding algorithms from counting different stuff
and drawing the graphics and that is why examineSquare() method is being
used.
Part of code from "path finder" :
.....
while (contents != FINISH)
{
if (x < maxX)
contents = examineSquare(x+1,y); // problems are with method used
here
else
{
noRoute();
break;
}
}
//route found
.....
Here is the examineSquare() method that causes problems:
private synchronized int examineSquare(int newX, int newY)
{
// Do some stuff with counters and manipulate the other
// variables that canvas uses with paint()
myCanvas.repaint();
// then I want the program to take a little break
try { wait ( theLenghtofDelay[speed] ); }
catch (Exception e) { System.out.println(e.toString()); }
x = newX;
y = newY;
return squares[x][y];
}
For some reason the program never executes the paint() method. Why?
Screen gets repainted only after the while loop has ended.
Same repaint() is used in many different parts of my program without
problems.
I'm not an expert on this but it seems that your wait() statement
executes on the same thread as the graphics update thread. The
repaint() method just places an "update" request on the main event
thread. Are you using Swing or AWT? Have you tried using
SwingUtilities.invokeLater() method? I think, what basically should
happen is, you should separate the main event thread and your
examinSquare() method executing thread, to allow the GUI to update
itself while your wait() is executing.
Hope this helps,
-murat
Esko Piirainen - 12 Nov 2005 19:32 GMT
Yes, perfectly clear now. I placed it to different thread and it's working.
Thanks!
> I'm not an expert on this but it seems that your wait() statement
> executes on the same thread as the graphics update thread. The
[quoted text clipped - 7 lines]
> Hope this helps,
> -murat
Thomas Hawtin - 14 Nov 2005 00:23 GMT
> I'm not an expert on this but it seems that your wait() statement
> executes on the same thread as the graphics update thread. The
[quoted text clipped - 4 lines]
> examinSquare() method executing thread, to allow the GUI to update
> itself while your wait() is executing.
Rather than just trying invokeLater randomly, it's better to have a very
clear idea what your threads are doing. You can indicate to the reader
whether you expect to be in the AWT Event Dispatch Thread (EDT) or not,
using assert EventQueue.isDispatchThread(); or assert
!EventQueue.isDispatchThread();. If you remember to use -ea or
-enableassertions, then the runtime will check your assertion for you. A
similar thing goes for Thread.holdsLock.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
On Sat, 12 Nov 2005 18:09:45 +0200, "Esko Piirainen"
<esko.piirainen@kolumbus.fi> wrote, quoted or indirectly quoted
someone who said :
> try { wait ( theLenghtofDelay[speed] ); }
it is impossible to paint while asleep.
See http://mindprod.com/jgloss/swingthreads.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.