Search code examples
javabufferstrategy

BufferStrategy drawing and updating the screen


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?

FRAME CLASS:

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

    }

}

GAME CLASS:

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

Solution

  • g.dispose() doesn't clear the screen.

    After g = bs.getDrawGraphics();

    You have to do that yourself.

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