Jump to content
  • Advertisement


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


[java] drawImage, buffer question

This topic is 5425 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

Got a problem with drawImage with Image double buffer. I''m drawing two or more pictures, one large background and other small images on top of the background to the backbuffer then transfer whole thing to display. But the problem, I think, due to the large size of the background image, the small images actually appears on the screen first then a few seconds later the background appears; quite annoying. Is there a way to synchronize and wait until all the images are drawn on the back buffer and then copy it all to the display? Maybe use a second Canvas as backbuffer rather than Image so it is possible to register an ImageObserver and control everything?
public class Display extends Canvas implements Runnable{
  Image gbuffer;
  Graphics bg;
  Thread runner;

  Image image1; //bottom layer large image
  Image image2; //top layer small image

  public Display(){
  runner = new Thread(this);

  public void paint(Graphics g) {
    System.out.println("paint called");

  public void update(Graphics g){
    if (gbuffer == null){
      gbuffer = createImage(240, 240);
      bg = gbuffer.getGraphics();
    bg.drawImage(image1, 0, 0, null);
    bg.drawImage(image2, 0, 0, null);
    g.drawImage(gbuffer, 0, 0, this);
  public void run() {
    while (true) {
      } catch (Exception e){}

Share this post

Link to post
Share on other sites
You should have a look at java.awt.image.BufferStrategy, it allows you to do exactly what you want to do only that it''s also fully hardware supported.

You might also want to have a look at the MediaTracker to make sure your images are fully loaded when you start the program....

Share this post

Link to post
Share on other sites
I don''t know how you are seeing anything at all. You broke the cycle of painting that Java uses. repaint() schedules a call to update, then update calls paint, unless you override it.

Try moving your painting code into paint and in update just call paint.

First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

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!