Archived

This topic is now archived and is closed to further replies.

Eelco

[java] Java program flow

Recommended Posts

Ive recently started Java, and ive been trying to create a simple app to create a simple game in. so i have an application class, with a main method. it does nothing but create a window (Jframe) so i construct this window with all sorts of gadgets. but then i have to let go of the program flow, cos my main method ends. i cant create a mainloop cos then my GUI hangs. my window still runs though, on some lower level, and can be interacted with using events. but if i wanted to create a pacman game for example, how would i update AI? i dont have control over the program flow, unless an event occurs. but i just want to get control over the programflow once every frame. so my question is: is there some abstract method in one of the superclasses of the jframe that i must overload, which is always called each frame? or is there some other way? i cant seem to find it.

Share this post


Link to post
Share on other sites
ok i think ive got something...

ive made my main class a thread, and have given it a mainloop with a yield() command in it.

is this a usual/good way of working?

Share this post


Link to post
Share on other sites
It does not to be in a thread of its own. They UI is updated in a separate thread. You can always do:

public class PacMan extends JFrame {
private boolean done = false;
public PacMan() {
//all sorts of gadgets

}

public void runGame() {
//will not exit until you tell it to

while(!done) {
getInput();
update();
...
render();
}
}

public static void main(String args[]) {
PacMan p = new PacMan();
p.show();//returns right away

p.runGame();
}
}


It depends how you want to do it. You can run a separate thread, but you don''t have to.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
quote:

They UI is updated in a separate thread.


uhm yeah but if i dont pass control to that thread how is it going to do its thing?

Share this post


Link to post
Share on other sites
quote:

uhm yeah but if i dont pass control to that thread how is it going to do its thing?


It just *knows*. Freaky, huh?

Share this post


Link to post
Share on other sites
quote:
Original post by Eelco
uhm yeah but if i dont pass control to that thread how is it going to do its thing?


The JVM has complete control over the entire system. Your program is started in its own thread. Then one is start for UI, Garbage Collection, etc... All you have to do is subclass a visual component and override paint() (or paintComponent() for Swing) and your custom drawing will be shown.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
arrgh!! all this implicitness is killing me!

it just doesnt makes sense. i type code, and i expect it to follow what i typed, not do another thousand things and do what i actualy intended when it suits java.

if this wasnt for uni id never touch java again :S

Share this post


Link to post
Share on other sites
quote:
Original post by Eelco
arrgh!! all this implicitness is killing me!

it just doesnt makes sense. i type code, and i expect it to follow what i typed, not do another thousand things and do what i actualy intended when it suits java.

if this wasnt for uni id never touch java again :S


Don''t blame Java, it would be nearly the same if you were using any other gui toolkit; c# or MFC or gtk, wxwidgets or whatever. MFC and wxwidgets may not be multithreaded, but your gui code doesn''t execute in a linear manner (well it does after its called).

Share this post


Link to post
Share on other sites
you do get control. first you have to realize that java runs on a virtual machine...i know you know that, but the computer you run a c or c++ program on also runs all sorts of things you have no control over, including memory management...java has to take care of the same stuff *and* manage integrating its "ideal" machine with the actual one.

second, by instantiating a JFrame, you are implicitly starting a few threads, but you are *explicitly* doing this by instantiating a JFrame. if you dont want to do that, make your own widget or write a console app...thats one of the pitfalls of using a library already written for you, you have to know it if you want complete control.

java's event and thread control scheme is quite well done (except for some early awt misfires), but like any language/libary you have to know it to have complete mastery. yes, you won't become a brilliant java programmer in a day.

[edited by - justo on May 25, 2004 2:04:47 PM]

Share this post


Link to post
Share on other sites