Double Buffering
I'm new to Java programming, and I was wondering if I could get some help with double buffering. Here's what I'm doing: I set up a Graphics object for my off-screen buffer, and draw various shapes to it with the Graphics methods. My question is how do I get that object on to the screen. Is there a method I'm not seeing in the Graphics class? Thanks.
[362 byte] By [
aburridoa] at [2007-9-29 14:54:43]

See my sample, hope this helps!
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class Test
{
public static void main(String [] args)
{
final BufferedImage offScreenBuffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_BGR);
Graphics graphics = offScreenBuffer.createGraphics();
graphics.drawString("hello world",100,100);
Canvas canvas = new Canvas()
{
public void paint(Graphics graphics)
{
graphics.drawImage(offScreenBuffer,0,0,null);
}
};
canvas.setSize(800,600);
JFrame frame = new JFrame();
frame.getContentPane().add(canvas);
frame.pack();
frame.show();
}
}
A small correction:final BufferedImage offScreenBuffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_BGR);
should befinal BufferedImage offScreenBuffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
or how about...final BufferedImage offScreenBuffer;(offScreenBuffer = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatbileImage(800,600)).getRaster().getDataBuffer();:D
True! I've compacted the code slightly. I have a question though; why do you attempt to get the DataBuffer? It only allows single-pixel-access making it utterly useless?
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class Test
{
public static void main(String [] args)
{
JFrame frame = new JFrame();
final BufferedImage offScreenBuffer = frame.getGraphicsConfiguration().createCompatibleImage(800,600);
Graphics graphics = offScreenBuffer.createGraphics();
graphics.drawString("hello world",100,100);
Canvas canvas = new Canvas()
{
public void paint(Graphics graphics)
{
graphics.drawImage(offScreenBuffer,0,0,null);
}
};
canvas.setSize(800,600);
frame.getContentPane().add(canvas);
frame.pack();
frame.show();
}
}
To prevent the image being cached in vram.
Images returned from createCompatibleImage() are managed images, they use the optimal ColorModel for blitting to the display and, if drawn multiple times without modification will be copied into vram.
The optimal ColorModel is a desirable attribute for a backBuffer, however caching in vram however is not.
Grabbing the images DataBuffer prevents the VM from caching the image in vram. ;)
Thanks for the reply. I don't understand it yet because I'm unfamiliar with the swing API, but I'm confident I'll be able to find the info I need in the docs. Thanks again.
> Thanks for the reply. I don't understand it yet
> because I'm unfamiliar with the swing API, but I'm
> confident I'll be able to find the info I need in the
> docs. Thanks again.
Non of the code above is specific to swing, its core AWT.
Its just new 1.4 stuff ;)