Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] Double buffering in applets


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 DiscoStoo   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 January 2001 - 03:24 PM

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.

Sponsor:

#2 Saluk   Members   -  Reputation: 127

Like
Likes
Like

Posted 09 January 2001 - 04:16 PM

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.

#3 gdalston   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 January 2001 - 09:03 PM

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.

#4 DJNattyP   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 January 2001 - 05:39 AM

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

#5 DiscoStoo   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 January 2001 - 02:03 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.

#6 deakin   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 January 2001 - 03:59 PM

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


#7 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 21 January 2001 - 12:26 PM

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

#8 JasonB   Members   -  Reputation: 122

Like
Likes
Like

Posted 21 January 2001 - 08:58 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.
}

#9 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 January 2001 - 04:18 AM

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.

#10 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 January 2001 - 08:02 PM

Ok, I got it sorted out... I needed to override update() in my applet, in addition to in my component.

#11 JasonB   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 January 2001 - 09:17 PM

If memory serves me correctly, when a component is painted, paint() is sometimes called, but update() is always called. And the default implementation of update() clears the component - which causes the flickering.

Therefore it''s slightly more efficient for paint to call update and for update to do the painting.


#12 JasonB   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 January 2001 - 02:54 AM

Ignore that last post. I was talking bollocks.

update() is not always called. paint() is usually called when part of a component is uncovered (update is not called in this case apparently)

I still think update is called more often than paint - I seem to recall reading that somewhere - so the efficiency argument is still true.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS