BufferStrategy清除帧背景

时间:2017-10-29 10:03:49

标签: java swing graphics2d bufferstrategy

我尝试使用graphics2DJFrame在Java中创建一个简单的GUI。 我在JFrame中在this.setBackground(new Color(54, 71, 99))方法中键入initWindow()添加了背景色。事实证明,backBuffer正在清除该背景,而不是重新绘制导致此问题的行,这是render()方法,最后一行backBuffer.show()

如何使清除主背景?

package asteroids;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

public class Main extends Canvas implements KeyListener {

    private boolean gameOver;
    private BufferStrategy backBuffer;  
    private Dimension dimension = new Dimension(Config.WINDOW_WH[0], Config.WINDOW_WH[1]);
    private List<Star> stars = new ArrayList<Star>();
    private HashMap<Integer,Boolean> keyDownMap = new HashMap<Integer, Boolean>();

    public Main() {
        // Initialize Window
        initWindow();
        addKeyListener(this);

        this.setBackground(new Color(54, 71, 99));
        this.createBufferStrategy(2);               
        backBuffer = this.getBufferStrategy();

        // init variables
        gameOver = false;

        // Generating stars
        generateStars();

        // Init loop
        gameLoop();
    }

    public void initWindow(){
        JFrame window = new JFrame("Asteroids"); 
        setPreferredSize(dimension); 

        window.add(this);           
        window.pack();  
        window.setResizable(false);
        window.setVisible(true); 
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(new Color(54, 71, 99));

        window.requestFocus();
    }

    public void update() {
        if(keyDownMap.containsKey(KeyEvent.VK_ESCAPE)){
            gameOver = false;
            System.exit(0);
        }
    }

    public void render(){
        Graphics2D g = (Graphics2D) backBuffer.getDrawGraphics();
        g.setColor(Color.WHITE);
        for(Star s: stars) {
            g.fillOval(s.posx - (s.width/2), s.posy - (s.height/2), s.width, s.height);
        }
        g.dispose();
        backBuffer.show();
    }

    public void generateStars() {
        for(int i = 0;i < 20;i++) {
            int starX = new Random().nextInt(Config.WINDOW_WH[0]-10)+5;
            int starY = new Random().nextInt(Config.WINDOW_WH[1]-10)+5;
            stars.add(new Star(starX, starY));
        }
    }

    public void gameLoop(){
        while(!gameOver){
            update();
            render();

            try{ Thread.sleep(20);}catch(Exception e){};
        }
    }

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


    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        keyDownMap.put(e.getKeyCode(), true);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        keyDownMap.remove(e.getKeyCode());
    }
}

1 个答案:

答案 0 :(得分:1)

  

如何使其不清楚主背景?

你做不到。除了Canvas不透明(无法更改)之外,BufferStrategy还有一个以上的页面,在其上绘制内容(从而允许执行页面翻转)。合并后,这将无法维护父容器的背景。

相反,你应该(事实上,你必须)清除你要绘制的缓冲区的Graphics上下文,每次调用render,否则你将绘制到以前的绘图画上它。

一种技术可能是使用“静态”内容生成BufferedImage,然后将其首先绘制到缓冲区