Java2D painting transparent overlay pause menu

I’m making a pacman clone and i’m trying to make a pause menu which is just painted on top of the game with transparency to look cool, problem is it paints over and over again making the menu opaque.

To stop this i can continue to paint the normal game underneath the overlay but if i do this it makes things animate as i call player.drawImage(g2d) which then goes and makes the player move onto next animation frame even though the game is paused, so i really don’t want to run the drawing code for the normal game at all.

I tried ‘screen shoting’ the game when i pause it making a copy of the buffer and storing in a buffered image drawing that and then the transparent menu to avoid calling the draw methods for tiles and player but this didn’t seem to work very well, i think it was the way i copied the image via bufferedimage.getRaster(), i also tried just doing pauseBuffer = buffer type of copying but this didn’t seem to work. Anyone know anyway i can make this work without having to add loads of checks in individual classes draw code to check if the game is paused?

If by just calling draw method results to animation, then you did it wrong. You injected logic part into your draw method.


private void update(long delta){
  if (!paused){
//continue game logic
  }
}

private void draw(Graphics g){
    //draw everything
    if (paused){
        //draw that menu
    }
}

aghh ok, i see. It was only getImage() being called in like the player draw method which went and counted to a next animation frame but i guess i could put it in a update method instead, thanks very much :smiley:

Make that getImage() really throws an image, do nothing more like index increment. Rather, do the increment on update(). You need delta to control the animation don’t you?

Not using delta as i’m using a fixed time step loop i believe, don’t really understand it. Its the only part of this clone i havent written i got it from a tutorial on here i plan to try and understand it one day :stuck_out_tongue:

It’s still same. On fixed time step loop you can skip part where you check how much time passed since last update of animation’s frame. You still need to separate logic and render. After that, you can try my pseudo-code.

okay i’m separating out the logic and rendering code completely into two different methods, should make life easier. I’m using an abstract super class for all my tiles so its an easy thing to add.

Can you link me your pseudo-code? cause it would be nice not to have to write timers everywhere :slight_smile:

Code on my first post is.

ohhhh okay i get you, think ill have the paused variable in the class thats calling those methods though e.g.


if(paused){
  player.update();
  map.updateTiles();
}

Yeah. Logic should updates when not paused. But game screen always renders, only the paused menu that makes difference.