2014-11-03 2 views
0

여기에 약간의 게임을 작성하고 몇 가지 문제가 발생했습니다. 나는 나의 삶이 나의 버퍼 된 이미지를 보여줄 수 없다.자바 - 버퍼 된 이미지를 사용하여 배경 표시

나는 모든 것을 가지고 있다고 생각하지만 배경이 여전히 검은면 분명히 뭔가 잘못되었다고 생각합니다.

게임 등급 :

public class Game extends Canvas implements Runnable { 
    private boolean running = false; 
    private Thread thread; 

    public static int WIDTH, HEIGHT; 



public BufferedImage background = null; 


// object handler 
Handler handler; 

Random rand = new Random(); 

private void init(){ 

    WIDTH = getWidth(); 
    HEIGHT = getHeight(); 
    BufferedImage background = new BufferedImage(0, 0, 0); 
    try{ 
     background = ImageIO.read(new File("/Background.png")); 

    }catch(IOException e){ 

    } 

    handler = new Handler(); 


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

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

    public void run(){ 
     // Heat of the Game. Game Loop. 
     init(); 
     this.requestFocus(); 
     long lastTime = System.nanoTime(); 
     double amountOfTicks = 60.0; 
     double ns = 1000000000/amountOfTicks; 
     double delta = 0; 
     long timer = System.currentTimeMillis(); 
     int updates = 0; 
     int frames = 0; 
     while(running){ 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/ns; 
      lastTime = now; 
      while(delta >= 1){ 
       tick(); 
       updates++; 
       delta--; 
      } 
      render(); 
      frames++; 

      if(System.currentTimeMillis() - timer > 1000){ 
       timer += 1000; 
       System.out.println("FPS: " + frames + " TICKS " + updates); 
       frames = 0; 
       updates = 0; 
      } 

     } 

    } 

    private void tick(){ 
     handler.tick(); 
    } 
    private void render(){ 
     BufferStrategy bs = this.getBufferStrategy(); 
     if(bs == null){ 
      this.createBufferStrategy(3); 
      return; 
     } 

     Graphics g = bs.getDrawGraphics(); 
     g.setColor(Color.black); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.drawImage(background, 0, 0, null); 
     g.dispose(); 
     bs.show(); 
    } 

    private void LoadImageBackground(BufferedImage image){ 
     int h = image.getHeight(); 
     int w = image.getWidth(); 
    } 

    public static void main(String[] args) { 
     new Window(762,720, "BusyBee", new Game()); 
    } 
} 

창 클래스 :

try { 
    background = ImageIO.read(new File("/Background.png")); 
} catch (IOException e) { 
} 

돈 :

public class Window extends Game { 
    public Window(int w, int h, String title, Game game){ 

     game.setPreferredSize(new Dimension(w,h)); 
     game.setMaximumSize(new Dimension(w,h)); 
     game.setMinimumSize(new Dimension(w,h)); 

     JFrame frame = new JFrame(title); 
     frame.add(game); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setIconImage(background); 


     // starts the synchronized game start() method 
     game.start(); 

    } 

} 
+0

'JFrame.setImageIcon'은 프레임의 장식 왼쪽 상단에 나타나는 프레임의 아이콘을 설정합니다. JFrame의 컨텐츠 창의 backgound를 설정하지 않습니다. 자세한 내용은 JavaDoc을 참조하십시오. http://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#setIconImage-java.awt.Image- – msrd0

+0

AH! 그 링크를 가져 주셔서 감사합니다. JFrame에 배경을 추가 할 올바른 영역에 있는지 또는 Game 클래스에서 수행해야하는지 말해 줄 수 있습니까? –

+0

'JFrame' 클래스에는'setBackgroundImage'가 없으므로 프레임의 내용 창을 대체해야한다고 생각해야합니다. 예 :'frame.setContentPane (new JLabel (background));' – msrd0

답변

1

내가 BufferedImageLoader이 없었다, 그래서 나는 내 이미지를로드하려면 다음 코드를 사용 렌더링에서 이미지를 그려야합니다!

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

    Graphics g = bs.getDrawGraphics(); 
    g.setColor(Color.black); 
    g.fillRect(0, 0, getWidth(), getHeight()); 

    // DRAW THE BACKGROUND!! 
    g.drawImage(background, 0, 0, null); 

    g.dispose(); 
    bs.show(); 
} 
+0

'ImageIcon'을 사용하는 것이 왜 더 좋은가? – msrd0

+0

편의. BufferedImageLoader가 없으므로 한 줄에 ImageIcon을로드 할 수 있습니다. BufferedImage를 사용하도록 편집합니다. – Chris

+0

그런 다음 BufferedImage를로드하십시오. 나는 BufferedImageLoader도 모른다. 그러나 javax.imageio도 그것들을 로딩한다. – msrd0

관련 문제