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

标签 java multithreading swing awt event-dispatch-thread

我在运行 Java 游戏时遇到一些奇怪的错误。作为我的计算机科学类(class)的一个项目,我已经花了数周时间开发这款游戏,截止日期为 2015 年 6 月 4 日,所以我感谢您的帮助。最初,我在名为 Game 的类中编写游戏,并通过名为 run 的静态方法运行它。后来,我决定在一个名为 Control 的类中添加一个 GUI 菜单。现在要运行游戏,我调用 Control 类中的 main 方法。该菜单有一个带有 Action 监听器的按钮。单击按钮时,将调用 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 中的按钮,则不行。我有一种感觉,这是一个线程问题,因为一旦调用 run,主线程就会结束。但我并不能 100% 确定原因。如果您有任何回复,我将不胜感激。作为计算机科学类(class)的一个项目,我已经花了数周的时间来开发这款游戏,截止日期为 2015 年 6 月 4 日。

最佳答案

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

关于java - 运行Java游戏时出现线程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602069/

相关文章:

java - 使用 Scala 在 Neo4j 中建立索引关系

java - 写入 jfloat 数组时获取 'Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)'

java - 如何调度作用于单个对象的三个线程?

java - 是否可以使用java将buffredImage转换为可缩放 vector 图像(SVG)?

java - 在 A JPanel 上绘制的网格不完全可见

java - 搜索 solr 关键字 "OR"、 "AND"AND “会报错[Hybris整合solr]

java - 如何在 groovy 脚本中包含 jar?

c# - 使用 .net 4.0 系统处理泄漏

iphone - Objective-C : Start an object on a background thread -- interact as usual?

java - 从字节数组转换和显示图像