View Full Version : Need help understanding doublebuffer

02-26-2012, 05:39 PM
Hi, I am new to java programming and am trying to make a very basic game. I would like to make a full screen gui with the code:
JFrame frame = new JFrame();
frame.setBounds(new Rectangle(new Point(0, 0), tk.getScreenSize()));
and then make the gui print our a graphic.
My problem is that the only way i know how to put stuff on the screen is with the paint(Graphics g) method and this forces me to put all my logic into the paint(Graphics g) method. I was wondering if there was a way to create a graphic outside of the paint method(with like drawImage(Image, int, int, ImageObserver)) and then feed it into the paint method. Thank you in advance for helping me.

02-26-2012, 05:45 PM
IN the end this is what i want:


public class Core{
public Core(){ //constructor
//JFrame code
public static void main(String args[]){
Core gui=new Core();
public void update(){
Graphic g;
//game logic decides what to draw on graphic
paint(g); //Feed the created graphic into paint
public void paint(Graphic g){
//Put graphic on screen?

02-26-2012, 07:29 PM
Graphics can be retrieved off of any Graphical component using .getGraphics(). This will return the Graphic in use.
Best I can describe the idea behind double buffering for the graphics is you track two sets of graphics: your graphic with the intent of being what the graphic will be, and the internal graphic that is. You paint everything you need to your graphic and then swap the graphics during a repaint which minimizes the flicker over using individual component updates.
Here's an example of double buffering. Its used in an applet, but its logic is identical to using it in a Frame: http://www.realapplets.com/tutorial/DoubleBuffering.html. Its usage overrides that of the paint and update methods to handle the double buffering. It looks like the easiest tutorial I could find, but doesn't explain why it works (for which you can try to google a few more for the actual ideas behind double buffering, there are a lot of good examples with step by step as to what does what).