Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

repaint() call doesn't get processed

Thread view: 
Esko Piirainen - 12 Nov 2005 16:09 GMT
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.
mgungora@gmail.com - 12 Nov 2005 17:51 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
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/

Roedy Green - 13 Nov 2005 02:38 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.