2013-12-20 4 views
0

수정을 시도했지만 화면을 변경하지 않습니다. 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(); 
} 

} 
+1

표시하기 전에 BufferStrategy를 처분()해야합니다. 그것은 좋을 수 없다. –

+0

나는 그것을 시험해 보았다. 작동하지 않았다. – user2469009

+0

코드가 [이 질문] (http://stackoverflow.com/questions/20547542/bufferstrategy-not-working/20547780#20547780)과 매우 유사하게 보입니다. 나는 그들이 아마 같은 튜토리얼을 사용하고 있다고 생각하기 때문에 당신의 코드를 비교하려고 시도 할 것이다. – DoubleDouble

답변

1

오류 스택 추적을 제공하겠습니다.

귀하의 렌더링 방법은 여기에서 호출되지 않습니다.
이것은 run 메소드가 전혀 호출되지 않기 때문입니다.
이 모든 이유는 스레드 생성시 올바른 Runnable 객체를 전달하지 않았기 때문입니다. 비어있는 실행 스레드를 만듭니다. 당신의 시작 방법에
, 단지

thread = new Thread(this); 

thread = new Thread(); 

교체 그리고 그것은 작동합니다.

희망이 도움이됩니다. 즐겨.

+0

그것은 효과가 있었다. 고마워 – user2469009

관련 문제