How would I restart a class?

I’m finished my noughts and crosses game, almost. I gave the option to restart or quite the game after the game ends.

How would I restart my game?
I tried restarting the applet (it’s in a JFrame) by calling destroy and then init method.
Doesn’t work.

Any ideas?

Thanks

Separate your frame/applet code and game code clearly. So when restarting, just re-create or reset the game stuff.

Exactly.
Try to think of what the Applet actually is: An online application embedded in a web page, nothing more, nothing less.
You don’t want to restart the Applet, you want to restart your game, not the whole applet. Try to refactor your code, so you can simply call Game.start() or something and that your class extending Applet will be dumb enough to just show what your ‘Game’ class does without knowing much else.
A (shaky) analogy: When you start an application on your PC, you’re not restarting your whole PC, are you?

(meant as constructive criticism :))

Erik

Thanks, I think I know where you guys are going with this. :slight_smile:

I do have my class constructor, I use that to start my program.
I need a stop method that will act as a destructor.
I will experiment with this.

Your starting object that is called by the applet is like any other object. If you return out of it and there are no other references it becomes a candidate for collection.

Not that you really should care, all you should really need to do is return any resources it grabbed and exit abck to your launch code.

This can be complciated IF you are using static varibales anywhere. These will need to be set back to their initial states by your code.