1

方法有ImageObserver能力Graphics.drawImage()。看到这一点的最简单方法是什么?我想ImageIO.read()返回BufferedImage所以它都在内存中。如何获取部分加载的图像以便观察者调用?

4

1 回答 1

3

这似乎调用了图像观察者。如果您正在调用该drawImage方法并且图像仍在处理中,它将开始使用ImageObserver. 缩放是通常会导致图像被处理的一个示例。

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);
    }

}
于 2012-10-05T15:57:16.777 回答