1 7 8 17 18 package java.awt.image.renderable; 19 import java.awt.color.ColorSpace ; 20 import java.awt.image.ColorModel ; 21 import java.awt.image.DataBuffer ; 22 import java.awt.image.DirectColorModel ; 23 import java.awt.image.ImageConsumer ; 24 import java.awt.image.ImageProducer ; 25 import java.awt.image.Raster ; 26 import java.awt.image.RenderedImage ; 27 import java.awt.image.SampleModel ; 28 import java.util.Enumeration ; 29 import java.util.Vector ; 30 31 44 public class RenderableImageProducer implements ImageProducer , Runnable { 45 46 47 RenderableImage rdblImage; 48 49 50 RenderContext rc; 51 52 53 Vector ics = new Vector (); 54 55 62 public RenderableImageProducer(RenderableImage rdblImage, 63 RenderContext rc) { 64 this.rdblImage = rdblImage; 65 this.rc = rc; 66 } 67 68 73 public synchronized void setRenderContext(RenderContext rc) { 74 this.rc = rc; 75 } 76 77 83 public synchronized void addConsumer(ImageConsumer ic) { 84 if (!ics.contains(ic)) { 85 ics.addElement(ic); 86 } 87 } 88 89 96 public synchronized boolean isConsumer(ImageConsumer ic) { 97 return ics.contains(ic); 98 } 99 100 106 public synchronized void removeConsumer(ImageConsumer ic) { 107 ics.removeElement(ic); 108 } 109 110 117 public synchronized void startProduction(ImageConsumer ic) { 118 addConsumer(ic); 119 Thread thread = new Thread (this, "RenderableImageProducer Thread"); 121 thread.start(); 122 } 123 124 130 public void requestTopDownLeftRightResend(ImageConsumer ic) { 131 } 133 134 139 public void run() { 140 RenderedImage rdrdImage; 142 if (rc != null) { 143 rdrdImage = rdblImage.createRendering(rc); 144 } else { 145 rdrdImage = rdblImage.createDefaultRendering(); 146 } 147 148 ColorModel colorModel = rdrdImage.getColorModel(); 150 Raster raster = rdrdImage.getData(); 151 SampleModel sampleModel = raster.getSampleModel(); 152 DataBuffer dataBuffer = raster.getDataBuffer(); 153 154 if (colorModel == null) { 155 colorModel = ColorModel.getRGBdefault(); 156 } 157 int minX = raster.getMinX(); 158 int minY = raster.getMinY(); 159 int width = raster.getWidth(); 160 int height = raster.getHeight(); 161 162 Enumeration icList; 163 ImageConsumer ic; 164 icList = ics.elements(); 166 while (icList.hasMoreElements()) { 167 ic = (ImageConsumer )icList.nextElement(); 168 ic.setDimensions(width,height); 169 ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT | 170 ImageConsumer.COMPLETESCANLINES | 171 ImageConsumer.SINGLEPASS | 172 ImageConsumer.SINGLEFRAME); 173 } 174 175 int pix[] = new int[width]; 178 int i,j; 179 int numBands = sampleModel.getNumBands(); 180 int tmpPixel[] = new int[numBands]; 181 for (j = 0; j < height; j++) { 182 for(i = 0; i < width; i++) { 183 sampleModel.getPixel(i, j, tmpPixel, dataBuffer); 184 pix[i] = colorModel.getDataElement(tmpPixel, 0); 185 } 186 icList = ics.elements(); 188 while (icList.hasMoreElements()) { 189 ic = (ImageConsumer )icList.nextElement(); 190 ic.setPixels(0, j, width, 1, colorModel, pix, 0, width); 191 } 192 } 193 194 icList = ics.elements(); 196 while (icList.hasMoreElements()) { 197 ic = (ImageConsumer )icList.nextElement(); 198 ic.imageComplete(ImageConsumer.STATICIMAGEDONE); 199 } 200 } 201 } 202 | Popular Tags |