7

Is double buffering (in java) possible with awt? Currently, I'm aware that swing should not be used with awt, so I can't use BufferStrategy and whatnot (I already have some code written in awt that I don't want to rewrite in swing).

If double buffering is possible with awt, do I have to write the buffer by hand? Unlike swing, awt doesn't seem to have the same built-in double buffering capability.

If I do have to write the code by hand, is there a good tutorial to look at? Or is it just easier/advisable for a novice programmer to use swing instead?

Sorry about the multi-step question. Thanks for your time :)

exodrifter
  • 658
  • 1
  • 12
  • 23

1 Answers1

6

This is easily answered on the web. Just search for "double buffer awt" and you'll find LOTS of examples. You can even see an old example I wrote myself in 1998 in Java 1.0 AWT. You just need to instantiate your own Graphics object and draw to an Image, then blit that image into a canvas. Here's the key bit of code in my example:

  public void paint(Graphics g) {
    if (doubleBuffer) {
      paintSky(top.gBuf);
      g.drawImage(top.buf, 0, 0, this);
    } else {
      paintSky(g);
    }
  }
Chris Dolan
  • 8,905
  • 2
  • 35
  • 73
  • 1
    I would say it's advisable to reuse the swing implementation if possible. – aioobe May 16 '10 at 20:39
  • Oh dear, I can't run the code, because the newStuff() method isn't there. D: – exodrifter May 16 '10 at 20:42
  • @DDP: huh, well that's strange. Obviously I haven't tried to compile that code in a decade... The compiled code is running as an applet here: http://www.astro.wisc.edu/~dolan/java/Constellations.html so the source I linked must not be the right version. – Chris Dolan May 17 '10 at 02:24
  • All right, I'll just look into your source code anyway (Everything else seems to be in order). Many thanks for the pointers, Chris Dolan :D – exodrifter May 17 '10 at 02:59