Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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
    matty@i-compute.net
Sign In or Register to comment.