how to double buffer in multiple classes with java
Posted
by
kdavis8
on Game Development
See other posts from Game Development
or by kdavis8
Published on 2012-06-20T04:40:55Z
Indexed on
2012/06/20
9:25 UTC
Read the original article
Hit count: 401
I am creating a Java 2D video game. I can load graphics just fine, but when it gets into double buffering I have issues.
My source code
package myPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class GameView extends JFrame {
private BufferedImage backbuffer;
private Graphics2D g2d;
public GameView() {
setBounds(0, 0, 500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backbuffer = new BufferedImage(getHeight(), getWidth(),
BufferedImage.TYPE_INT_BGR);
g2d = backbuffer.createGraphics();
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = tk.getImage(this.getClass().getResource("cage.png"));
g2d.setColor(Color.red);
//g2d.drawString("Hello",100,100);
g2d.drawImage(img, 100, 100, this);
repaint();
}
public static void main(String args[]) {
new GameView();
}
public void paint(Graphics g) {
g2d = (Graphics2D)g;
g2d.drawImage(backbuffer, 0, 0, this);
}
}
© Game Development or respective owner