帆布游戏渲染冻结电脑

时间:2014-12-11 23:56:54

标签: java

我不知道发生了什么,但每次运行此代码时,我的计算机完全冻结,我无法做任何事情......它曾经在Windows上工作但在linux上冻结了吗?它出什么问题了?

package game;

import java.awt.Canvas;

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Gaming extends Canvas implements Runnable {


    private Thread thread;

    public Gaming(){
    }

    public void start(){
        thread = new Thread(this);
        thread.start();
    }


    public static void main(String[] args) {
        JFrame j = new JFrame();
        Gaming g = new Gaming();
        j.add(g);
        j.setVisible(true);
        j.setSize(800,600);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        g.start();

    }

    public void render(){
        BufferStrategy bs = getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.fillRect(50, 50, 50, 50);
        g.dispose();
        bs.show();
    }

    @Override
    public void run() {
        while(true){
            render();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

1.我知道你不能使用:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        create your jFrame here
    }
});

2.我看到这段代码完全没有意义。你想要实现什么目标?

3.你想通过重新绘制循环来实现什么?(下面的代码)

@Override
public void run() {
    while (true) {
        render();
    }
}

答案 1 :(得分:0)

  • 在调用渲染后添加某种延迟,以允许系统实际将图形推送到硬件并更新屏幕
  • 阅读BufferStrategy的JavaDocs以获取如何使用它的示例,您忽略了由于系统中的其他更改和其他因素而导致更新丢失的事实
  • 在绘画之前不要忘记清除Graphics背景......

例如......

import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Gaming extends Canvas implements Runnable {

    private Thread thread;

    public Gaming() {
    }

    public void start() {
        thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame j = new JFrame();
                Gaming g = new Gaming();
                j.add(g);
                j.setVisible(true);
                j.setSize(800, 600);
                j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                g.start();
            }
        });
    }

    public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }

        do {
                 // The following loop ensures that the contents of the drawing buffer
            // are consistent in case the underlying surface was recreated
            do {
                         // Get a new graphics context every time through the loop
                // to make sure the strategy is validated
                Graphics graphics = bs.getDrawGraphics();

                // Render to graphics
                graphics.setColor(Color.WHITE);
                graphics.fillRect(0, 0, getWidth(), getHeight());
                graphics.setColor(Color.BLACK);
                graphics.fillRect(50, 50, 50, 50);
                // Dispose the graphics
                graphics.dispose();

                         // Repeat the rendering if the drawing buffer contents
                // were restored
            } while (bs.contentsRestored());

            // Display the buffer
            bs.show();

            // Repeat the rendering if the drawing buffer was lost
        } while (bs.contentsLost());
    }

    @Override
    public void run() {
        while (true) {
            render();
            try {
                Thread.sleep(16);
            } catch (InterruptedException ex) {
            }
        }
    }

}