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));
The MemoryImageSource is also capable of managing a memory image which
varies over time to allow animation or custom rendering. Here is an
example showing how to set up the animation source and signal changes
in the data (adapted from the MemoryAnimationSourceDemo by Garth Dickie):
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
| Modifier and Type | Field and Description |
|---|---|
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv int | |
| pack-priv ColorModel | |
| pack-priv int | |
| pack-priv Object | |
| pack-priv int | |
| pack-priv Hashtable | |
| pack-priv Vector | |
| pack-priv int |
| Access | Constructor and Description |
|---|---|
| public | MemoryImageSource(int
the width of the rectangle of pixels w, int the height of the rectangle of pixels h, ColorModel the specified cm, byte[] ColorModelan array of pixels pix, int the offset into the array of where to store the
first pixel off, int the distance from one row of pixels to the next in
the array scan)Constructs an ImageProducer object which uses an array of bytes to produce data for an Image object. |
| public | MemoryImageSource(int
the width of the rectangle of pixels w, int the height of the rectangle of pixels h, ColorModel the specified cm, byte[] ColorModelan array of pixels pix, int the offset into the array of where to store the
first pixel off, int the distance from one row of pixels to the next in
the array scan, Hashtable<?, ?> a list of properties that the props)ImageProducer
uses to process an imageConstructs an ImageProducer object which uses an array of bytes to produce data for an Image object. |
| public | MemoryImageSource(int
the width of the rectangle of pixels w, int the height of the rectangle of pixels h, ColorModel the specified cm, int[] ColorModelan array of pixels pix, int the offset into the array of where to store the
first pixel off, int the distance from one row of pixels to the next in
the array scan)Constructs an ImageProducer object which uses an array of integers to produce data for an Image object. |
| public | MemoryImageSource(int
the width of the rectangle of pixels w, int the height of the rectangle of pixels h, ColorModel the specified cm, int[] ColorModelan array of pixels pix, int the offset into the array of where to store the
first pixel off, int the distance from one row of pixels to the next in
the array scan, Hashtable<?, ?> a list of properties that the props)ImageProducer
uses to process an imageConstructs an ImageProducer object which uses an array of integers to produce data for an Image object. |
| public | MemoryImageSource(int
the width of the rectangle of pixels w, int the height of the rectangle of pixels h, int[] an array of pixels pix, int the offset into the array of where to store the
first pixel off, int the distance from one row of pixels to the next in
the array scan)Constructs an ImageProducer object which uses an array of integers in the default RGB ColorModel to produce data for an Image object. |
| public | MemoryImageSource(int
the width of the rectangle of pixels w, int the height of the rectangle of pixels h, int[] an array of pixels pix, int the offset into the array of where to store the
first pixel off, int the distance from one row of pixels to the next in
the array scan, Hashtable<?, ?> a list of properties that the props)ImageProducer
uses to process an imageConstructs an ImageProducer object which uses an array of integers in the default RGB ColorModel to produce data for an Image object. |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | addConsumer(ImageConsumer
the specified ic)ImageConsumerImplements java. Adds an ImageConsumer to the list of consumers interested in data for this image. |
| private void | |
| private void | |
| public synchronized boolean | Returns: true if the ImageConsumer
is on the list; false otherwise.the specified ic)ImageConsumerImplements java. Determines if an ImageConsumer is on the list of consumers currently interested in data for this image. |
| public void | newPixels()
Sends a whole new buffer of pixels to any ImageConsumers that are currently interested in the data for this image and notify them that an animation frame is complete. |
| public synchronized void | newPixels(int
the x coordinate of the upper left corner of the rectangle
of pixels to be sent x, int the y coordinate of the upper left corner of the rectangle
of pixels to be sent y, int the width of the rectangle of pixels to be sent w, int the height of the rectangle of pixels to be sent h)Sends a rectangular region of the buffer of pixels to any ImageConsumers that are currently interested in the data for this image and notify them that an animation frame is complete. |
| public synchronized void | newPixels(int
the x coordinate of the upper left corner of the rectangle
of pixels to be sent x, int the y coordinate of the upper left corner of the rectangle
of pixels to be sent y, int the width of the rectangle of pixels to be sent w, int the height of the rectangle of pixels to be sent h, boolean true if the consumers should be sent a
SINGLEFRAMEDONE notificationSends a rectangular region of the buffer of pixels to any ImageConsumers that are currently interested in the data for this image. |
| public synchronized void | newPixels(byte[]
the new pixel array newpix, ColorModel the specified newmodel, int ColorModelthe offset into the array offset, int the distance from one row of pixels to the next in
the array scansize)Changes to a new byte array to hold the pixels for this image. |
| public synchronized void | newPixels(int[]
the new pixel array newpix, ColorModel the specified newmodel, int ColorModelthe offset into the array offset, int the distance from one row of pixels to the next in
the array scansize)Changes to a new int array to hold the pixels for this image. |
| public synchronized void | removeConsumer(ImageConsumer
the specified ic)ImageConsumerImplements java. Removes an ImageConsumer from the list of consumers interested in data for this image. |
| public void | requestTopDownLeftRightResend(ImageConsumer
the specified ic)ImageConsumerImplements java. Requests that a given ImageConsumer have the image data delivered one more time in top-down, left-right order. |
| private void | |
| public synchronized void | setAnimated(boolean
true if the image is a
multi-frame animationChanges this memory image into a multi-frame animation or a single-frame static image depending on the animated parameter. |
| public synchronized void | setFullBufferUpdates(boolean
true if the complete pixel
buffer should always
be sentSpecifies whether this animated memory image should always be updated by sending the complete buffer of pixels whenever there is a change. |
| public void | startProduction(ImageConsumer
the specified ic)ImageConsumer
image data through the ImageConsumer interface.Implements java. Adds an ImageConsumer to the list of consumers interested in data for this image and immediately starts delivery of the image data through the ImageConsumer interface. |
| animating | back to summary |
|---|---|
| pack-priv boolean animating | |
| fullbuffers | back to summary |
|---|---|
| pack-priv boolean fullbuffers | |
| height | back to summary |
|---|---|
| pack-priv int height | |
| model | back to summary |
|---|---|
| pack-priv ColorModel model | |
| pixeloffset | back to summary |
|---|---|
| pack-priv int pixeloffset | |
| pixels | back to summary |
|---|---|
| pack-priv Object pixels | |
| pixelscan | back to summary |
|---|---|
| pack-priv int pixelscan | |
| properties | back to summary |
|---|---|
| pack-priv Hashtable<?, ?> properties | |
| theConsumers | back to summary |
|---|---|
| pack-priv Vector<ImageConsumer> theConsumers | |
| width | back to summary |
|---|---|
| pack-priv int width | |
| MemoryImageSource | back to summary |
|---|---|
| public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan) Constructs an ImageProducer object which uses an array of bytes to produce data for an Image object.
| |
| MemoryImageSource | back to summary |
|---|---|
| public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan, Hashtable<?, ?> props) Constructs an ImageProducer object which uses an array of bytes to produce data for an Image object.
| |
| MemoryImageSource | back to summary |
|---|---|
| public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan) Constructs an ImageProducer object which uses an array of integers to produce data for an Image object.
| |
| MemoryImageSource | back to summary |
|---|---|
| public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off, int scan, Hashtable<?, ?> props) Constructs an ImageProducer object which uses an array of integers to produce data for an Image object.
| |
| MemoryImageSource | back to summary |
|---|---|
| public MemoryImageSource(int w, int h, int[] pix, int off, int scan) Constructs an ImageProducer object which uses an array of integers in the default RGB ColorModel to produce data for an Image object.
| |
| MemoryImageSource | back to summary |
|---|---|
| public MemoryImageSource(int w, int h, int[] pix, int off, int scan, Hashtable<?, ?> props) Constructs an ImageProducer object which uses an array of integers in the default RGB ColorModel to produce data for an Image object.
| |
| addConsumer | back to summary |
|---|---|
| public synchronized void addConsumer(ImageConsumer ic) Implements java. Adds an ImageConsumer to the list of consumers interested in data for this image.
| |
| initConsumer | back to summary |
|---|---|
| private void initConsumer(ImageConsumer ic) | |
| initialize | back to summary |
|---|---|
| private void initialize(int w, int h, ColorModel cm, Object pix, int off, int scan, Hashtable<?, ?> props) | |
| isConsumer | back to summary |
|---|---|
| public synchronized boolean isConsumer(ImageConsumer ic) Implements java. Determines if an ImageConsumer is on the list of consumers currently interested in data for this image.
| |
| newPixels | back to summary |
|---|---|
| public void newPixels() Sends a whole new buffer of pixels to any ImageConsumers that are currently interested in the data for this image and notify them that an animation frame is complete. This method only has effect if the animation flag has been turned on through the setAnimated() method. | |
| newPixels | back to summary |
|---|---|
| public synchronized void newPixels(int x, int y, int w, int h) Sends a rectangular region of the buffer of pixels to any ImageConsumers that are currently interested in the data for this image and notify them that an animation frame is complete. This method only has effect if the animation flag has been turned on through the setAnimated() method. If the full buffer update flag was turned on with the setFullBufferUpdates() method then the rectangle parameters will be ignored and the entire buffer will always be sent.
| |
| newPixels | back to summary |
|---|---|
| public synchronized void newPixels(int x, int y, int w, int h, boolean framenotify) Sends a rectangular region of the buffer of pixels to any ImageConsumers that are currently interested in the data for this image. If the framenotify parameter is true then the consumers are also notified that an animation frame is complete. This method only has effect if the animation flag has been turned on through the setAnimated() method. If the full buffer update flag was turned on with the setFullBufferUpdates() method then the rectangle parameters will be ignored and the entire buffer will always be sent.
| |
| newPixels | back to summary |
|---|---|
| public synchronized void newPixels(byte[] newpix, ColorModel newmodel, int offset, int scansize) Changes to a new byte array to hold the pixels for this image. If the animation flag has been turned on through the setAnimated() method, then the new pixels will be immediately delivered to any ImageConsumers that are currently interested in the data for this image.
| |
| newPixels | back to summary |
|---|---|
| public synchronized void newPixels(int[] newpix, ColorModel newmodel, int offset, int scansize) Changes to a new int array to hold the pixels for this image. If the animation flag has been turned on through the setAnimated() method, then the new pixels will be immediately delivered to any ImageConsumers that are currently interested in the data for this image.
| |
| removeConsumer | back to summary |
|---|---|
| public synchronized void removeConsumer(ImageConsumer ic) Implements java. Removes an ImageConsumer from the list of consumers interested in data for this image.
| |
| requestTopDownLeftRightResend | back to summary |
|---|---|
| public void requestTopDownLeftRightResend(ImageConsumer ic) Implements java. Requests that a given ImageConsumer have the image data delivered one more time in top-down, left-right order.
| |
| sendPixels | back to summary |
|---|---|
| private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) | |
| setAnimated | back to summary |
|---|---|
| public synchronized void setAnimated(boolean animated) Changes this memory image into a multi-frame animation or a single-frame static image depending on the animated parameter. This method should be called immediately after the MemoryImageSource is constructed and before an image is created with it to ensure that all ImageConsumers will receive the correct multi-frame data. If an ImageConsumer is added to this ImageProducer before this flag is set then that ImageConsumer will see only a snapshot of the pixel data that was available when it connected.
| |
| setFullBufferUpdates | back to summary |
|---|---|
| public synchronized void setFullBufferUpdates(boolean fullbuffers) Specifies whether this animated memory image should always be updated by sending the complete buffer of pixels whenever there is a change. This flag is ignored if the animation flag is not turned on through the setAnimated() method. This method should be called immediately after the MemoryImageSource is constructed and before an image is created with it to ensure that all ImageConsumers will receive the correct pixel delivery hints.
| |
| startProduction | back to summary |
|---|---|
| public void startProduction(ImageConsumer ic) Implements java. Adds an ImageConsumer to the list of consumers interested in data for this image and immediately starts delivery of the image data through the ImageConsumer interface.
| |