Forcing repaint in a loop with a delay - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Forcing repaint in a loop with a delay

TCaldwellTCaldwell Posts: 1Member
Hi all,
I am writing a game for my first java program. I want to paint each time a loop executes with a small delay in between to create a cascade effect on screen. I believe that the effeciency routines in the system are collapsing the calls to repaint() into a single call. At any rate, the changes all occur at once after the total delay. Is there some way to force the system to repaint immediately and then delay on each iteration of the loop?

Here is the loop code:

for (row=row;row<gridRows-1;row++) {
if (row == gridRows - 1 || col == 0) break;
if (tiles[row+1][col-1] == null) break;
Tile14.move(tiles[row+1][col-1],tiles[row][col]);
repaint();
try {
Thread.sleep(200);
}
catch (InterruptedException e) {}
col--;
}
Tile14.move(holdTile,tiles[row][col]);
repaint();

Any suggestions would be appreciated.

Comments

  • Undead_ZeusUndead_Zeus Posts: 4Member
    : Hi all,
    : I am writing a game for my first java program. I want to paint each time a loop executes with a small delay in between to create a cascade effect on screen. I believe that the effeciency routines in the system are collapsing the calls to repaint() into a single call. At any rate, the changes all occur at once after the total delay. Is there some way to force the system to repaint immediately and then delay on each iteration of the loop?
    :
    : Here is the loop code:
    :
    : for (row=row;row<gridRows-1;row++) {
    : if (row == gridRows - 1 || col == 0) break;
    : if (tiles[row+1][col-1] == null) break;
    : Tile14.move(tiles[row+1][col-1],tiles[row][col]);
    : repaint();
    : try {
    : Thread.sleep(200);
    : }
    : catch (InterruptedException e) {}
    : col--;
    : }
    : Tile14.move(holdTile,tiles[row][col]);
    : repaint();
    :
    : Any suggestions would be appreciated.
    :
    What makes the loop execute? I had a problem in my Breakout game where if you moved the mouse cursor everything went slow. I fixed by only letting it update every 5th cycle[code]if(toggle==5)
    {
    //move everything
    }
    else
    {
    toggle++
    }[/code]
    Maybe (if the situation allows) you can put a toggle system in?
  • SnookerLoopySnookerLoopy Posts: 41Member
    : Hi all,
    : I am writing a game for my first java program. I want to paint each time a loop executes with a small delay in between to create a cascade effect on screen. I believe that the effeciency routines in the system are collapsing the calls to repaint() into a single call. At any rate, the changes all occur at once after the total delay. Is there some way to force the system to repaint immediately and then delay on each iteration of the loop?
    :
    : Here is the loop code:
    :
    : for (row=row;row<gridRows-1;row++) {
    : if (row == gridRows - 1 || col == 0) break;
    : if (tiles[row+1][col-1] == null) break;
    : Tile14.move(tiles[row+1][col-1],tiles[row][col]);
    : repaint();
    : try {
    : Thread.sleep(200);
    : }
    : catch (InterruptedException e) {}
    : col--;
    : }
    : Tile14.move(holdTile,tiles[row][col]);
    : repaint();
    :
    : Any suggestions would be appreciated.
    :

    I was pulling my hair out over the same issue a while ago - until I just gave in and placed a large chunk of "control" code actually inside the paint() method.

    There are limitations to this way of doing things, but as a quick fix it certainly worked :-)

    Matty
    [email protected]
Sign In or Register to comment.