[java] Java applet - double buffering?

Started by
4 comments, last by Malone1234 20 years, 12 months ago
I know how to double buffer Image objects in java applets, but my problem is I want to double buffer the entire Graphics context because I have an applet where the code does all the drawing. Is this possible? If so, how would I go about doing it? I''ve perused the Java API but I can''t really seem to find anything in the Graphics or Component classes that does what I need. Would JApplet be a better way to go? I remember reading that Swing is a much more advanced graphics API than AWT, but I haven''t had a chance to sit down and learn swing yet.
Advertisement
Something like this (not guranteed to compile: )

EDIT: reread your post and see this isn't what your asking for. No I don't know how to double buffer an awt gc, japplet would apparently be the way to go for this as iirc a graphics2d has douple buffering capability.

public class gamemain extends Applet implements Runnable{	Thread animation;	Graphics backbuffergc;	Image backbuffer;	static final int REFRESH_RATE = 40;	public void init ()	{	showStatus ("loading");		setBackground (Color.black);		width = bounds().width;		height = bounds().height;		backbuffer = createImage(width, height);		backbuffergc = backbuffer.getGraphics();	}        public void start ()	{	showStatus("starting applet");		animation = new Thread (this);		if (animation != null)		{	animation.start();		}	}        public void paint (Graphics g)	{		backbuffergc.setColor(Color.black);		backbuffergc.fillRect(0,0,width,height);		g.drawImage(backbuffer,0,0,this);	}	public void run ()  {	    while (true)		{	repaint ();			Thread.currentThread().yield();			 try			{	Thread.sleep (REFRESH_RATE);			}			catch (Exception exc)			{} 		}	}   




[edited by - nonnus29 on April 20, 2003 8:59:02 PM]
Actually, that sort of helps. I''m still getting some flickering but not as bad as before. Thanks.
You should use a BufferedImage.

    public class gamemain extends Applet implements Runnable {Thread animation;	Graphics backbuffergc;	BufferedImage backbuffer;	static final int REFRESH_RATE = 40;	public void init ()	{	  showStatus ("loading");		  setBackground (Color.black);		  width = bounds().width;		  height = bounds().height;		  backbuffer = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);		  backbuffergc = backbuffer.getGraphics();	}        public void start ()	{	  showStatus("starting applet");		  animation = new Thread (this);		  if (animation != null) {	    animation.start();		  }	}        public void paint (Graphics g)	{  backbuffergc.setColor(Color.black);	  backbuffergc.fillRect(0,0,width,height);	  g.drawImage(backbuffer,0,0,this);	}	public void run ()  {	      while (true) {	    repaint ();			    Thread.currentThread().yield();			     try	{	      Thread.sleep (REFRESH_RATE);    }			    catch (Exception exc){} 		  }	}    


Just replace Image with BufferedImage and replace createImage with new BufferedImage.

[edited by - NuffSaid on April 21, 2003 6:17:35 AM]
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
or you could just code it differently. and use normal double buffering.

[edited by - fadilthrejk on March 19, 2002 at 4:53 AM]
quote:Original post by nonnus29
Something like this (not guranteed to compile: )

EDIT: reread your post and see this isn''t what your asking for. No I don''t know how to double buffer an awt gc, japplet would apparently be the way to go for this as iirc a graphics2d has douple buffering capability.

The advantage swing has is that you have a lot more flexability with all the components. As well swing is supposed to implement double buffering for you. However, if you are doing custom drawing, swing is no better than AWT. Graphics 2D is available to both, you just have to cast the Graphics object that is passed in.

Malone1234: this might help. Avoid calling repaint() and call paint() or update() directly, as shown below.

  public class gamemain extends Applet implements Runnable{	Thread animation;	Graphics backbuffergc;	Image backbuffer;	static final int REFRESH_RATE = 40;	public void init ()	{	showStatus ("loading");		setBackground (Color.black);		width = bounds().width;		height = bounds().height;		backbuffer = createImage(width, height);		backbuffergc = backbuffer.getGraphics();	}        public void start ()	{	showStatus("starting applet");		animation = new Thread (this);		if (animation != null)		{	animation.start();		}	}        public void paint (Graphics g)	{		backbuffergc.setColor(Color.black);		backbuffergc.fillRect(0,0,width,height);		g.drawImage(backbuffer,0,0,this);	}	public void run ()  {	    while (true)		{	paint(getGraphics());			Thread.currentThread().yield();			 try			{	Thread.sleep (REFRESH_RATE);			}			catch (Exception exc)			{} 		}	}  


repaint() only schedules a call to update(). So you could call repaint() 20 times and only get one call to update().


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement