程序随机启动时不会显示

时间:2017-08-03 19:00:52

标签: java swing

我正在使用Swing制作游戏,并且我遇到了一个随机错误,当打开程序时,没有显示任何内容。似乎没有什么可以触发它,但我不知道可能导致它的原因。而且这也是我的程序中似乎问题的一切,因为当我测试问题的根源时,似乎问题是它没有呈现任何东西。我的更新方法正常,但paint方法没有运行。

这是gameHandler类加载所有内容以及我认为问题可能发生的地方,如果这还不足以找到错误,请让我添加你想要的任何内容见。

public class GameHandler 
{

    private GamePanel gp;
    private Player p;
    private StarManager sm;
    private EnemyManager em;
    private UI ui;
    private BulletManager bm;
    private PANEL state;
    private MenuPanel mp;

    public GameHandler(GamePanel gp, Core c)
    {
        this.gp = gp;
        p = new Player(c.getWidth()/2 - 50/2, c.getHeight() - c.getHeight()/4, 25, 50, c);
        sm = new StarManager(50, c.getWidth(), c.getHeight(), 15);
        em = new EnemyManager(gp, c, p);
        bm = new BulletManager(4, p, em);
        new GameInput(c, p, bm);
        ui = new UI(p);
        mp = new MenuPanel(c);
    }

    public void update()
    {
        state = gp.getState();

        sm.update();
        if(state == PANEL.GAME)
        {
            p.update();
            em.update();
            bm.update();
        }
    }

    public void render(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        sm.render(g);
        if(state == PANEL.GAME)
        {   
            p.render(g);
            bm.render(g);
            em.render(g);

            ui.render(g);
        }
        else if(state == PANEL.MENU)
        {
            mp.render(g);
        }
    }

}
public class Core extends JComponent implements Runnable
{

    private static final long serialVersionUID = 1L;

    private int 
    WIDTH = 800, 
    HEIGHT = 800;

    public boolean running;

    public GamePanel gp;

    public Core()
    {
        new Display(WIDTH, HEIGHT, "Geometric Space Invasion", this);
        init();
    }

    public void init()
    {
        gp = new GamePanel(this);
        setLayout(new BorderLayout());

        add(gp, BorderLayout.CENTER);

        Thread t = new Thread(this);
        running = true;
        t.start();
    }

    public static void main(String[] args) 
    {
        new Core();
    }

    @Override
    public void run()
    {
        while(running)
        {
            try 
            {
                Thread.sleep(7);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            gp.update();
            gp.repaint();

        }
    }

    public int getWidth()
    { return WIDTH; }

    public int getHeight()
    { return HEIGHT; }

}
public class GamePanel extends JPanel
{

    private static final long serialVersionUID = 1L;

    private GameHandler gh;
    private PANEL state = PANEL.MENU;
    private boolean gameOver;

    public GamePanel(Core c)
    {
        setBackground(Color.BLACK);
        gameOver = false;
        gh = new GameHandler(this, c);
    }

    public void update()
    {
        if(gameOver)
            state = PANEL.MENU;
        gh.update();
    }

    public enum PANEL
    {
        MENU,
        GAME
        /* THIS IS HOW TO CREATE A PANEL VAIRABLE AND CHECK
        private PANEL test = PANEL.MENU;
        if(test == PANEL.MENU)
            //Do whatever.
        */
    }

    public enum DIFFICULTY
    {
        EASY,
        MEDIUM,
        HARD
    }

    public DIFFICULTY diff = DIFFICULTY.MEDIUM; //TODO : REMOVE

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        gh.render(g);
    }

    public DIFFICULTY getDifficulty()
    {
        return diff;
    }

    public PANEL getState()
    {
        return state;
    }

    public void setGameOver(boolean gameOver)
    {
        this.gameOver = gameOver;
    }
}

编辑:修复。我通过在Core类的构造函数中切换两行来修复它。

public Core()
{
    new Display(WIDTH, HEIGHT, "Geometric Space Invasion", this);
    init();
}

成了

public Core()
{
    init();
    new Display(WIDTH, HEIGHT, "Geometric Space Invasion", this);
}

0 个答案:

没有答案