运行Java游戏时出现线程错误

时间:2015-06-02 16:53:37

标签: java multithreading swing awt event-dispatch-thread

运行Java游戏时遇到一些奇怪的错误。我已经在这个游戏上工作了几个星期作为我的计算机科学课程的项目,它将于2015年6月4日到期,所以我感谢任何帮助。最初我在一个名为Game的类中编写游戏,并从名为run的静态方法运行它。后来,我决定在名为Control的类中添加一个GUI菜单。要立即运行游戏,我在Control类中调用main方法。此菜单有一个带动作监听器的按钮。单击该按钮时,将调用Game类中的run方法。如果我直接点击并运行run方法,那么游戏运行正常。但是,如果我单击调用run方法的按钮,它会绘制一个帧,但不是实际的游戏。

以下是我的GUI代码:

    public class Control extends JFrame implements ActionListener {

    // JPanel
    public JPanel pnlButton = new JPanel();

    // Buttons
    public JButton btnAddFlight = new JButton("Multiplayer");
    public JButton single = new JButton("Singleplayer");

    public Control() throws IOException,InterruptedException {
        super("Bouncy Ball");
        //Set button size
        btnAddFlight.setBounds(150, 400, 220, 30);
        single.setBounds(150,350,220,30);

        // JPanel bounds
        pnlButton.setBounds(0, 0, 500, 500);
        pnlButton.setBackground(Color.WHITE);

        // Adding the Bouncy Ball logo to JPanel
        String path = "gg.jpg";
        File file = new File(path);
        BufferedImage image = ImageIO.read(file);
        JLabel label = new JLabel(new ImageIcon(image));
        label.setBounds(179,50,150,150);

        //Action Listener setup
        single.addActionListener(this);

        //add buttons and title logo to JPanel
        pnlButton.add(btnAddFlight);
        pnlButton.add(label);
        pnlButton.add(single);

        //Set up and add the instructions to the JPanel
        JLabel gg = new JLabel("Welcome to Bouncy Ball, a game where you have to manipulate");
        gg.setFont(new Font("Serif", Font.PLAIN, 18));
        gg.setBounds(0,10,500,500);
        pnlButton.add(gg);
        JLabel f = new JLabel("the window size with WASD to win! Click the buttons to start.");
        f.setFont(new Font("Serif", Font.PLAIN, 18));
        f.setBounds(0,28,500,500);
        pnlButton.add(f);
        pnlButton.setLayout(null);

        //Add JPanel to JFrame
        this.add(pnlButton);

        // JFrame properties
        setSize(500, 500);       
        setTitle("Bouncy Ball");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);;
    }

    @Override
    public void actionPerformed(ActionEvent submitClicked) {
        try{
            isClicked = true;

            Game.run();
        }
        catch(InterruptedException a){}
    }

    public static void main(String[] args) throws IOException,InterruptedException{
        new Control();
    }
}

这是我游戏类中的run方法:

public static void run() throws InterruptedException {
    //Setting up JFrame
    frame = new KeyFrame("Bouncy Ball");
    frame.setSize(1000, 1000);

    //Setting up Scanner and get user level input
    Scanner scan = new Scanner (System.in);
    System.out.println("Level one, two, three, or four?(must be an int, four is multiplayer)");
    f = scan.nextInt();

    //Display JFrame
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.toFront();

    //Add Game JPanel
    game = new Game();
    frame.add(game);

    //Setting up game basics
    b = new Ball(game,0,0,30,30);
    setUpLevel();
    objList.add(0,b);

    //Main Game Loop
    while (true) {
        //Request Focus
        game.requestFocusInWindow();

        //Update current Time
        lastStartTime = System.currentTimeMillis();

        //Move the ball and the player
        p.move();
        b.move();

        //Refresh JPanel
        game.repaint();
        Thread.sleep(10);

        //Check if the player wins or loses
        checkWin();
        checkLoss();
    }
}

如果我直接调用run方法它可以工作,但是如果我单击GUI中的按钮则不行。我觉得这是一个线程问题,因为主线程一旦调用就结束了。尽管如此,我并不是百分之百确定。我感谢任何回复,作为我的计算机科学课程的一个项目我已经在这个游戏上工作了几个星期,它将于2015年6月4日到期。

1 个答案:

答案 0 :(得分:1)

Swing是单线程的 - 所有绘画,事件等都发生在这个线程上(名为EDT)。在EDT上调用Game.run方法,然后执行长时间运行的任务(例如while (true)Thread.sleep) - 这会阻止EDT执行任何其他操作。要执行动画,请考虑使用Thread,或者更好的是使用Swing Timer