Question

There is ImageObserver ability in Graphics.drawImage() method. What is the easiest way to see this in action? I guess ImageIO.read() returns BufferedImage so it is all in memory. How to get partially loaded images so that observer called?

Was it helpful?

Solution

This seems to call the image observer. If you're calling the drawImage method and the image is still processing, it'll start using the ImageObserver. Scaling is one example which will cause the image to generally be processing.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageObserverDemo extends Box{

    Image bi;
    Image bi2;
    Dimension d = new Dimension(300,200);

    public ImageObserverDemo() {
        super(BoxLayout.Y_AXIS);        
        try {
            bi = ImageIO.read(new URL("http://apod.nasa.gov/apod/image/0304/bluemarble2k_big.jpg"));
            bi = bi.getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH);
            bi2 = new BufferedImage(bi.getWidth(this), bi.getHeight(this), BufferedImage.TYPE_INT_ARGB);
            bi2.getGraphics().drawImage(bi, 0, 0, this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean imageUpdate(final Image img, int infoflags, int x, int y,
            int width, int height) {

        boolean result = true;
        if((infoflags | ImageObserver.FRAMEBITS) == ImageObserver.FRAMEBITS){
            result = false;
        } else if((infoflags | ImageObserver.ALLBITS) == ImageObserver.ALLBITS){
            result = false;
        }

        if(result){
            System.out.println("Image incomplete");
        } else{
            System.out.println("Complete");
        }

        return result;
    }

    @Override
    public Dimension getPreferredSize(){
        return d;
    }

    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ImageObserverDemo());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top