2014-01-10 3 views
0

프로그램을 실행하면 run이 호출 될 때 "Run() 메서드가 호출되었습니다"라는 메시지가 표시됩니다. 그러나 if 문 내부의 System.out.println()도 호출되지 않으며 render() 메서드가 호출되지 않습니다.Run 메서드가 호출되었지만 그리기가 없음

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

import javax.swing.JFrame; 

public class Game extends Canvas implements Runnable { 
public static int WIDTH = 300; 
public static int HEIGHT = WIDTH/16*9; 
public static int SCALE = 3; 
public static String TITLE = ""; 

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


public void start() { 
    if(running) return; 

    thread = new Thread(this); 
    thread.start(); 
} 

public void stop() { 
    if(!running) return; 

    try{ 
     thread.join(); 
    }catch(InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

public void run() { 
    System.out.println("Run() has been called"); 
    long lastTime = System.nanoTime(); 
    long timer = System.currentTimeMillis(); 
    double ns = 1000000000.0/60.0; 

    double delta = 0; 
    int ticks = 0; 
    int fps = 0; 

    while(running) { 
     long now = System.nanoTime(); 
     delta += (now-lastTime)/ns; 
     lastTime = now; 
     while(delta >= 1) { 
      tick(); 
      ticks++; 
      delta--; 
     } 
     render(); 
     fps++; 
     if(System.currentTimeMillis() - timer > 1000) { 
      timer += 1000; 
      System.out.println("Fps: " + fps + " Ticks: " + ticks); 
      fps = 0; 
      ticks = 0; 
     } 
    } 
    stop(); 
} 

public void tick() {   
} 

public void render() { 
    BufferStrategy bs = getBufferStrategy(); 
    if(bs==null) { 
     createBufferStrategy(3); 
     return; 
    } 

    Graphics g = bs.getDrawGraphics(); 
    g.fillRect(36, 25, 25, 25); 
    g.dispose(); 
    bs.show(); 
} 

public Game() { 
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 

    frame = new JFrame(); 
} 

public static void main(String[] args) { 
    Game game = new Game(); 

    game.frame.setResizable(false); 
    game.frame.setTitle("SPACE ADAPT PRE-ALPHA 0.001"); 
    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

넣으십시오'에서 System.out.println (실행) 읽기,'while 루프 전에. – tilpner

+0

당신의 달리기는'거짓 '입니다. 결코 설정하지 마십시오 – nachokk

+0

오 세상에, 고마워요. 나는 왜 내가 그것에 대해 생각하지 않았는지 모르겠다! – user3183397

답변

3

running을 절대로 true로 설정하면 false입니다. 이 질문과 관련이없는 보조 참고로 대부분의 스윙 구성 요소 메서드는 스레드로부터 안전하지 않으므로 Event Dispatch Thread이 아닌 다른 스레드를 호출하면 예상대로 작동하지 않습니다.

Concurrency in Swing

+1

니스 수정 ... :) –

관련 문제