Java 申请冻结
Java application freezing
我对 java 很陌生。非常。就像我今天基本上开始一样。我以前有其他语言的编程知识,如 c、c++、PHP、javascript 等,但我无法弄清楚这一点。我开始在 Youtube 上观看有关如何在 Java 中制作视频游戏的教程(来自 theChernoProject 的视频),但在大约 7 集中,我遇到了一个问题,我们有我们的 window,我们在绘画一个黑色的矩形横跨整个东西,应用程序冻结了我的整个计算机。这是我的代码:
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做这个项目(非常违背我的意愿),当我按下调试按钮时,出现window,我的电脑死机,导致不得不强制退出整个计算机。有能力的话请帮忙,提前谢谢大家的帮助!
由于我的声望低,我会post我的评论作为答案,尽量不要-rep
我将你的代码复制到 netbeans,出现一个像 window 的黑色控制台,没有任何反应,但它没有冻结,但是 JVM 使用了大约 50% cpu
我对 java 很陌生。非常。就像我今天基本上开始一样。我以前有其他语言的编程知识,如 c、c++、PHP、javascript 等,但我无法弄清楚这一点。我开始在 Youtube 上观看有关如何在 Java 中制作视频游戏的教程(来自 theChernoProject 的视频),但在大约 7 集中,我遇到了一个问题,我们有我们的 window,我们在绘画一个黑色的矩形横跨整个东西,应用程序冻结了我的整个计算机。这是我的代码:
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做这个项目(非常违背我的意愿),当我按下调试按钮时,出现window,我的电脑死机,导致不得不强制退出整个计算机。有能力的话请帮忙,提前谢谢大家的帮助!
由于我的声望低,我会post我的评论作为答案,尽量不要-rep
我将你的代码复制到 netbeans,出现一个像 window 的黑色控制台,没有任何反应,但它没有冻结,但是 JVM 使用了大约 50% cpu