how to double buffer in multiple classes with java
- by kdavis8
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);
}
}