Double Buffering Clarification

Hello all,

I’ve been following some java tutorials to move myself outside of the realm of text adventures, but I’m having some difficulty wrapping my head around the concept of double buffering.

The sample code I’ve been examining is as follows:


import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class Game extends JFrame{
	
	int x, y;
	private Image dbImage;
	private Graphics dbg;
	
	public class AL extends KeyAdapter {
	public void keyPressed(KeyEvent e)
	{
	int keyCode = e.getKeyCode();
	if(keyCode == e.VK_LEFT)
	{
		x--;
	}
	if(keyCode == e.VK_RIGHT)
	{
		x++;
	}
	if(keyCode == e.VK_UP)
	{
	y--;	
	}
	if(keyCode == e.VK_DOWN)
	{
	y++;	
	}
	}
	public void keyReleased(KeyEvent e)
	{
		
	}
	}
	
	public Game()
	{
		addKeyListener(new AL());
		setTitle("Java Game");
		setSize(500,500);
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	x = 150;
	y = 150;
	}
	
	public void paint(Graphics g)
	{
		dbImage = createImage(getWidth(),getHeight());
		dbg = dbImage.getGraphics();
		paintComponent(dbg);
		g.drawImage(dbImage, 0, 0, this);
	}
	
	
	public void paintComponent(Graphics g)
	{
		g.fillOval(x, y, 15, 15);
		repaint();
	}
	
	public static void main(String[] args)
	{
		new Game();
	}
}

I get that the paint method is called automatically for some reason, and that the paintComponent method draws the image in its new position every time the paint method is called, and I can see why he needed to convert the image of the screen that’s stored in dbImage to the type graphics so he could pass it to the paintComponent method, but it seems from the code like dbImage, which should be the unmodified older image, is what’s displayed on the screen and the new image isn’t displayed until the paint method is called again so everything would be on a slight lag if paint was called in some sort of loop that updated the screen over and over again, but I’m not sure how the calling of paint works.

I call out to more experienced programmers. Would someone be able to explain to me what’s going on here? I would really appreciate any assistance.