You could use a state engine, which I believe is pretty much standard for this. I don’t know how advanced your knowledge of Java is or anything, but it simplifies a lot of things and allow you to add as many different “states” as you want without having to change the main loop. It’s pretty easy, but it relies heavily on object orientation, but as you have experience with C/C++ it should be a piece of cake.
Create a small State interface:
package somewhere.meaningful;
public interface State{
public void update();
}
Then create a class that implements this interface for each “state” in the game; main menu, playing, pause menu, options menu, ending credits, e.t.c.
package somewhere.meaningful;
public class MainMenu implements State{
public void update(){
//Do main menu stuff
}
}
package somewhere.meaningful;
public class GameRunning implements State{
public void update(){
//Do game stuff
}
}
E.t.c.
Your main class then looks something like this:
private State state; //Remember to set state to something in the constructor of the main class
private boolean running = true;
public void gameloop(){
while(running){
state.update();
}
}
//Use this method to switch between states
public void setState(State state){
this.state = state;
}
//Use this method to end the game
public void endGame(){
running = false;
}
You also need to let each State keep a reference to the main class so they can actually access the setState() and endGame() functions.