Java应用程序冻结

科迪·理查森(Cody Richardson)

我是Java的新手。非常。就像我今天基本上开始。我以前有其他语言的编程知识,例如c,c ++,PHP,javascript等,但我无法弄清楚。我开始在YouTube上观看有关如何用Java制作视频游戏的教程(来自ChernoProject的视频),但是在大约7集的过程中,我遇到了一个问题,即我们的窗口所在的位置,并且在整个对象上绘制了一个黑色的矩形,该应用程序冻结了我的整个计算机。这是我的代码:

package com.darksun.theonetruemike.rain;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

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

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

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

    public Game(){
        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(){
        BufferStrategy bs = getBufferStrategy();

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

        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.BLACK);
        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();
    }

}

我正在使用eclipse制作此项目(强烈违背我的意愿),当我按“调试”按钮时,出现窗口,并且计算机冻结,导致不得不强制退出整个计算机。如果可以的话,请提供帮助,并感谢您的提前帮助!

阿科斯

由于我的信誉不佳,我将发表评论作为答案,请尽量不要-rep

我将您的代码复制到了netbeans,出现了一个黑色的控制台(如窗口),什么也没有发生,但是它没有冻结,但是JVM使用了大约50%的cpu

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章