Java Modal Dialogs

A question for any Java gurus out there.

I’m working on a full screen Java game that basically blows off all the Java controls and panels and such. I use double buffered 2D graphics (using AWT), and my own library for implementing windows, buttons, and the like.

But I’m stuck on how to do modal dialogs. In C/C++, I do it like this

Normal Loop:
while (true)
{
update
draw
}

Dialog function
{
while (dialog not done)
{
Update
Draw screen behind dialog
Draw Dialog)
}
}

So basically, whenever a function invokes a dialog, the the Dialog function goes into an update/draw loop until the dialog is resolved, then returns to the calling function.

However, in Java, my main loop is not the infinite loop of {while (true){update;draw;}}, but rather the standard (AFAIK) Java method of a thread that looks like this:

public void update (Graphics g)
{
UpdateEverything();
Draw(g);
}

  while (true)
  {
        repaint();

        try
        {
              Thread.sleep (5);
        }
        catch (InterruptedException ex)
        {
              // do nothing
        }

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  }

So there’s an outer thread that calls repaint and goes to sleep, at which point Java calls the update() function

But when I try to implement this same logic in my DoDialog function, it doesn’t work - I call repaint and go to sleep, but it never calls my update() function. This is probably because I’m already IN an update function (the main update function for the whole game loop). So is there a way to get it to do what I want? (i.e. allow a modal dialog, of my own control, with an infinite loop pending dialog resolution, INSIDE of the main update loop).

Hopefully this wasn’t completely incoherent. Alternate suggestions as to how to implement modal dialogs are also welcome.

You should not calll repaint(), it ask for a repainting (in the awt thread), and may not do it inmediatelly.

My game loop (using active rendering) always begin from this template:


class Game extends Canvas implements Runnable{
 public void run(){
  while(true){
   doGame();
   doRender();
   doPaint();
  }
 }
 public void doGame(){
  //updates the game info
 }
 public void doRender(){
  //draws all the graphics into a buffer image
 }
 public void doPaint(){
  getGaphics().drawImage(buffer,....);
 }
 //those are for repaints called by awt Thread
 public void update(Graphics g){
  paint(g);
 }
 public void paint(Graphics g){
  g.drawImage(buffer,....);
 }
 //To start the game
 public void start(){
  Thread t = new Thread(t);
  t.start();
 }
}

For your problem, you can put some vars to indicate to the rendering stage to draw your dialog over the game.
You can use the same vars to skip the game execution in the doGame() method.

The changes are:


..
 public void doGame(){
  if(dialog){
   //process input for dialog
  }
  else{
   //do all the updates for the game
  }
 }
 public void doRender(){
  //render graphics to buffer
  if(dialog){
   //render dilaog to buffer
  }
 }

Hope it helps you.

Rafael.-

If you want a more traditional game loop then you can use a BufferStrategy on your display canvas/frame. Then your modal dialogs are easy and you can throw away those extra threads. :slight_smile:

rd - many thanks. I hadn’t realized that I could run a continuous game loop like that. I switched to do so (mirroring the logic I use for C++ games), and that made implementing the dialog trivially easy.

Thanks,
Phil