Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Malone1234

[java] Java applet - double buffering?

This topic is 5631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!