Java - 使用缓冲图像显示背景

时间:2014-11-03 15:06:27

标签: java bufferedimage

在这里写一个小游戏,我遇到了一些问题。我不能为我的生活设法显示我的缓冲图像。

我认为我已经掌握了一切,但如果背景仍然是黑色,显然会出现问题。

游戏类:

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());
    }   
}

窗口类:

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();

    }

}

1 个答案:

答案 0 :(得分:1)

我没有BufferedImageLoader,所以我使用以下代码加载我的图片:

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

不要忘记在渲染中绘制图像!

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();
}