MemoryImageSource.java [plain text]
package java.awt.image;
import java.awt.Image;
import java.util.Enumeration;
import java.util.Hashtable;
public class MemoryImageSource implements ImageProducer
{
private boolean animated = false;
private boolean fullbuffers = false;
private int pixeli[], width, height, offset, scansize;
private byte pixelb[];
private ColorModel cm;
private Hashtable props, consumers = new Hashtable();
public MemoryImageSource(int w, int h, ColorModel cm,
byte pix[], int off, int scan)
{
this ( w, h, cm, pix, off, scan, null );
}
public MemoryImageSource( int w, int h, ColorModel cm,
byte pix[], int off, int scan,
Hashtable props)
{
width = w;
height = h;
this.cm = cm;
offset = off;
scansize = scan;
this.props = props;
int max = (( scansize > width ) ? scansize : width );
pixelb = new byte[ max * height ];
System.arraycopy( pix, 0, pixelb, 0, max );
}
public MemoryImageSource(int w, int h, ColorModel cm,
int pix[], int off, int scan)
{
this ( w, h, cm, pix, off, scan, null );
}
public MemoryImageSource(int w, int h, ColorModel cm,
int pix[], int off, int scan,
Hashtable props)
{
width = w;
height = h;
this.cm = cm;
offset = off;
scansize = scan;
this.props = props;
int max = (( scansize > width ) ? scansize : width );
pixeli = new int[ max * height ];
System.arraycopy( pix, 0, pixeli, 0, max );
}
public MemoryImageSource(int w, int h,
int pix[], int off, int scan,
Hashtable props)
{
this ( w, h, ColorModel.getRGBdefault(), pix, off, scan, props);
}
public MemoryImageSource(int w, int h,
int pix[], int off, int scan)
{
this ( w, h, ColorModel.getRGBdefault(), pix, off, scan, null);
}
public synchronized void addConsumer(ImageConsumer ic) {
if (consumers.containsKey(ic))
return;
consumers.put(ic, ic);
}
public synchronized boolean isConsumer(ImageConsumer ic) {
if (consumers.containsKey(ic))
return true;
return false;
}
public synchronized void removeConsumer(ImageConsumer ic) {
consumers.remove(ic);
}
public void startProduction(ImageConsumer ic) {
if (!(consumers.containsKey(ic))) {
consumers.put(ic, ic);
}
Enumeration e = consumers.elements();
for( ; e.hasMoreElements(); ) {
ic = (ImageConsumer)e.nextElement();
sendPicture( ic );
ic.imageComplete( ImageConsumer.SINGLEFRAME );
}
}
public void requestTopDownLeftRightResend(ImageConsumer ic) {
startProduction ( ic );
}
public synchronized void setAnimated(boolean animated)
{
this.animated = animated;
}
public synchronized void setFullBufferUpdates(boolean fullbuffers)
{
this.fullbuffers = fullbuffers;
}
public void newPixels()
{
if( animated == true ) {
ImageConsumer ic;
Enumeration e = consumers.elements();
for( ; e.hasMoreElements(); ) {
ic = (ImageConsumer)e.nextElement();
sendPicture( ic );
ic.imageComplete( ImageConsumer.SINGLEFRAME );
}
}
}
private void sendPicture ( ImageConsumer ic )
{
ic.setHints( ImageConsumer.TOPDOWNLEFTRIGHT );
if( props != null ) {
ic.setProperties( props );
}
if( pixeli != null ) {
ic.setPixels( 0, 0, width, height, cm, pixeli, offset, scansize );
} else {
ic.setPixels( 0, 0, width, height, cm, pixelb, offset, scansize );
}
}
public synchronized void newPixels(int x,
int y,
int w,
int h)
{
if( animated == true )
{
if( fullbuffers ) {
newPixels();
} else {
ImageConsumer ic;
Enumeration e = consumers.elements();
for( ; e.hasMoreElements(); ) {
ic = (ImageConsumer)e.nextElement();
ic.setHints( ImageConsumer.TOPDOWNLEFTRIGHT );
if( props != null ) {
ic.setProperties( props );
}
if( pixeli != null ) {
ic.setPixels( 0, 0, width, height, cm, pixeli, offset, scansize );
} else {
ic.setPixels( 0, 0, width, height, cm, pixelb, offset, scansize );
}
ic.imageComplete( ImageConsumer.SINGLEFRAME );
}
}
}
}
public synchronized void newPixels(int x,
int y,
int w,
int h,
boolean framenotify)
{
if( animated == true )
{
if( fullbuffers ) {
newPixels();
} else {
ImageConsumer ic;
Enumeration e = consumers.elements();
for( ; e.hasMoreElements(); ) {
ic = (ImageConsumer)e.nextElement();
ic.setHints( ImageConsumer.TOPDOWNLEFTRIGHT );
if( props != null ) {
ic.setProperties( props );
}
if( pixeli != null ) {
ic.setPixels( 0, 0, width, height, cm, pixeli, offset, scansize );
} else {
ic.setPixels( 0, 0, width, height, cm, pixelb, offset, scansize );
}
if( framenotify == true )
ic.imageComplete( ImageConsumer.SINGLEFRAME );
}
}
}
}
public synchronized void newPixels(byte newpix[],
ColorModel newmodel,
int offset,
int scansize)
{
if( animated == true )
{
}
}
public synchronized void newPixels(int newpix[],
ColorModel newmodel,
int offset,
int scansize)
{
if( animated == true )
{
}
}
}