Java双缓冲只生成灰色屏幕

时间:2013-05-02 02:24:40

标签: java graphics double game-engine buffering

我正在为一个类项目开发一个简单的游戏,并且不能让我的图像在屏幕上绘画。 这是我的JFrame,用作用户界面。

import java.awt.image.BufferStrategy;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

public class MMFrame extends JFrame {
int w;
int h;
boolean exit = false;
BufferStrategy buffer;
GameInputHandler gih;
Canvas canvas = new Canvas();
public MMFrame(){
    w = 768;
    h = 768;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setUndecorated(true);
this.setSize(w,h);
this.setVisible(true);
    this.setLocation((screenSize.width /2)-410, (screenSize.height /2)-310);
this.createBufferStrategy(2);


    canvas.setIgnoreRepaint( true );
    canvas.setSize( 768, 768 );
    gih = new GameInputHandler(this);

    canvas.addKeyListener(new KeyListener() {

        @Override
         public void keyPressed(KeyEvent e) {
                   gih.ProcessKey(e);   // --make an input handler       

          }                          

        @Override
        public void keyTyped(KeyEvent ke) {
                //gih.ProcessKey(ke);
           //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyReleased(KeyEvent ke) {
           // throw new UnsupportedOperationException("Not supported yet.");
        }

    }); //end of key listener




    this.add(canvas);




}


public void setExit(boolean b){
    exit = b;
}

public void drawStuff(Image img) {
    buffer = this.getBufferStrategy();
    Graphics graphics = null;
try {
        // clear back buffer...
        graphics = buffer.getDrawGraphics();
        graphics.drawImage(img, 0, 0, this);

        if( !buffer.contentsLost() ){
         //System.out.println("Buffer contents not lost.");
         buffer.show();
         //this.repaint();
        } else {
         //System.out.println("Buffer contents lost.");
        }

        // Let the OS have a little time...
        Thread.yield();
     } finally {
        if( graphics != null ) 
         graphics.dispose();

        }
     }


      }

这是我的主要游戏循环。

public class MouseMazeClient {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   //statemanager to handle state changes as game runs 
   StateManager SM = new StateManager(); 
   MMFrame GameWindow = new MMFrame(); 
   RenderEngine R = new RenderEngine();

   Graphics g;
   Image i;



    while (GameWindow.exit == false){
        //main render loop




        i = R.RenderFrame(SM.GetCurrentID());
        GameWindow.drawStuff(i);
        try {
           Thread.sleep(30);
        } catch (InterruptedException ex) {
           Logger.getLogger(MMFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

  System.exit(0);



  }
}

我的想法是我的渲染引擎会运行一些代码:

 public class RenderEngine {
Image RenderFrame(int state){
    Image z;


    switch (state){
        case 1:
            z = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imgs/mm-title.jpg"));
        break;

        default:
            z = null;
            //oops.                
    }

    return z;
}

}

当我编写处理更多状态的代码时,显然会增加复杂性 - 我知道程序使它成为案例1:fork我的渲染逻辑因为我在那里放了一个println。我想我可能弄错了我的BufferStrategy。任何帮助将不胜感激。

0 个答案:

没有答案