Elton
Elton

Reputation: 93

Killing all processes, force everything to stop

I have a game that runs off of JPanel which has on it many other things which have their own independent timers and such. It seems that when I try to remove the panel from my frame to replace it with another JPanel it refuses to actually end all of its own processes. So even if I am able to remove it from the screen of the panel by removing it and setting it null, its processes are still going off in the background, IE the music and the stuff flying around.

What i need to know is some solution as to how to completely kill this JPanel and terminate its life to its entirety.

Seems not many people have run into this problem.

Upvotes: 0

Views: 666

Answers (2)

David Kroukamp
David Kroukamp

Reputation: 36423

I remember having that issue in my own game..

Simply create some custom method i.e destroy() which will stop all timers gameloops music etc.

i.e

MyPanel panel=new MyPanel();

...

panel.destory();//stop music, timers etc

frame.remove(panel);

//refresh frame to show changes
frame.revalidate(); 
frame.repaint();

where panel would be:

class MyPanel extends JPanel {

    private Timer t1,t2...;

    //this method will terminate the game i.e timers gameloop music etc
    void destroy() {
       t1.stop();
       t2.stop();
    }

}

Alternatively you could make your Swing Timers observers of sorts by making it check each time whether the panel is visible and if not it should stop executing. This would though now of course cause you to create a timer which will only start the others once the panel becomes visible:

class MyPanel extends JPanel {

    private Timer t1,t2,startingTimer;

    MyPanel() {
       t1=new Timer(60,new AbstractAction() {
           @Override
           public void actionPerformed(ActionEvent ae) {
               if(!MyPanel.this.isVisible()) {//if the panel is not visible
                   ((Timer)(ae.getSource())).stop();
               }
           }
       });
       startingTimer=new Timer(100,new AbstractAction() {
           @Override
           public void actionPerformed(ActionEvent ae) {
               if(MyPanel.this.isVisible()) {//if the panel is visible
                   t1.start();//start the timers
                   t2.start();
                   ((Timer)(ae.getSource())).stop();//dont forget we must stop this timer now

               }
           }
       });
       startingTimer.start();//start the timer which will check when panel becomes visible and start the others as necessary
    }

}

now all you would do is:

frame.remove(panel);//JPanel timers should also see panel is no more visible and timer will stop

//refresh frame to show changes 
frame.revalidate(); 
frame.repaint();

Upvotes: 1

Nic
Nic

Reputation: 98

Try this:

myFrame.getContentPane().remove(myPanel);
            myFrame.validate();

Make sure your music and other components are within the panel so they are removed as well.

Upvotes: 1

Related Questions