그래서 저는 지금 막 자바를 배우기 시작했으며 YouTube에서 비디오 자습서를 따르고 있습니다. 이 코드를 실행하려고하면 실행되지만 내 컴퓨터가 느려지고 잠시 후에 모든 것이 고정됩니다. 그게 뭐가 잘못 되었 니? 나는 이것이 관련이 있는지도 모른다. 그러나 나는 리눅스 머신에서 이것을 실행하고있다.내 프로그램이 내 컴퓨터를 사용할 수없는 속도로 늦추는 이유
package com.matt.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(){
int x = 1;
while (running == true){
System.out.println("Running..."+ x);
update();
render();
x = x + 1;
}
}
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();
}
}
Canvas를 최대 속도로 업데이트하면 다른 많은 것들 중에서 Graphics 객체를 가져오고 릴리스하기 때문에. javax.swing을 살펴 보시기 바랍니다. Timer. – EJP