🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.programmer
Article #641

Re: The best synchronized Paint!

#641
From: David Holmes
Date: Wed, 24 Mar 1999 00:00
89 lines
3994 bytes
Kim Frederiksen wrote in message <36F75CF1.37367641@greydirect.dk>...
>I've been working on getting my paint synchronized as good as possible,
>this is what I've come up with, can you think of something that would
>work even better, let me know :-)


If you *really* think you need to do this then you should make some changes.

First off repaint() results in update() being invoked so put the
notification in there. That way you don't synchronize paint() which is
invoked much more often than update.

However a warning. repaint() is not guaranteed to have update() invoked as
the system can swallow the repaint request if it decides that nothing needs
repainting. To combat this use a timed version of wait().

Don't synchronize run() as that will prevent paint() from being invoked even
when your thread is sleeping. Simply use a synchronized block of an
appropriate form.

You should always wait() in a loop, to avoid problems with spurious
wakeups - though in this case you don't care that much.

Use long's not int's otherwise you'll have problems on machines that
actually stay up for a reasonable length of time. :)

Your timing logic could be simpler. You want to see if the elapsed time is
greater than the 'interval' and if not you want to sleep for the difference.

Below is a revised version of your code. Note that all this timing stuff is
pretty loose anyway as scheduling can make your calculations meaningless.
Given that its not surprising that most simple animations just do a
repaint() followed by a sleep().

David
------------------------------------------

long appletSpeed=30; // Max wait time in milliseconds
int signals = 0;     // flag to signal repaint has been done
long timeout = 2000; // timeout incase repaint() gets ignored

public void run (){
   long lastPaint = 0;
   while (true){
mainloop:
      lastPaint = System.currentTimeMillis();
      repaint();
      synchronized(this){
         int origSignals = signals;
         while(true){
            try { wait(timeout); } catch(InterruptedException ie){}
            // either signalled or timedout or spurious
            if (origSignals != signals) break; // notified
            else {
                long now = System.currentTimeMillis();
                if (now - lastPaint > timeout)

                   // we timed out which probably means the repaint
                   // was ignored, so restart the main loop
                   continue mainloop;
                }
                // else spurious so re-wait
            }
         }
      }
      // we were signalled so see if enough time has elapsed
      // since the last repaint
      long now = System.currentTimeMillis();
      long sleepTime = (now - lastPaint) - appletSpeed;
      if(sleepTime>0){
         try {
           Thread.sleep(sleepTime);
         }
         catch(InterruptedException e) { }
      }
   }
}

public void update(Graphics g){
   paint(g);
   synchronized(this){
      signals++;
      notifyAll(); // notify() will do if only ever one thread
   }
}

public void paint(Graphics g){
   // paint as appropriate
}
Technical Details
Message-ID: <7d9jcc$ki5$1@sunb.ocs.mq.edu.au>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36F75CF1.37367641@greydirect.dk>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f9a23,e7dc548027defe93
X-Google-Attributes: gidf9a23,public
X-Google-Thread: 100145,e7dc548027defe93
X-Google-Attributes: gid100145,public
X-Google-Thread: f5e83,e7dc548027defe93
X-Google-Attributes: gidf5e83,public
X-Google-Thread: 10a146,e7dc548027defe93
X-Google-Attributes: gid10a146,public
From: "David Holmes" <dholmes@mri.mq.edu.au>
Subject: Re: The best synchronized Paint!
Date: 1999/03/24
Message-ID: <7d9jcc$ki5$1@sunb.ocs.mq.edu.au>
X-Deja-AN: 458222072
References: <36F75CF1.37367641@greydirect.dk>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0
Organization: Microsoft Research Institute
Newsgroups: comp.lang.java.programmer,comp.lang.java.softwaretools,dk.edb.programmering.java,microsoft.public.java.visualj++