Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java Modal Dialogs  (Read 759 times)
0 Members and 1 Guest are viewing this topic.
Offline Phil_Stein

Junior Newbie




Java games rock!


« Posted 2005-04-19 20:48:09 »

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.
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #1 - Posted 2005-04-20 15:56:20 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
..
 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.-
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2005-04-20 16:27:18 »

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. Smiley

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phil_Stein

Junior Newbie




Java games rock!


« Reply #3 - Posted 2005-04-22 15:47:43 »

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (45 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!