Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

Double Buffering?

Thread view: 
Matt Smith - 22 Jan 2004 15:29 GMT
Hello,

I would like to implement double buffering. I have done it before by
introducing a new "update( Graphics g )" method, which is automatically
called by the "paint( Graphics g ) " method.

But this time I wrote a new method, like the one below, without using the
"paint( Graphics g )" method  but the "getGraphics ()".
How can I still implement double buffering???

Cheers

public void doSomething( ) {

    Graphics2D g2 = (Graphics2D) this.getGraphics();

    Rectangle2D tt = new Rectangle( x, y, xSize, ySize );
    g2.fill( tt );
}
Jon A. Cruz - 22 Jan 2004 21:40 GMT
> Hello,
>
[quoted text clipped - 5 lines]
> "paint( Graphics g )" method  but the "getGraphics ()".
> How can I still implement double buffering???

Just use Swing. Then you get automatic double buffering.

No more need for all that low-level mucking about.
Alex Hunsley - 23 Jan 2004 00:01 GMT
>> Hello,
>>
[quoted text clipped - 9 lines]
>
> No more need for all that low-level mucking about.

There is, if his painting is time consuming. In that case, just using
Swing will result in a blocked event despatch thread and an unresponsive
application.
Ryan Stewart - 22 Jan 2004 22:10 GMT
> Hello,
>
[quoted text clipped - 15 lines]
>      g2.fill( tt );
> }

What Jon said or create a BufferedImage and its Graphics object as class
variables, render everything to the BufferedImage first, and then copy the
image to the actual drawing surface using Graphics.drawImage().
Matt Smith - 23 Jan 2004 00:27 GMT
> What Jon said or create a BufferedImage and its Graphics object as class
> variables, render everything to the BufferedImage first, and then copy the
> image to the actual drawing surface using Graphics.drawImage().

Unfortunately just using Swing is not sufficient, but I don't know how to
implement the other solution exactly.
Jon A. Cruz - 23 Jan 2004 17:11 GMT
> Unfortunately just using Swing is not sufficient,

Are you sure?

How are you trying to do things?

You're not overriding paint() or update() instead of paintComponent(),
are you?

> but I don't know how to
> implement the other solution exactly.

http://java.sun.com/docs/books/tutorial/2d/images/doublebuffering.html
Jon A. Cruz - 23 Jan 2004 17:13 GMT
> http://java.sun.com/docs/books/tutorial/2d/images/doublebuffering.html

Also, for a little more background...

http://java.sun.com/docs/books/tutorial/extra/fullscreen/doublebuf.html
ak - 23 Jan 2004 02:14 GMT
> I would like to implement double buffering. I have done it before by
> introducing a new "update( Graphics g )" method, which is automatically
> called by the "paint( Graphics g ) " method.

*
* @(#)DoubleBufferPanel.java        1.2 97/01/14 Jeff Dinkins
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
*/

package actual;

import java.awt.*;
import java.applet.*;

public class DoubleBufferPanel extends Panel {

 Image offscreen;

 /**
  * null out the offscreen buffer as part of invalidation
  */
 public void invalidate() {
     super.invalidate();
     offscreen = null;
 }

 /**
  * override update to *not* erase the background before painting

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

 /**
  * paint children into an offscreen buffer, then blast entire image
  * at once.
  */
 public void paint(Graphics g) {
     if(offscreen == null) {
        offscreen = createImage(getSize().width, getSize().height);
     }

     Graphics og = offscreen.getGraphics();
     og.setClip(0,0,getSize().width, getSize().height);
     super.paint(og);
     g.drawImage(offscreen, 0, 0, null);

     og.dispose();
 }
}

> But this time I wrote a new method, like the one below, without using the
> "paint( Graphics g )" method  but the "getGraphics ()".
> How can I still implement double buffering???

same way.

____________

http://reader.imagero.com the best java image reader.
Andrew Thompson - 23 Jan 2004 04:28 GMT
| > I would like to implement double buffering.
...
| *
|  * @(#)DoubleBufferPanel.java        1.2 97/01/14 Jeff Dinkins
|  *
|  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.

Hey ak.  I updated some source to use
DoubleBuffering, using that sample above
as a guide If you get a moment could you
have a look at it for me?

( Or for that matter, anybody who
is interested! )

It seems to work fine, but I am tired. It is here..
http://www.physci.org/launcher.jsp#DoubleBufferedAnimateFrame

I based it on the automatically double-buffered
http://www.physci.org/launcher.jsp#JAnimateFrame
and Alberto's non-double buffered AWT original..
http://www.physci.org/launcher.jsp#AnimateFrame

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Ryan Stewart - 23 Jan 2004 13:19 GMT
> Hey ak.  I updated some source to use
> DoubleBuffering, using that sample above
[quoted text clipped - 11 lines]
> and Alberto's non-double buffered AWT original..
> http://www.physci.org/launcher.jsp#AnimateFrame

Question: How do you consider the JAnimateFrame to be double-buffered?
ak - 23 Jan 2004 14:15 GMT
> Question: How do you consider the JAnimateFrame to be double-buffered?

_swing_makes_it_for_you_

____________

http://reader.imagero.com the best java image reader.
Ryan Stewart - 23 Jan 2004 22:51 GMT
> > Question: How do you consider the JAnimateFrame to be double-buffered?
> >
> _swing_makes_it_for_you_

Ah, I think I remember reading that somewhere now. I've just been ignoring
repaints and doing my own drawing. Is there a benefit to doing it the other
way?
ak - 24 Jan 2004 01:01 GMT
> Ah, I think I remember reading that somewhere now. I've just been ignoring
> repaints and doing my own drawing. Is there a benefit to doing it the other
> way?

sure

____________

http://reader.imagero.com the best java image reader.
Ryan Stewart - 24 Jan 2004 13:58 GMT
> > Ah, I think I remember reading that somewhere now. I've just been ignoring
> > repaints and doing my own drawing. Is there a benefit to doing it the
> other
> > way?
>
> sure

Immensely helpful. Thanks.
ak - 23 Jan 2004 14:14 GMT
> Hey ak.  I updated some source to use
> DoubleBuffering, using that sample above
[quoted text clipped - 6 lines]
> It seems to work fine, but I am tired. It is here..
> http://www.physci.org/launcher.jsp#DoubleBufferedAnimateFrame
yes, it's ok.
But, I would change something to keep balls on screen:

____________

http://reader.imagero.com the best java image reader.
Andrew Thompson - 23 Jan 2004 20:57 GMT
| > Hey ak.  I updated some source to use
| > DoubleBuffering, using that sample above
..
| But, I would change something to keep balls on screen:

Good Point!  I'll do ..something..

I am glad I got involved in those three
examples, DoubleBuffering has become
a little more clear.

It is also interesting to _see_ the difference
between the original form and both the
Swing and DoubleBuffered versions.

To those that have not followed the links,
you can see the app's within 2 clicks of a web
link.  All of them are launchable right then
and their.  They can all be found through
http://www.physci.org/launcher.jsp
code AnimateFrame, JAnimateFrame
and DoubleBufferedAnimateFrame.

A very quick way to see for yourself.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Matt Smith - 25 Jan 2004 16:24 GMT
Thanks for all answers.

Unfortunately I still can't get it properly. The problem is:
I'm painting on a JComponent, there's an image in the background and
there are already several objects ( circle, etc. ) drawn on that background.

I don't have the coordinates of these objects any more, which means I can't
use repaint(), etc. . I can't clear the whole image. ( I don't use paint(),
update() .. ).

Is there a method which gives me the current content of the JComponent and
allows me, unlike this.getGraphics() , to render on a copied version of this
content
first before I use this.getGraphics.draw( ... )  to finally put it onto the
screen?

Does somebody know where my problem is / what I'm doing wrong?

Cheers
Andrew Thompson - 25 Jan 2004 17:25 GMT
"Matt Smith" ...
...
| Unfortunately I still can't get it properly.

Have you had a look over the example
I put up during this discussion?  It is here..
http://www.physci.org/launcher.jsp#DoubleBufferedAnimateFrame

It might be at the stage where _you_
should provide an example, rather than
posting code snippets..
http://www.physci.org/codes/sscce.jsp

Please read the document carefully, you
will not be making any friends if you dump
26Kb of tortuous code on the group.

Note that the working example requires
only 4.4Kb..

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Ryan Stewart - 25 Jan 2004 18:46 GMT
> Thanks for all answers.
>
[quoted text clipped - 15 lines]
>
> Cheers

If you have a background and you drew on it, why not keep that copy of the
background to use? But if you draw things on the background and then forget
about them, shouldn't they be part of the background to start with?
Anders - 25 Jan 2004 19:08 GMT
> Thanks for all answers.
>
[quoted text clipped - 15 lines]
>
> Cheers

Here is how i do:

Step 1: Make an Image for dubbelbuffering and get the Graphics object
for this Image. Make this object a "global" variable so you can paint
everywhere in the code.

Step 2: When you want the screen to update call update( getGraphics() ).

Step 3: Override paint and update like this:

paint(Graphic g) { update(g); }

update(Graphic g) {
   g.drawImage(.....);  // draw the doublebuffered image
}

This is the way that I have managed to get full control of the java
painting system :-)

mvh Martin
Jon A. Cruz - 25 Jan 2004 19:18 GMT
> Thanks for all answers.
>
> Unfortunately I still can't get it properly. The problem is:
> I'm painting on a JComponent, there's an image in the background and
> there are already several objects ( circle, etc. ) drawn on that background.

Why?

Why are there things on the background, and are they actually *part* of
the background?

This sounds more like an problem with approach. Could be some simple way
of shifting things to be more "java-ish" that would solve things for
you. At the moment it sounds like you're doing it half-and-half.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.