Double Buffer a JFrame

做~自己de王妃 提交于 2019-12-02 11:32:47

问题


I have been reading a lot about Double Buffering as I am working on a 2D game. I have come across many different strategies for implementation, but am unsure how Double Buffering would fit into the way I have created my game window. For example, one article I came across (http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering) suggested having a separate method for drawing; however, I suspect this would be applicable if you were drawing shapes, instead of adding components to the window.

Here is my main GUI code (keylistener methods omitted)

public class MainWindow extends JFrame implements KeyListener{
private Dimension dim;
private CardLayout layout;
private JPanel panel;
private JLayeredPane gameLayers;
private Menu menu;
private MiniGame miniGame;
private Board board;
private Sprite sprite;
private Game game;
private Map map;
private GameState gs;

private Boolean[] keys;


public MainWindow(Game game, GameState gs, Map map, Sprite sprite){
    //Call superclass.
    super();

    addKeyListener(this);

    //Sore references to critical game components.
    this.game = game;//So we can call methods when an event occurs.
    this.gs = gs;
    this.map = map;//Used to construct the board.
                    //The board needs to know the layout of the map.
    this.sprite = sprite;//Used to add the sprite to one of the layers.

    //Instantiate objects.
    dim = new Dimension(800, 600);
    layout = new CardLayout();
    panel = new JPanel(layout);
    menu = new Menu();
    miniGame = new MiniGame();
    board = new Board(map);
    gameLayers = new JLayeredPane();

    //Remove decoration and place window in center of screen.
    setUndecorated(true);
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenDim.width /2)-(dim.width/2),
            (screenDim.height/2)-(dim.height/2),
            dim.width,
            dim.height);

    //Add the board to a layer.
    gameLayers.add(board, JLayeredPane.DEFAULT_LAYER);
    board.setBounds(0, 0, dim.width, dim.height);
    board.setBoard();

    //Add the sprite to a layer.
    gameLayers.add(sprite, JLayeredPane.PALETTE_LAYER);
    sprite.setBounds(0, 0, 50, 50);

    //Add components to window.
    panel.add(gameLayers, "gameLayers");
    panel.add(miniGame, "miniGame");
    panel.add(menu, "menu");

    //Add the "cards" to the window.
    add(panel);

    //JFrame housekeeping.
    pack();
    setSize(dim);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);

    //Holds state when an event is triggered.
    //Ugly hack to circumvent delay issue.
    keys = new Boolean[4];
    for(int i=0; i<keys.length; i++){
        keys[i] = false;
    }
}
}

How would you recommend I approach this? Bearing in mind that the Board is a JPanel consisting of a grid of scaled images, and the sprite will be a JComponent displaying a scaled image.

Regards, Jack.


回答1:


Override the JPanel's paintComponent() Method and paint the content into a BufferedImage image first. Once done, copy the content of the BufferedImage into the graphics context you get from paintComponent().

protected void paintComponent(Graphics g) 
{
    BufferedImage bufferedImage = new BufferedImage(500, 500, BufferedImage.TYPE_ARGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    //paint using g2d ...

    Graphics2D g2dComponent = (Graphics2D) g;
    g2dComponent.drawImage(bufferedImage, null, 0, 0);  
}



回答2:


As the Java Graphics Library is not fast, either well functional, you should inform yourself about the 'Lightweight Java Game Library' ( http://lwjgl.org/ ). It uses OpenGL, a strong Graphics Library, using native code. Lwjgl is also used for the game Minecraft. Also, OpenGL is used for different languages, so you could learn multi-functional and high-level programming.



来源:https://stackoverflow.com/questions/9367502/double-buffer-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!