我的Java图片无法正确显示

时间:2014-12-12 21:04:37

标签: java image

我正在尝试制作一个简单的横向滚动游戏,我的背景颜色不起作用。它应该用fps计数器填充整个页面,但它在弹出窗口的顶部只有一小段颜色。

到目前为止,这是我的整个代码(我必须复制并粘贴,因为我“不是10级”)。

public static final int WIDTH = 640;
    public static final int HIGHT = WIDTH / 4 * 3;
    public static final String TITLE = "Sploocher's Epic Quest";
    private static Game game = new Game();

    private boolean running = false;
    private Thread thread;

    public void init(){

    }

    public void tick(){

    }

    public void renderBackground(Graphics g){

    }

    public void renderForeground(Graphics g){

    }

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

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0,0,WIDTH,HEIGHT);

        //////////////////////////////////////////////////

        renderBackground(g);
        renderForeground(g);

        g.dispose();
        bs.show();

    }


    @Override
    public void run() {
        init();
        long lastTime= System.nanoTime();
        final double numTicks = 60.0;
        double n = 1000000000 / numTicks;
        double delta = 0;
        int frames = 0;
        int ticks = 0;
        long timer = System.currentTimeMillis();

        while(running){
            long currentTime = System.nanoTime();
            delta += (currentTime - lastTime) / n;
            lastTime = currentTime;

            if(delta >= 1){
                tick();
                ticks++;
                delta--;
            }

            render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000){
                timer+=1000;
                System.out.println(ticks + "Ticks, FPS: " + frames);
                ticks = 0;
                frames = 0;
            }
        }
        stop();
    }

    public static void main(String args[]){
        JFrame frame = new JFrame(TITLE);
        frame.add(game);
        frame.setSize(WIDTH, HIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setFocusable(true);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.pack();
        game.start();

    }


    private synchronized void start(){
        if(running)
            return;
        else
            running = true;
        thread = new Thread(this);
        thread.start();
    }

    private synchronized void stop(){
        if(!running)
                return;
        else
            running = false;

        try {
            thread.join();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        System.exit(1);
    }

}

如果有人能帮我弄清楚如何修复我的代码,我真的很感激。

0 个答案:

没有答案