JavaTM Platform
Standard Ed. 6

java.awt.image
类 MemoryImageSource

java.lang.Object
  继承者 java.awt.image.MemoryImageSource
所有已实现的接口:
ImageProducer

public class MemoryImageSource
extends Object
implements ImageProducer

此类是 ImageProducer 接口的一个实现,该接口使用一个数组为 Image 生成像素值。下面的示例计算了一幅 100x100 的图像,表示沿 X 轴从黑色渐变到蓝色,沿 Y 轴从黑色渐变到红色:

 
        int w = 100;
        int h = 100;
        int pix[] = new int[w * h];
        int index = 0;
        for (int y = 0; y < h; y++) {
            int red = (y * 255) / (h - 1);
            for (int x = 0; x < w; x++) {
                int blue = (x * 255) / (w - 1);
                pix[index++] = (255 << 24) | (red << 16) | blue;
            }
        }
        Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
 
 
MemoryImageSource 还能够管理随时间的推移而变化的内存图像,以实现动画或自定义呈现。下面的示例显示了如何设置动画源并通知数据的改变(改编自 Garth Dickie 的 MemoryAnimationSourceDemo):

        int pixels[];
        MemoryImageSource source;

        public void init() {
            int width = 50;
            int height = 50;
            int size = width * height;
            pixels = new int[size];

            int value = getBackground().getRGB();
            for (int i = 0; i < size; i++) {
                pixels[i] = value;
            }

            source = new MemoryImageSource(width, height, pixels, 0, width);
            source.setAnimated(true);
            image = createImage(source);
        }

        public void run() {
            Thread me = Thread.currentThread( );
            me.setPriority(Thread.MIN_PRIORITY);

            while (true) {
                try {
                    thread.sleep(10);
                } catch( InterruptedException e ) {
                    return;
                }

                // Modify the values in the pixels array at (x, y, w, h)

                // Send the new data to the interested ImageConsumers
                source.newPixels(x, y, w, h);
            }
        }

 

另请参见:
ImageProducer

构造方法摘要
MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan)
          构造一个使用 byte 数组为 Image 对象生成数据的 ImageProducer 对象。
MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan, Hashtable<?,?> props)
          构造一个使用 byte 数组为 Image 对象生成数据的 ImageProducer 对象。
MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan)
          构造一个使用整数数组为 Image 对象生成数据的 ImageProducer 对象。
MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan, Hashtable<?,?> props)
          构造一个使用整数数组为 Image 对象生成数据的 ImageProducer 对象。
MemoryImageSource(int w, int h, int[] pix, int off, int scan)
          构造一个使用默认 RGB ColorModel 中的整数数组为 Image 对象生成数据的 ImageProducer 对象。
MemoryImageSource(int w, int h, int[] pix, int off, int scan, Hashtable<?,?> props)
          构造一个使用默认 RGB ColorModel 中的整数数组为 Image 对象生成数据的 ImageProducer 对象。
 
方法摘要
 void addConsumer(ImageConsumer ic)
          将 ImageConsumer 添加到对此图像数据感兴趣的使用者列表。
 boolean isConsumer(ImageConsumer ic)
          确定某个 ImageConsumer 目前是否处于对此图像数据感兴趣的使用者列表中。
 void newPixels()
          将全部的新像素缓冲区发送到所有目前对此图像数据感兴趣的 ImageConsumer,并通知这些 ImageConsumer 一个动画帧已完成。
 void newPixels(byte[] newpix, ColorModel newmodel, int offset, int scansize)
          改用一个新的 byte 数组以保存此图像的像素。
 void newPixels(int[] newpix, ColorModel newmodel, int offset, int scansize)
          改用一个新的 int 数组以保存此图像的像素。
 void newPixels(int x, int y, int w, int h)
          将像素缓冲区的矩形区域发送到所有目前对此图像数据感兴趣的 ImageConsumer,并通知这些 ImageConsumer 一个动画帧已完成。
 void newPixels(int x, int y, int w, int h, boolean framenotify)
          将像素缓冲区的矩形区域发送到所有目前对此图像数据感兴趣的 ImageConsumer。
 void removeConsumer(ImageConsumer ic)
          从对此图像数据感兴趣的使用者列表中移除 ImageConsumer。
 void requestTopDownLeftRightResend(ImageConsumer ic)
          请求给定的 ImageConsumer 再次按从上到下、从左到右的顺序传递图像数据。
 void setAnimated(boolean animated)
          根据 animated 参数将此内存图像更改为多帧动画或单帧静态图像。
 void setFullBufferUpdates(boolean fullbuffers)
          指定在像素缓冲区发生变化时,是否总是通过发送像素的完整缓冲区来更新此动画内存图像。
 void startProduction(ImageConsumer ic)
          将 ImageConsumer 添加到对此图像数据感兴趣的使用者列表中,并立即开始通过 ImageConsumer 接口传递图像数据。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

MemoryImageSource

public MemoryImageSource(int w,
                         int h,
                         ColorModel cm,
                         byte[] pix,
                         int off,
                         int scan)
构造一个使用 byte 数组为 Image 对象生成数据的 ImageProducer 对象。

参数:
w - 像素矩形的宽度
h - 像素矩形的高度
cm - 指定的 ColorModel
pix - 一个像素数组
off - 数组中存储首个像素的偏移量
scan - 数组中一行像素到下一行像素之间的距离
另请参见:
Component.createImage(java.awt.image.ImageProducer)

MemoryImageSource

public MemoryImageSource(int w,
                         int h,
                         ColorModel cm,
                         byte[] pix,
                         int off,
                         int scan,
                         Hashtable<?,?> props)
构造一个使用 byte 数组为 Image 对象生成数据的 ImageProducer 对象。

参数:
w - 像素矩形的宽度
h - 像素矩形的高度
cm - 指定的 ColorModel
pix - 一个像素数组
off - 数组中存储首个像素的偏移量
scan - 数组中一行像素到下一行像素之间的距离
props - ImageProducer 用于处理图像的属性列表
另请参见:
Component.createImage(java.awt.image.ImageProducer)

MemoryImageSource

public MemoryImageSource(int w,
                         int h,
                         ColorModel cm,
                         int[] pix,
                         int off,
                         int scan)
构造一个使用整数数组为 Image 对象生成数据的 ImageProducer 对象。

参数:
w - 像素矩形的宽度
h - 像素矩形的高度
cm - 指定的 ColorModel
pix - 一个像素数组
off - 数组中存储首个像素的偏移量
scan - 数组中一行像素到下一行像素之间的距离
另请参见:
Component.createImage(java.awt.image.ImageProducer)

MemoryImageSource

public MemoryImageSource(int w,
                         int h,
                         ColorModel cm,
                         int[] pix,
                         int off,
                         int scan,
                         Hashtable<?,?> props)
构造一个使用整数数组为 Image 对象生成数据的 ImageProducer 对象。

参数:
w - 像素矩形的宽度
h - 像素矩形的高度
cm - 指定的 ColorModel
pix - 一个像素数组
off - 数组中存储首个像素的偏移量
scan - 数组中一行像素到下一行像素之间的距离
props - ImageProducer 用于处理图像的属性列表
另请参见:
Component.createImage(java.awt.image.ImageProducer)

MemoryImageSource

public MemoryImageSource(int w,
                         int h,
                         int[] pix,
                         int off,
                         int scan)
构造一个使用默认 RGB ColorModel 中的整数数组为 Image 对象生成数据的 ImageProducer 对象。

参数:
w - 像素矩形的宽度
h - 像素矩形的高度
pix - 一个像素数组
off - 数组中存储首个像素的偏移量
scan - 数组中一行像素到下一行像素之间的距离
另请参见:
Component.createImage(java.awt.image.ImageProducer), ColorModel.getRGBdefault()

MemoryImageSource

public MemoryImageSource(int w,
                         int h,
                         int[] pix,
                         int off,
                         int scan,
                         Hashtable<?,?> props)
构造一个使用默认 RGB ColorModel 中的整数数组为 Image 对象生成数据的 ImageProducer 对象。

参数:
w - 像素矩形的宽度
h - 像素矩形的高度
pix - 一个像素数组
off - 数组中存储首个像素的偏移量
scan - 数组中一行像素到下一行像素之间的距离
props - ImageProducer 用于处理图像的属性列表
另请参见:
Component.createImage(java.awt.image.ImageProducer), ColorModel.getRGBdefault()
方法详细信息

addConsumer

public void addConsumer(ImageConsumer ic)
将 ImageConsumer 添加到对此图像数据感兴趣的使用者列表。

指定者:
接口 ImageProducer 中的 addConsumer
参数:
ic - 指定的 ImageConsumer
抛出:
NullPointerException - 如果指定的 ImageConsumer 为 null
另请参见:
ImageConsumer

isConsumer

public boolean isConsumer(ImageConsumer ic)
确定某个 ImageConsumer 目前是否处于对此图像数据感兴趣的使用者列表中。

指定者:
接口 ImageProducer 中的 isConsumer
参数:
ic - 指定的 ImageConsumer
返回:
如果该 ImageConsumer 处于列表中,则返回 true;否则返回 false
另请参见:
ImageConsumer

removeConsumer

public void removeConsumer(ImageConsumer ic)
从对此图像数据感兴趣的使用者列表中移除 ImageConsumer。

指定者:
接口 ImageProducer 中的 removeConsumer
参数:
ic - 指定的 ImageConsumer
另请参见:
ImageConsumer

startProduction

public void startProduction(ImageConsumer ic)
将 ImageConsumer 添加到对此图像数据感兴趣的使用者列表中,并立即开始通过 ImageConsumer 接口传递图像数据。

指定者:
接口 ImageProducer 中的 startProduction
参数:
ic - 通过 ImageConsumer 接口的指定 ImageConsumer 图像数据。
另请参见:
ImageConsumer

requestTopDownLeftRightResend

public void requestTopDownLeftRightResend(ImageConsumer ic)
请求给定的 ImageConsumer 再次按从上到下、从左到右的顺序传递图像数据。

指定者:
接口 ImageProducer 中的 requestTopDownLeftRightResend
参数:
ic - 指定的 ImageConsumer
另请参见:
ImageConsumer

setAnimated

public void setAnimated(boolean animated)
根据 animated 参数将此内存图像更改为多帧动画或单帧静态图像。

应该在构造 MemoryImageSource 后和使用它创建图像前立即调用此方法,以确保所有的 ImageConsumer 都将接收正确的多帧数据。如果在设置此标志前将一个 ImageConsumer 添加到此 ImageProducer 中,则该 ImageConsumer 将只能看到在它连接时可用的一个像素数据快照。

参数:
animated - 如果该图像是一个多帧动画,则为 true

setFullBufferUpdates

public void setFullBufferUpdates(boolean fullbuffers)
指定在像素缓冲区发生变化时,是否总是通过发送像素的完整缓冲区来更新此动画内存图像。如果未通过 setAnimated() 方法打开 animation 标志,则忽略此标志。

应该在构造 MemoryImageSource 后和使用它创建图像前立即调用此方法,以确保所有的 ImageConsumer 都将接收正确的像素传递提示。

参数:
fullbuffers - 如果总是应该发送完整的像素缓冲区,则为 true
另请参见:
setAnimated(boolean)

newPixels

public void newPixels()
将全部的新像素缓冲区发送到所有目前对此图像数据感兴趣的 ImageConsumer,并通知这些 ImageConsumer 一个动画帧已完成。仅在通过 setAnimated() 方法打开 animation 标志时,此方法才有效。

另请参见:
newPixels(int, int, int, int, boolean), ImageConsumer, setAnimated(boolean)

newPixels

public void newPixels(int x,
                      int y,
                      int w,
                      int h)
将像素缓冲区的矩形区域发送到所有目前对此图像数据感兴趣的 ImageConsumer,并通知这些 ImageConsumer 一个动画帧已完成。仅在通过 setAnimated() 方法打开 animation 标志时,此方法才有效。如果已使用 setFullBufferUpdates() 方法打开了全部缓冲区更新标志,则将忽略矩形参数并且始终发送整个缓冲区。

参数:
x - 要发送的像素矩形左上角的 x 坐标
y - 要发送的像素矩形左上角的 y 坐标
w - 要发送的像素矩形的宽度
h - 要发送的像素矩形的高度
另请参见:
newPixels(int, int, int, int, boolean), ImageConsumer, setAnimated(boolean), setFullBufferUpdates(boolean)

newPixels

public void newPixels(int x,
                      int y,
                      int w,
                      int h,
                      boolean framenotify)
将像素缓冲区的矩形区域发送到所有目前对此图像数据感兴趣的 ImageConsumer。如果 framenotify 参数为 true,则通知使用者一幅动画帧已完成。仅在通过 setAnimated() 方法打开 animation 标志时,此方法才有效。如果已使用 setFullBufferUpdates() 方法打开了全部缓冲区更新标志,则将忽略矩形参数并且始终发送整个缓冲区。

参数:
x - 要发送的像素矩形左上角的 x 坐标
y - 要发送的像素矩形左上角的 y 坐标
w - 要发送的像素矩形的宽度
h - 要发送的像素矩形的高度
framenotify - 如果应该向使用者发送 SINGLEFRAMEDONE 通知,则为 true
另请参见:
ImageConsumer, setAnimated(boolean), setFullBufferUpdates(boolean)

newPixels

public void newPixels(byte[] newpix,
                      ColorModel newmodel,
                      int offset,
                      int scansize)
改用一个新的 byte 数组以保存此图像的像素。如果已通过 setAnimated() 方法打开了 animation 标志,则立即将新的像素传递到所有目前对此图像数据感兴趣的 ImageConsumer。

参数:
newpix - 新的像素数组
newmodel - 指定的 ColorModel
offset - 数组中的偏移量
scansize - 数组中一行像素到下一行像素之间的距离
另请参见:
newPixels(int, int, int, int, boolean), setAnimated(boolean)

newPixels

public void newPixels(int[] newpix,
                      ColorModel newmodel,
                      int offset,
                      int scansize)
改用一个新的 int 数组以保存此图像的像素。如果已通过 setAnimated() 方法打开了 animation 标志,则立即将新的像素传递到所有目前对此图像数据感兴趣的 ImageConsumer。

参数:
newpix - 新的像素数组
newmodel - 指定的 ColorModel
offset - 数组中的偏移量
scansize - 数组中一行像素到下一行像素之间的距离
另请参见:
newPixels(int, int, int, int, boolean), setAnimated(boolean)

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策