1

我正在为一个班级项目制作一个简单的游戏,但无法让我的图像在屏幕上绘制。这是用作用户界面的我的 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 可能搞砸了。任何帮助将不胜感激。

4

0 回答 0