Overlaylayout flickering

Hi I’m using Overlaylayout to arrange my game interface.

The bottom layer contains all the game rendering, and the upper layer contains Swing widgets for the user interface.

My problem is, I’m using the double buffering repaint() method specified in Killer Game Programming in Java to paint the main game area (lower layer) and its causing the upper layer (the Swing widget layer) to flash and disappear.

Here’s the code I use for double-buffering, I’m skeptical if this is the correct way to do it. Thanks.



public void run()
		{
			while (running)
			{
				updateGameState();
				renderGame();
				repaint();

				System.out.println("update!");

				try
				{
					Thread.sleep(200);
				}
				catch (Exception e)
				{
				}
			}
		}


		/**
		 *  Description of the Method
		 */
		private void renderGame()
		{// draw the current frame to an image buffer

			if (dbImage == null)
			{// create the buffer
				dbImage = createImage(getWidth(), getHeight());
				if (dbImage == null)
				{
					return;
				}
				else
				{
					dbg = dbImage.getGraphics();
				}
			}
			dbg.setColor(Color.white);
			dbg.fillRect(0, 0, getWidth(), getHeight());
		}


		/**
		 *  Description of the Method
		 *
		 *@param  g  Description of the Parameter
		 */
		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			if (dbImage != null)
			{
				g.drawImage(dbImage, 0, 0, null);
			}
		}


why do you always keep on creating that bg in you loop?
create it before the loop and then allways repaint it
i think that will run much smoother

maybe adjust the sleeptime bcz 200 ms sleep is only 5 fps