Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 系统在运行此代码时冻结_Java_Linux_Graphics - Fatal编程技术网

Java 系统在运行此代码时冻结

Java 系统在运行此代码时冻结,java,linux,graphics,Java,Linux,Graphics,我正在youtube上关注一个Java游戏编程系列,在我们为程序添加一些代码之前,一切都进展顺利。该程序的代码为: package com.fagyapong.rain; import javax.swing.*; import java.awt.*; import java.awt.image.*; public class Game extends Canvas implements Runnable{ private static final long serialVersio

我正在youtube上关注一个Java游戏编程系列,在我们为程序添加一些代码之前,一切都进展顺利。该程序的代码为:

package com.fagyapong.rain;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class Game extends Canvas implements Runnable{
    private static final long serialVersionUID = -247215114548172830L;

    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3;

    private JFrame frame;
    public Thread thread;
    private boolean running = false;

    public Game() {

        // Setup Game window
        Dimension size = new Dimension(width * scale, height * scale);
        setPreferredSize(size);

        frame = new JFrame();
    }

    public synchronized void start() {
        running = true;
        thread = new Thread(this, "Display");
        thread.start();
    }

    public synchronized void stop() {

        running = false;
        try {
            thread.join();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {

        while (running) {
            update();
            render();
        }
    }

    public void update() {

    }

    public void render() {

        // Get the canvas' BufferStragy object
        BufferStrategy bs = getBufferStrategy();

        if (bs == null) {
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.GRAY);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {

        Game game = new Game();
        game.frame.setResizable(false);
        game.frame.setTitle("Rain");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);

        game.start();
    }
}
下面是导致系统冻结的代码(已在上述代码中注释掉)


好吧,我稍微解决了这个问题。我也有同样的问题。对我来说,问题是三重缓冲。相反,请将代码设置为:

createBufferStrategy(2);

这样,它只是双缓冲。我没有一台很棒的电脑,所以我不得不把它设为1而不是2。在这一点上,我猜它根本不是缓冲。这就是我让它工作的方式。

它不会为我冻结系统。到底发生了什么?程序运行并显示一个窗口,但系统随后变得无响应它是否达到了内存使用峰值?我认为这就是它所做的,因为系统变得完全无响应为什么不使用
SwingUtilities.invokeLater()
createBufferStrategy(2);