Jump to content
  • Advertisement

Archived

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

Eelco

[java] Java program flow

This topic is 5234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!