Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with Double Buffering and Swing

807601Mar 16 2008 — edited Mar 17 2008
Hi

I made a game and basically it works pretty well, my only problem is it flickers really badly right now. I read up on a whole lot of forums about double buffering and none of those methods seemed to work. Then I noticed that Swing has double buffering built in so I tried that but then I get compilation errors and I'm really not sure why. My original code was a console application and worked perfectly, then I ported it into a JApplet and it still works but it flickers and thats what I'm tryign to fix now.

The code below is in my main class under the constructor.

Heres the double buffering code I'm trying to use, I'm sure you all seen it before lol
public void update(Graphics g)
	{
		// initialize buffer 
		if (dbImage == null)
		{
			dbImage = createImage(this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics();

		}

		// clear screen in background 
		dbg.setColor(getBackground());
		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

		// draw elements in background 
		dbg.setColor(getForeground());
		paint(dbg);

		// draw image on the screen 
		g.drawImage(dbImage, 0, 0, this);
	}
My paint is right under neath and heres how it looks
This snipet of code works but when I change the method to

public paintComponent(Graphics g){

super.paintComponent(g)...
}

everythign stops working and get a compilation error and says that it can't find paintComponent in javax.swing.JFrame.
public void paint(Graphics g)
	{
		super.paint(g);

		//if game starting display menue
		if (show_menue)
		{
			//to restart lives if player dies
			lives = 3;

			menue.draw_menue(g);
			menue_ufo1.draw_shape(g);

			menue_ufo2.shape_color = Color.DARK_GRAY;
			menue_ufo2.draw_shape(g);

			menue_ufo3.shape_color = Color.BLUE;
			menue_ufo3.draw_shape(g);

			menue_ufo4.shape_color = new Color(82, 157, 22);
			menue_ufo4.draw_shape(g);

			menue_ufo5.draw_shape(g);

			menue_ufo6.shape_color = new Color(130, 3, 3); ;
			menue_ufo6.draw_shape(g);

			menue_turret.draw_ship(g);

			menue_ammo.draw_ammo(g);
		}

		else
		{
			//otherwise redraw game objects 
			gunner.draw_ship(g);

			y_ammo.draw_ammo(g);

			grass.draw_bar(g);
			o_ufo.draw_shape(g);
			b_ufo.draw_shape(g);
			m_ufo.draw_shape(g);
			s_ufo.draw_shape(g);
			z_ufo.draw_shape(g);
			xx_ufo.draw_shape(g);


			info.draw_bar(g);
			live_painter.draw_lives(g, lives);
			score_painter.draw_score(g, score);
			level_display.draw_level(g, level);

			explosion.draw_boom(g);
		}

	}
I just want to get rid of the flickering for now so any help will be greatly appreciated. Depending which will be simpler I can either try to double buffer this program or port it all to swing but I'm not sure which elements are effected by AWT and which by Swing. Also I read some of the Java documentation but couldn't really understand how to implement it to fix my program.

Thanks in advance
Sebastian
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2008
Added on Mar 16 2008
17 comments
898 views