Question

Ok so this is my code:

package game;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PatternGame implements Properties
{
public static JFrame df = new JFrame();
public static int height = 800;
public static int width = 600;
public static int gsize = 4;
public static int order =1;
public static Dimension size = new Dimension(height,width);
public static menu Menu = new menu();
public static GridLayout Ggrid = new GridLayout(gsize,gsize);

public void DisplayPat()
{
    df.dispose();
    df = new JFrame();
    df.setLocation(300,100);
    df.setSize(size);
    df.setResizable(false);
    df.setLayout(Ggrid);
    df.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    df.setVisible(true);


}
public static InputGame game = new InputGame();
public static int flag =0;
public static void clear()
{
    df.dispose();
}
public void gameStart()
{

    DisplayPat();
    getpattern();
    try {
        Thread.sleep(3000);
    } 
    catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    InputGame.setup();

//notifyAll();
}
public void blank()
{
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {

            JButton button = new JButton(" ");
            df.add(button);


        }
    }
}
public void getpattern()
{
    if (order == 1)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (handlebars[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
                flag =1;

            }
        }
        }
    if (order == 2)
        {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (ys[a][b] == 1)
                {
                JButton button = new JButton("X");
                df.add(button);
                }
                else
                {
                    JButton button = new JButton("");
                    df.add(button);
                }
            }
        }
        }
    if (order == 3)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (spaceShip[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }   if (order == 4)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (flock[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }   if (order == 5)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (percent[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }
    if (order == 6)
    {
    for (int a=0;a<4;a++)
    {
        for(int b=0;b<4;b++)
        {
            if (square[a][b] == 1)
            {
            JButton button = new JButton("X");
            df.add(button);
            }
            else
            {
                JButton button = new JButton("");
                df.add(button);
            }
        }
    }
    }
    df.setVisible(true);
}



}

package game;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class InputGame implements Properties, Runnable{
public static JFrame gf = new JFrame();
public static int height = 800;
public static int width = 600;
public static int gsize = 4;
public static int order =1;
public static Dimension size = new Dimension(height,width);
public static menu Menu = new menu();
public static GridLayout Ggrid = new GridLayout(gsize,gsize);
//public static Thread d;


public static void setup() {

    gf.dispose();
    gf = new JFrame();
    gf.setLocation(300,100);
    gf.setSize(size);
    gf.setResizable(false);
    gf.setLayout(Ggrid);
    gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PatternGame.clear();
    blank();
    gf.setVisible(true);

}
public static void blank()
{
    for (int a =0;a<4;a++)
    {
        for (int b =0;b<4;b++)
        {
            JButton button = new JButton("");
            gf.add(button);
        }
    }
}
static Thread t;
public void run() {
    // TODO Auto-generated method stub
    t = new Thread(this);
    t.start();
}
}

Now I want this code to display a pattern on the screen wait three seconds and then display a blank screen for the user to input. Now when I launch it it displays a white screen waits and then displays the blank screen. I know that the reason behind the white screen is something to do with the display not being able to get the main thread before it sleeps. How can I fix this? Thanks in advance!

Was it helpful?

Solution

You can use Timer as shown below:

public void gameStart() {
    DisplayPat();
    getpattern();
    schedule(3); //start the timer task
}

Timer timer = new Timer(); //timer object

public void schedule(int seconds) {
    timer.schedule(new ScheduleTask(), seconds*1000);
}

class ScheduleTask extends TimerTask {
    public void run() {
        System.out.format("Time completed, invoke setup");
        InputGame.setup();
        timer.cancel(); //Terminate Timer thread
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top