Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q4002 How can I preload an image before I paint it?

You are here: irt.org | FAQ | Java | Q4002 [ previous next ]

Use ImageObserver interface, ImageObserver is an interface used to receive notification as an image is being generated. ImageObserver defines only one method: imageUpdate(). The following code waits until the image is completely loaded before snapping it onto the screen in a single repaint.

public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h){
    if ((flags & ALLBITS) != 0{
      repaint();
    } else if (flags & (ABORT|ERROR)) != 0){
      error = true; //file not found
      repaint();
    }
    return (flags & (ALLBITS|ABORT|ERROR)) == 0;
}

©2018 Martin Webb