Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23994

Re: The invokeLater loop

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: The invokeLater loop
References <invokeLater-loop-20130510204735@ram.dialup.fu-berlin.de> <YJbjt.18$fS4.15@newsfe08.iad> <looping-20130510213759@ram.dialup.fu-berlin.de>
Message-ID <uCdjt.437$Dd2.356@newsfe29.iad> (permalink)
Date 2013-05-10 14:26 -0700

Show all headers | View raw


On 5/10/13 12:40 PM, Stefan Ram wrote:
> Daniel Pitts <newsgroup.nospam@virtualinfinity.net> writes:
>> It is a good way to eat up CPU cycles if you don't want to have your CPU
>> go idle.  Also known as a laptop warmer.
>
>    Say, I am writing an implementation of the »Game Of Life«. I have an
>    endless main loop whose contents updates to the next generation.
>
>    I want to have a sequence like:
>
>        - calculate next generation
>        - update screen
>        - calculate next generation
>        - update screeen
>        - calculate next generation
>        - ...
>
>    I think that I can do this by calculating the next generation and
>    updating the screen in »paintComponent« and then »invokingLater«
>    paintComponent or requesting a screen update which indirectly will
>    call »paintComponent« again. This will give the sequence above.
Depending on the type of simulation, you might not actually want to wait 
for the screen to update before continuing. This is actually 
particularly true in multi-player games.  GoL may be an exception of 
course, since typically users do want to see every frame.

By the way, calling "repaint()" doesn't mean the screen is updated. 
You'd have to be actively updating the screen each loop.

>    Another possibility might be to calculate the next generation in
>    a separate thread (SwingWorker). But I am not sure how to get
>    the above sequence (without waiting an arbitrary period of
>    times or complicated handshaking across thread boundaries).
>
Calling SwingWorker.invokeAndWait() isn't that complicated of a boundary 
IMO.

public class GoLMain {
    public static void main(String...args) {
       GameOfLifeSimulation gols = new GameOfLifeSimulation();
       GolUI ui = initUi();
       while (true) {
          Generation g = gols.nextGeneration();
          SwingUtilities.invokeAndWait(ui.generationPainterFor(g));
       }
    }
}


Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

The invokeLater loop ram@zedat.fu-berlin.de (Stefan Ram) - 2013-05-10 18:54 +0000
  Re: The invokeLater loop Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-10 12:18 -0700
    Re: The invokeLater loop Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-10 14:26 -0700
    Re: The invokeLater loop Joerg Meier <joergmmeier@arcor.de> - 2013-05-10 23:28 +0200
    Re: The invokeLater loop Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-10 17:39 -0400
    Re: The invokeLater loop Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-05-10 23:50 +0200
  Re: The invokeLater loop markspace <markspace@nospam.nospam> - 2013-05-10 14:51 -0700
  Re: The invokeLater loop Silvio <silvio@internet.com> - 2013-05-11 00:26 +0200
    Re: The invokeLater loop Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-10 15:52 -0700
  Re: The invokeLater loop Roedy Green <see_website@mindprod.com.invalid> - 2013-05-10 23:27 -0700
    Re: The invokeLater loop "Qu0ll" <Qu0llSixFour@gmail.com> - 2013-05-12 19:44 +1000
  Re: The invokeLater loop Knute Johnson <eternal@knutejohnson.com> - 2014-01-30 20:48 -0800

csiph-web