animation / timing issue

Hi guys first post.

ill try and explain this with pseudo code but will recreate the problem in actual code if i cant explain it very well.

this is just for a simple game im making.

baiscly i want this to happen

  • user clicks on the screen.
  • the screen then updates.
  • waits one second
  • then updates the screen again.

i think what happens when i implement this is the repaint requests get combined on the event dispatch thread and dont update the way it i thought when i wrote the code. but im not sure.

let me try some pseudo code


//class members
boolean background = false;
boolean square = false

paintScreen() {

  if (background == true) {
     paint the screen blue
  } else {
     paint the screen green
  } 

   is (square == true) {
   paint a square
   }


}

//when users clicks on screen run this function

clickOnScreenEvent() {

  square = !square;

  paintScreen();

  pauseProgram('1 second');

  background = !background;

  paintScreen();

  }

}

that’s basically what my program dose

the logic to me seems fine but it doesn’t work. basically you never see the square get painted. it just changes the background.

also i use Thread.sleep(1000); which im pretty sure locks up the EDT so yeah how would i go about implementing this in java ?

You could change your variables from booleans to longs and then, where you set them to true, call System.nanoTime().
In you check for true, you would get the current time with another call to System.nanoTime() and then subtract the two to get your current elapsed time. If it is still less then your threshold (one second) then your condition is true, otherwise, it is false.
No sleep to mess up the EDT.

i don’t think that will work i my game. there is no game loop.

in my game the screen actually only needs to repaint when the user clicks on the game screen or a button.

i could write a game loop easy tho. which would enable me to upgrade the game with animations but yeah i dunno. is that the correct way to go about it ? with the system time thing ?

ok i found a dead simple way around the issue.

i made a very basic game loop.

basically then i do this

  • click mouse on screen (change a boolean flag)
  • force repaint by calling paintImmediately
  • pause the gameLoop for 1 second(which repaints the screen)
  • change another boolean flag which causes diffrent things to be painted
  • gameloop now resumes updating the screen as normal

pretty sure this is kind of a simple work around that will not scale well if i want to add animations.
which i do want to do at some future stage. im happy for now but i think ill have to re write the whole game so it scales better. right now its dead simple. Its my first game.


if (playerOneTurn == true) {

                     paintImmediately(0, 0, getWidth(), getHeight());

                    try {
                        gameLoop.sleep(2000);  // pause for 2 seconds
                    } catch (InterruptedException ex) {
                    }

                    //this like filps the boolean to its opposite value in this case false so it will draw diffrent 
                    //background to the screeen once the gameloop comes out of its 2 second sleep

                    playerOneTurn = !playerOneTurn;