[java] Double buffering in applets

Started by
10 comments, last by DiscoStoo 23 years, 3 months ago
I am a long time veteran to C++, and just learned Java, mainly for the purpose of writing applets. Of course games are my main focus, and ya'' can''t have games without double buffering! (Or some equivalent form of animation) I''ve found many tutorials, but they all say: "Next lesson: Double buffering in applets!", then there are no more lessons. Almost like it''s a conspiracy... Anyway, I just need to know how to draw to an off-screen buffer instead of onscreen, then how to blit that to the screen. Everything else can be figured out with time. ------------------------------------------------------------ He wants a shoehorn, the kind with teeth, because he knows there''s no such thing.
------------------------------------------------------------He wants a shoehorn, the kind with teeth, because he knows there's no such thing.
Advertisement
I haven''t actually programmed in java in a while, but if I remember correctly, drawing to another buffer is really simple. In fact, it''s just like drawing to the onscreen buffer. You know about wrinting your paint function and passing Graphics g to it? Well, all you need to do to create a new buffer is to make another Graphics object.
Graphics g2 = new Graphics();
then, you draw to that just like you would draw to Graphics g in your paint function.
Check out http://www.binod.com/reference/index.html
for some good books. I know that the first one under java,
Black Art of Game Programming, has double buffer examples.

An easy way to get double buffering is to use a JApplet instead- this is the swing equivalent of Applet and hence is double buffered - but this is only one solution and probably not the best.
DiscoStoo,

Here's the way to do double-buffering :

At the class level make an image and a graphics object -


private Image buffer;
private Graphics bg;


Then, in the beginning of the paint method do this -


public void paint(Graphics g)
{
if (buffer == null)
{
buffer = createImage(getWidth(), getHeight());
bg = buffer.getGraphics();
}


After which you draw only to the bg graphics context (i.e. bg.drawRect(x1,y1,x2,y2); ). This draws everything to the off-screen buffer. Then at the end of the paint method -


g.drawImage(buffer, 0, 0, this);
}


This draws the offscreen buffer to the actual screen.

As gdalston suggests above, a JApplet does double-buffering automatically, but, unfortunately, will not run in any major web browsers w/o the Java2 plugin installed... (I might be wrong... I have heard rumors that the newest version of Netscape will run Java2 stuff... but have not downloaded it myself.) That's a pretty major drawback! So you've gotta use the old AWT stuff for applet games still... hopefully this will change in the near future...

Hope this helps,
-Nate


Edited by - DJNattyP on January 10, 2001 12:47:22 PM
Thanks much, it''s easier than I thought. And actually, I''ve seen double-buffered applets run in my browser, and most things I do write will probably be for my own satisfaction and not published anywhere, so it''s OK.

------------------------------------------------------------
He wants a shoehorn, the kind with teeth, because he knows there''s no such thing.
------------------------------------------------------------He wants a shoehorn, the kind with teeth, because he knows there's no such thing.
Just a quick note... I believe that Netscape 6 comes with the Sun JRE 1.3. Or at least my version did.

- Daniel
My homepage
- DanielMy homepage
Ummm... What else can I do to double buffer an applet? That isn't good enough in my case. Here's my very simple applet. In fact, it's so simple that painting to an offscreen buffer only makes the flickering a LOT worse.

The problem potentially lies in how repaint() is called in the drawing area of that applet. Specifically, I have a MouseMotionListener set up that calls repaint() on mouseMoved().

Running in a JApplet or JFrame eliminates the flicker, which I do on local testing. However, the applet form can't take advantage of that, as I'd prefer not force those without it to download the java plugin.

Edited by - c_wraith on January 21, 2001 7:29:42 PM
Are you sure you are overriding both paint() & update()?

The only time I''ve seen flickering when double-buffering has been inplemented is when one of those hasn''t been overridden.

eg.

public void paint(Graphics g) {
update(g);
}

public void update(Graphics g) {
... do your double-buffer & actual painting in here.
}
Um, isn''t that backwards? Isn''t update() supposed to call paint(), not the other way around?

Anyway, I''ve tried overriding both update() and paint(), and either way, it just made the flickering worse. I''ll put up some demonstrations later today.
Ok, I got it sorted out... I needed to override update() in my applet, in addition to in my component.

This topic is closed to new replies.

Advertisement