How do I fix this splash code?

I am trying to create a splash screen for a game, both of which are JFrames. I want the splash screen to be open for 3 seconds and then removed. The JFrame for the main body of the game needs to be created and displayed immediately after that. I am using Thread.sleep () to wait 3 seconds, but the loading page is delayed for 3 seconds instead of playing. Code below:

new load();
try 
{
    Thread.sleep(3000);
    dispose();
    new gameInfo();
} 
catch (InterruptedException ex) 
{
    Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}

      

+3


source to share


2 answers


you need to start it in a new thread because now you are freezing the main thread and affecting the GUI and making it freeze. So, you need to wait 3000ms in the background and the only easy way is to create a new thread. here is the pseudocode

new load();
new Thread(){
    public void run(){
        try {
            Thread.sleep(3000);
            //i think you should call this 2 lines below in main thread
            dispose();
            new gameInfo();
        } catch (InterruptedException ex) {
            Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }
}.start();

      



this code won't work, it's just pseudocode. I need to see the whole class for it to work.

0


source


new load();
new Thread(){
    public void run(){
        try {
            Thread.sleep(3000);
            //i think you should call this 2 lines below in main thread
            dispose();
            new gameInfo();
        } catch (InterruptedException ex) {
            Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }
}.start();

      



0


source







All Articles