私は2Dゲームで作業しているので、私はDouble Bufferingについて多くのことを読んできました。私は実装のためのさまざまな戦略に出くわしましたが、Double Bufferingが私のゲームウィンドウを作成した方法にどのように適合するかは不明です。たとえば、私が出会った記事(http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering)は、描画のための別の方法を提案していました。ただし、ウィンドウにコンポーネントを追加するのではなく、シェイプを描画する場合にはこれが当てはまると思われます。ここでDouble Buffer a JFrame
あなたは、私がこれに近づく推薦する方法私のメインのGUIコード(のKeyListenerのメソッドは省略)
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;
}
}
}
のですか?ボードはスケーリングされたイメージのグリッドからなるJPanelであり、スプライトはスケーリングされたイメージを表示するJComponentになります。
よろしく、 ジャック。
'JPanel'はデフォルトでダブルバッファされています。 – trashgod
そうですね。ドキュメントでそれを見ただけです。 Swingでのダブルバッファリングの一般的なアプローチとして、うまくいくはずです。 – Simon
はい、BufferedImageは中間体を構成するのに適しています。 [合成](http://ja.wikipedia.org/wiki/Digital_compositing)。 – trashgod