BufferStrategy绘制和更新屏幕 [英] BufferStrategy drawing and updating the screen

查看:80
本文介绍了BufferStrategy绘制和更新屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图并排绘制单个矩形(未填充)以制成地板,然后以恒定速度向左移动整个地板.我的问题是,当他们向左移动时,屏幕不会刷新或删除前一个屏幕,因此几秒钟后,矩形集就变成了纯色.

I am trying to draw individual rectangles(non filled) side by side to make a floor and then moving the whole floor left at a constant speed. My issue here is that when they move left the screen doesn't refresh or delete the previous screen therefore after a few seconds the set of Rectangles is a solid color.

这是我的代码. 我想了解如何加载屏幕,然后更新值,删除旧屏幕并显示新屏幕?

here is my code. I would like to find out how to load the screen, then update values, delete old screen and display the new screen?

框架类:

package Main;

import java.awt.Dimension;

import javax.swing.JFrame;

public class Frame extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static Dimension frameDim;
    public Game game;
    public JFrame frame;
    public static int WIDTH= 800;
    public static int HEIGHT= 400;

    public Frame(Game game) {
        frameDim = new Dimension(WIDTH, HEIGHT);
        frame = new JFrame("TileJumper");
        frame.setFocusable(true);
        game.setPreferredSize(frameDim);
        frame.add(game);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        game.start();

    }

}

游戏类:

public class Game extends Canvas implements Runnable {

/**
 * 
 */
    private static final long serialVersionUID = 1L;
    public Thread thread;
    public boolean running = false;

    public double xmap = 0;
    public int tileSize = 20;
    public Font titleFont;
    public String title = "Tile Jumper";
    public int WIDTH;
    public int HEIGHT;
    public Graphics g;
    public BufferStrategy bs;


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

    }
    public void init(){
        setBackground(Color.black);
        titleFont = new Font("Dialog", 0,28);
    }
    public void tick(){
        xmap -= .1;
    }
    public void render(){

        bs = this.getBufferStrategy(); 

        if(bs == null)          {
            this.createBufferStrategy(3);
            return;
        }

        bs = this.getBufferStrategy();

        g = bs.getDrawGraphics();


        g.setColor(Color.blue);
        g.drawRect(275, 50, 250, 50);
        g.setColor(Color.white);
        g.setFont(titleFont);
        g.drawString(title, 330, 85);

        for(int i = 0; i < 100; i++){
            g.drawRect((int)(xmap + i*tileSize), 350, tileSize, tileSize);
        }

        g.dispose();
        bs.show();
        //Toolkit.getDefaultToolkit().sync();
    }
    public void run() {
        init();
        long lastTime = System.nanoTime();
        double numTicks = 60.0;
        double ns = 1000000000/numTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int updates = 0;
        int frames = 0;


        while(running){
            long currentTime = System.nanoTime();   
            delta += (currentTime - lastTime)/ns;
            lastTime = currentTime;
            while(delta>=1){
                tick();
                updates++;
                delta--;
            }
            render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000){
                timer += 1000;
                System.out.println("FPS = " + frames + " TICK = " + updates);
                frames = 0;
                updates = 0;


           }
        }

    }
    public static void main(String [] args){
        new Frame(new Game());
    }
}

推荐答案

g.dispose()不能清除屏幕.

g = bs.getDrawGraphics();

您必须自己做.

  g.setColor(Color.black);
  g.fillRect(0,0,getWidth(),getHeight());

这篇关于BufferStrategy绘制和更新屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆