Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 双缓冲帧_Java_Swing_Double Buffering - Fatal编程技术网

Java 双缓冲帧

Java 双缓冲帧,java,swing,double-buffering,Java,Swing,Double Buffering,我一直在读很多关于双缓冲的书,因为我正在做一个2D游戏。我遇到过许多不同的实现策略,但我不确定双缓冲如何适合我创建游戏窗口的方式。例如,我看到一篇文章(http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering)建议采用单独的绘图方法;但是,如果您正在绘制形状,而不是将组件添加到窗口中,我怀疑这将适用 这是我的主要GUI代码(省略了keylistener方法) public类主窗口扩展JFrame实现KeyListene

我一直在读很多关于双缓冲的书,因为我正在做一个2D游戏。我遇到过许多不同的实现策略,但我不确定双缓冲如何适合我创建游戏窗口的方式。例如,我看到一篇文章(http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering)建议采用单独的绘图方法;但是,如果您正在绘制形状,而不是将组件添加到窗口中,我怀疑这将适用

这是我的主要GUI代码(省略了keylistener方法)

public类主窗口扩展JFrame实现KeyListener{
私有维度;
私人卡布局;
私人JPanel小组;
私人JLayeredPane游戏玩家;
私人菜单;
私人小游戏;
私人董事会;
私人雪碧;
私人游戏;
私人地图;
私人博弈状态;
私有布尔[]密钥;
公共主窗口(游戏、游戏状态gs、地图地图、精灵){
//调用超类。
超级();
addKeyListener(此);
//对关键游戏组件的引用。
this.game=game;//因此我们可以在事件发生时调用方法。
这1.gs=gs;
this.map=map;//用于构造电路板。
//董事会需要知道地图的布局。
this.sprite=sprite;//用于将精灵添加到其中一个层。
//实例化对象。
尺寸=新尺寸(800600);
布局=新卡片布局();
面板=新JPanel(布局);
菜单=新菜单();
迷你游戏=新迷你游戏();
线路板=新线路板(地图);
gameLayers=新的JLayeredPane();
//拆除装饰并将窗户置于屏风中心。
未装饰的设置(真实);
维度screenDim=Toolkit.getDefaultToolkit().getScreenSize();
立根((屏幕尺寸宽度/2)-(尺寸宽度/2),
(屏幕尺寸高度/2)-(尺寸高度/2),
宽度,
高度);
//将线路板添加到图层。
gameLayers.add(board,JLayeredPane.DEFAULT_层);
板立根(0,0,尺寸宽度,尺寸高度);
板。立根板();
//将精灵添加到层中。
游戏层。添加(精灵,JLayeredPane.palete\u层);
雪碧.倒退(0,0,50,50);
//将组件添加到窗口。
添加(游戏层,“游戏层”);
面板。添加(小型游戏,“小型游戏”);
面板。添加(菜单,“菜单”);
//将“卡片”添加到窗口中。
添加(面板);
//JFrame客房部。
包装();
设置大小(dim);
setDefaultCloseOperation(关闭时退出);
可设置大小(假);
setVisible(真);
//在触发事件时保持状态。
//难看的黑客绕过延迟问题。
键=新布尔值[4];

对于(inti=0;i由于Java图形库速度不快,功能也不好,您应该了解“轻量级Java游戏库”(Lightweight Java Game Library))。它使用OpenGL,一个强大的图形库,使用本机代码。 Lwjgl也用于游戏Minecraft。
此外,OpenGL用于不同的语言,因此您可以学习多功能和高级编程。

重写JPanel的paintComponent()方法,并首先将内容绘制到图像中。完成后,将BuffereImage的内容复制到从paintComponent()获得的图形上下文中


默认情况下,
JPanel
是双缓冲的。是的,你是对的。只是在文档中看到了它。但是,作为Swing中双缓冲的一般方法,应该可以工作。是的,
buffereImage
有助于编写中间版本,例如,.JPanel没有为我提供双缓冲。不过,BuffereImage工作正常。
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;
    }
}
}
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);  
}