0

I'm making my first applet game in Java, read a couple of tutorials and found different solutions for double-buffering. I would like to know what is the difference between them, any pros-cons, etc. Thanks in advance!

First one:

public void update(Graphics g) {
    if (offImage == null) {
        offImage = createImage(this.getWidth(), this.getHeight());
        offGraphics = offImage.getGraphics();
    }
    offGraphics.setColor(getBackground());
    offGraphics.fillRect(0, 0, getWidth(), getHeight());
    offGraphics.setColor(getForeground());
    paint(offGraphics);
    g.drawImage(offImage, 0, 0, this);
}

Second one:

public void init() {
    offImage = createImage(getWidth(), getHeight());
    offGraphics = offImage.getGraphics();
}

public void paint(Graphics g) {
    g.drawImage(offImage,0,0,this);
}

public void update(Graphics g) {
    paint(g);
}
Kokufuu
  • 29
  • 6

1 Answers1

1

It's pretty much the same. Still, I would use the paint method as many people who will check your code for the first time will search for it since it is mostly used in Graphics development.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76