java.lang.Object
java.awt.image.MemoryImageSource
- 所有已实现的接口:
ImageProducer
此类是 ImageProducer 接口的实现,它使用数组为图像生成像素值。下面是一个计算 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);
}
}
- 参见:
-
构造方法总结
构造方法构造方法描述MemoryImageSource(int w, int h, int[] pix, int off, int scan) 构造一个 ImageProducer 对象,该对象使用默认 RGB ColorModel 中的整数数组来为 Image 对象生成数据。MemoryImageSource(int w, int h, int[] pix, int off, int scan, Hashtable<?, ?> props) 构造一个 ImageProducer 对象,该对象使用默认 RGB ColorModel 中的整数数组来为 Image 对象生成数据。MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan) 构造一个 ImageProducer 对象,该对象使用字节数组为 Image 对象生成数据。MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan, Hashtable<?, ?> props) 构造一个 ImageProducer 对象,该对象使用字节数组为 Image 对象生成数据。MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan) 构造一个 ImageProducer 对象,该对象使用整数数组为 Image 对象生成数据。MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan, Hashtable<?, ?> props) 构造一个 ImageProducer 对象,该对象使用整数数组为 Image 对象生成数据。 -
方法总结
修饰符和类型方法描述void将 ImageConsumer 添加到对此图像的数据感兴趣的消费者列表中。boolean确定 ImageConsumer 是否在当前对此图像数据感兴趣的消费者列表中。void向当前对此图像数据感兴趣的任何 ImageConsumer 发送一个全新的像素缓冲区,并通知他们动画帧已完成。voidnewPixels(byte[] newpix, ColorModel newmodel, int offset, int scansize) 更改为新的字节数组以保存此图像的像素。voidnewPixels(int[] newpix, ColorModel newmodel, int offset, int scansize) 更改为一个新的 int 数组以保存此图像的像素。voidnewPixels(int x, int y, int w, int h) 将像素缓冲区的矩形区域发送到当前对此图像数据感兴趣的任何 ImageConsumer,并通知他们动画帧已完成。voidnewPixels(int x, int y, int w, int h, boolean framenotify) 将像素缓冲区的矩形区域发送到当前对此图像的数据感兴趣的任何 ImageConsumer。void从对此图像的数据感兴趣的消费者列表中删除 ImageConsumer。void请求给定的 ImageConsumer 以自上而下、左右的顺序再次传送图像数据。voidsetAnimated(boolean animated) 根据动画参数将此内存图像更改为多帧动画或单帧静态图像。voidsetFullBufferUpdates(boolean fullbuffers) 指定是否应始终通过在发生更改时发送完整的像素缓冲区来更新此动画内存图像。void将 ImageConsumer 添加到对此图像的数据感兴趣的消费者列表中,并立即通过 ImageConsumer 接口开始传送图像数据。
-
构造方法详细信息
-
MemoryImageSource
构造一个 ImageProducer 对象,该对象使用字节数组为 Image 对象生成数据。- 参数:
w- 像素矩形的宽度h- 像素矩形的高度cm- 指定的ColorModelpix- 像素数组off- 存储第一个像素的数组中的偏移量scan- 从一行像素到数组中下一行的距离- 参见:
-
MemoryImageSource
public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan, Hashtable <?, ?> props) 构造一个 ImageProducer 对象,该对象使用字节数组为 Image 对象生成数据。- 参数:
w- 像素矩形的宽度h- 像素矩形的高度cm- 指定的ColorModelpix- 像素数组off- 存储第一个像素的数组中的偏移量scan- 从一行像素到数组中下一行的距离props-ImageProducer用于处理图像的属性列表- 参见:
-
MemoryImageSource
构造一个 ImageProducer 对象,该对象使用整数数组为 Image 对象生成数据。- 参数:
w- 像素矩形的宽度h- 像素矩形的高度cm- 指定的ColorModelpix- 像素数组off- 存储第一个像素的数组中的偏移量scan- 从一行像素到数组中下一行的距离- 参见:
-
MemoryImageSource
public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan, Hashtable <?, ?> props) 构造一个 ImageProducer 对象,该对象使用整数数组为 Image 对象生成数据。- 参数:
w- 像素矩形的宽度h- 像素矩形的高度cm- 指定的ColorModelpix- 像素数组off- 存储第一个像素的数组中的偏移量scan- 从一行像素到数组中下一行的距离props-ImageProducer用于处理图像的属性列表- 参见:
-
MemoryImageSource
public MemoryImageSource(int w, int h, int[] pix, int off, int scan) 构造一个 ImageProducer 对象,该对象使用默认 RGB ColorModel 中的整数数组来为 Image 对象生成数据。- 参数:
w- 像素矩形的宽度h- 像素矩形的高度pix- 像素数组off- 存储第一个像素的数组中的偏移量scan- 从一行像素到数组中下一行的距离- 参见:
-
MemoryImageSource
构造一个 ImageProducer 对象,该对象使用默认 RGB ColorModel 中的整数数组来为 Image 对象生成数据。- 参数:
w- 像素矩形的宽度h- 像素矩形的高度pix- 像素数组off- 存储第一个像素的数组中的偏移量scan- 从一行像素到数组中下一行的距离props-ImageProducer用于处理图像的属性列表- 参见:
-
-
方法详情
-
addConsumer
将 ImageConsumer 添加到对此图像的数据感兴趣的消费者列表中。- 指定者:
addConsumer在接口ImageProducer中- 参数:
ic- 指定的ImageConsumer- 抛出:
NullPointerException- 如果指定的ImageConsumer为空- 参见:
-
isConsumer
确定 ImageConsumer 是否在当前对此图像数据感兴趣的消费者列表中。- 指定者:
isConsumer在接口ImageProducer中- 参数:
ic- 指定的ImageConsumer- 返回:
true如果ImageConsumer在名单上;false否则。- 参见:
-
removeConsumer
从对此图像的数据感兴趣的消费者列表中删除 ImageConsumer。- 指定者:
removeConsumer在接口ImageProducer中- 参数:
ic- 指定的ImageConsumer- 参见:
-
startProduction
将 ImageConsumer 添加到对此图像的数据感兴趣的消费者列表中,并立即通过 ImageConsumer 接口开始传送图像数据。- 指定者:
startProduction在接口ImageProducer中- 参数:
ic- 通过ImageConsumer接口指定ImageConsumer图像数据。- 参见:
-
requestTopDownLeftRightResend
请求给定的 ImageConsumer 以自上而下、左右的顺序再次传送图像数据。- 指定者:
requestTopDownLeftRightResend在接口ImageProducer中- 参数:
ic- 指定的ImageConsumer- 参见:
-
setAnimated
public void setAnimated(boolean animated) 根据动画参数将此内存图像更改为多帧动画或单帧静态图像。应在构造 MemoryImageSource 之后和使用它创建图像之前立即调用此方法,以确保所有 ImageConsumers 都将接收到正确的多帧数据。如果在设置此标志之前将 ImageConsumer 添加到此 ImageProducer,则该 ImageConsumer 将仅看到连接时可用的像素数据的快照。
- 参数:
animated-true如果图像是多帧动画
-
setFullBufferUpdates
public void setFullBufferUpdates(boolean fullbuffers) 指定是否应始终通过在发生更改时发送完整的像素缓冲区来更新此动画内存图像。如果未通过 setAnimated() 方法打开动画标志,则忽略此标志。应在构造 MemoryImageSource 之后和使用它创建图像之前立即调用此方法,以确保所有 ImageConsumers 都将收到正确的像素传递提示。
- 参数:
fullbuffers-true如果应始终发送完整的像素缓冲区- 参见:
-
newPixels
public void newPixels()向当前对此图像数据感兴趣的任何 ImageConsumer 发送一个全新的像素缓冲区,并通知他们动画帧已完成。只有通过 setAnimated() 方法打开动画标志时,此方法才有效。- 参见:
-
newPixels
public void newPixels(int x, int y, int w, int h) 将像素缓冲区的矩形区域发送到当前对此图像数据感兴趣的任何 ImageConsumer,并通知他们动画帧已完成。只有通过 setAnimated() 方法打开动画标志时,此方法才有效。如果使用 setFullBufferUpdates() 方法打开了完整缓冲区更新标志,则矩形参数将被忽略并且将始终发送整个缓冲区。- 参数:
x- 待发送像素矩形左上角的x坐标y- 待发送像素矩形左上角的y坐标w- 要发送的像素矩形的宽度h- 要发送的像素矩形的高度- 参见:
-
newPixels
public void newPixels(int x, int y, int w, int h, boolean framenotify) 将像素缓冲区的矩形区域发送到当前对此图像的数据感兴趣的任何 ImageConsumer。如果 framenotify 参数为真,那么消费者也会收到动画帧已完成的通知。只有通过 setAnimated() 方法打开动画标志时,此方法才有效。如果使用 setFullBufferUpdates() 方法打开了完整缓冲区更新标志,则矩形参数将被忽略并且将始终发送整个缓冲区。- 参数:
x- 待发送像素矩形左上角的x坐标y- 待发送像素矩形左上角的y坐标w- 要发送的像素矩形的宽度h- 要发送的像素矩形的高度framenotify-true如果应向消费者发送SINGLEFRAMEDONE通知- 参见:
-
newPixels
更改为新的字节数组以保存此图像的像素。如果已通过 setAnimated() 方法打开动画标志,则新像素将立即传递给当前对此图像数据感兴趣的任何 ImageConsumers。- 参数:
newpix- 新的像素阵列newmodel- 指定的ColorModeloffset- 数组中的偏移量scansize- 从一行像素到数组中下一行的距离- 参见:
-
newPixels
更改为一个新的 int 数组以保存此图像的像素。如果已通过 setAnimated() 方法打开动画标志,则新像素将立即传递给当前对此图像数据感兴趣的任何 ImageConsumers。- 参数:
newpix- 新的像素阵列newmodel- 指定的ColorModeloffset- 数组中的偏移量scansize- 从一行像素到数组中下一行的距离- 参见:
-