수정을 시도했지만 화면을 변경하지 않습니다. render()
메서드에서 볼 수있는 그래픽을 사용하려고합니다. 문제를 발견 할 수 없어서 렌더링 방법 내에서 뭔가 잘못되었다고 말해서 긴장을 풀어주세요.화면 색상이 변경되지 않는 이유는 무엇입니까?
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;
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 boolean running = false;
private JFrame frame;
public synchronized void start() {
thread = new Thread();
thread.start();
running = true;
}
public synchronized void stop() {
running = false;
try{
thread.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public void run() {
while(running) {
tick();
render();
}
}
void tick() {}
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());
bs.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();
}
}
표시하기 전에 BufferStrategy를 처분()해야합니다. 그것은 좋을 수 없다. –
나는 그것을 시험해 보았다. 작동하지 않았다. – user2469009
코드가 [이 질문] (http://stackoverflow.com/questions/20547542/bufferstrategy-not-working/20547780#20547780)과 매우 유사하게 보입니다. 나는 그들이 아마 같은 튜토리얼을 사용하고 있다고 생각하기 때문에 당신의 코드를 비교하려고 시도 할 것이다. – DoubleDouble