如何制作缓冲图像?

时间:2019-05-21 00:03:01

标签: game-engine

我现在正在为游戏编写代码,但似乎无法显示图像。我可以帮忙吗?

基本上,我一直在尝试使png图像出现在我的代码中,以便可以显示它,但是由于某种原因,它一直在向我抛出空指针异常。我正在尽力解决此问题,但似乎无法解决。我试图重定位源文件夹,但随后似乎没有任何进展。

我的代码如下:

package gameMaker;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class MainGame extends GameDriverV4{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    int gameState=0;

    BufferedImage startScreen=addImage("output-onlinepngtools.png");
    BufferedImage gameScreen=addImage("output-onlinepngtools(1).png");

    public MainGame() {

    }

    public static void main(String[] args) {

        MainGame s = new MainGame();

        s.start();
        s.setFocusable(true);

    }



    public void splash(Graphics2D win) {


        int xLoc = 400;
        int yLoc = 300;


        win.drawImage(this.startScreen, null, xLoc,yLoc);

    }


    public void draw(Graphics2D win) {
        if (this.gameState==0) {
            splash(win);
        }
    }

}

作为参考,GameDriver是我们的老师制作的,这里是:

package gameMaker;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public abstract class GameDriverV4
  extends Canvas
  implements Runnable, KeyListener
{
  private static final long serialVersionUID = 1L;
  private int FramesPerSecond;
  private Color backGroundColor = Color.WHITE;
  protected static boolean[] Keys;
  private JFrame frame;
  private String title = "KatKollector";
  private int width = 800, height = 600;


  public GameDriverV4(int frames) {
    this.FramesPerSecond = frames;

    addKeyListener(this);






    Keys = new boolean[402];
  }






  public GameDriverV4() { this(60); }







  public void start() {
    this.frame = new JFrame();
    this.frame.setSize(this.width, this.height);
    this.frame.setTitle(this.title);
    this.frame.setDefaultCloseOperation(3);
    this.frame.setLocationRelativeTo(null);
    this.frame.setResizable(false);
    this.frame.add(this);
    this.frame.setVisible(true);

    startThread();
  }


  private void startThread() {
    Thread t1 = new Thread(this);
    t1.start();
  }








  public void setFrames(int num) { this.FramesPerSecond = num; }







  private void render() {
    BufferStrategy buffs = getBufferStrategy();
    if (buffs == null) {
      createBufferStrategy(3);
      buffs = getBufferStrategy();
    } 


    Graphics g = buffs.getDrawGraphics();

    g.setColor(this.backGroundColor);
    g.fillRect(0, 0, getWidth(), getHeight());
    draw((Graphics2D)g);


    g.dispose();

    buffs.show();
  }



  public abstract void draw(Graphics2D paramGraphics2D);



  public void run() {
    setFocusable(true);



    long beforeTime = System.currentTimeMillis();





    while (true) {
      long timeDiff = System.currentTimeMillis() - beforeTime;

      long sleep = (1000 / this.FramesPerSecond) - timeDiff;

      if (sleep < 0L) {
        sleep = 2L;
      }

      try {
        Thread.sleep(sleep);
      } catch (InterruptedException e) {

        String msg = String.format("Thread interrupted: %s", new Object[] { e.getMessage() });
        System.out.println(msg);
      } 


      render();
      beforeTime = System.currentTimeMillis();
    } 
  }



  public BufferedImage addImage(String name) {
    BufferedImage img = null;

    try {
      img = ImageIO.read(getClass().getResource(name));

    }
    catch (IOException e) {
      System.out.println(e);
    } 

    return img;
  }


  public Color getBackGroundColor() { return this.backGroundColor; }


  public void setBackGroundColor(Color backGroundColor) { this.backGroundColor = backGroundColor; }





  public void keyPressed(KeyEvent e) { Keys[e.getKeyCode()] = true; }







  public void keyReleased(KeyEvent e) { Keys[e.getKeyCode()] = false; }




  public void keyTyped(KeyEvent e) {}




  public String getTitle() { return this.title; }


  public void setTitle(String title) { this.title = title; }
}

0 个答案:

没有答案