1 7 8 package java.awt.image; 9 10 import java.awt.image.ImageConsumer ; 11 import java.awt.image.ImageProducer ; 12 import java.awt.image.ColorModel ; 13 import java.util.Hashtable ; 14 import java.util.Vector ; 15 import java.util.Enumeration ; 16 17 90 public class MemoryImageSource implements ImageProducer { 91 int width; 92 int height; 93 ColorModel model; 94 Object pixels; 95 int pixeloffset; 96 int pixelscan; 97 Hashtable properties; 98 Vector theConsumers = new Vector (); 99 boolean animating; 100 boolean fullbuffers; 101 102 115 public MemoryImageSource(int w, int h, ColorModel cm, 116 byte[] pix, int off, int scan) { 117 initialize(w, h, cm, (Object ) pix, off, scan, null); 118 } 119 120 135 public MemoryImageSource(int w, int h, ColorModel cm, 136 byte[] pix, int off, int scan, 137 Hashtable <?,?> props) 138 { 139 initialize(w, h, cm, (Object ) pix, off, scan, props); 140 } 141 142 155 public MemoryImageSource(int w, int h, ColorModel cm, 156 int[] pix, int off, int scan) { 157 initialize(w, h, cm, (Object ) pix, off, scan, null); 158 } 159 160 175 public MemoryImageSource(int w, int h, ColorModel cm, 176 int[] pix, int off, int scan, 177 Hashtable <?,?> props) 178 { 179 initialize(w, h, cm, (Object ) pix, off, scan, props); 180 } 181 182 private void initialize(int w, int h, ColorModel cm, 183 Object pix, int off, int scan, Hashtable props) { 184 width = w; 185 height = h; 186 model = cm; 187 pixels = pix; 188 pixeloffset = off; 189 pixelscan = scan; 190 if (props == null) { 191 props = new Hashtable (); 192 } 193 properties = props; 194 } 195 196 209 public MemoryImageSource(int w, int h, int pix[], int off, int scan) { 210 initialize(w, h, ColorModel.getRGBdefault(), 211 (Object ) pix, off, scan, null); 212 } 213 214 229 public MemoryImageSource(int w, int h, int pix[], int off, int scan, 230 Hashtable <?,?> props) 231 { 232 initialize(w, h, ColorModel.getRGBdefault(), 233 (Object ) pix, off, scan, props); 234 } 235 236 244 public synchronized void addConsumer(ImageConsumer ic) { 245 if (theConsumers.contains(ic)) { 246 return; 247 } 248 theConsumers.addElement(ic); 249 try { 250 initConsumer(ic); 251 sendPixels(ic, 0, 0, width, height); 252 if (isConsumer(ic)) { 253 ic.imageComplete(animating 254 ? ImageConsumer.SINGLEFRAMEDONE 255 : ImageConsumer.STATICIMAGEDONE); 256 if (!animating && isConsumer(ic)) { 257 ic.imageComplete(ImageConsumer.IMAGEERROR); 258 removeConsumer(ic); 259 } 260 } 261 } catch (Exception e) { 262 if (isConsumer(ic)) { 263 ic.imageComplete(ImageConsumer.IMAGEERROR); 264 } 265 } 266 } 267 268 276 public synchronized boolean isConsumer(ImageConsumer ic) { 277 return theConsumers.contains(ic); 278 } 279 280 286 public synchronized void removeConsumer(ImageConsumer ic) { 287 theConsumers.removeElement(ic); 288 } 289 290 298 public void startProduction(ImageConsumer ic) { 299 addConsumer(ic); 300 } 301 302 308 public void requestTopDownLeftRightResend(ImageConsumer ic) { 309 } 312 313 326 public synchronized void setAnimated(boolean animated) { 327 this.animating = animated; 328 if (!animating) { 329 Enumeration enum_ = theConsumers.elements(); 330 while (enum_.hasMoreElements()) { 331 ImageConsumer ic = (ImageConsumer ) enum_.nextElement(); 332 ic.imageComplete(ImageConsumer.STATICIMAGEDONE); 333 if (isConsumer(ic)) { 334 ic.imageComplete(ImageConsumer.IMAGEERROR); 335 } 336 } 337 theConsumers.removeAllElements(); 338 } 339 } 340 341 356 public synchronized void setFullBufferUpdates(boolean fullbuffers) { 357 if (this.fullbuffers == fullbuffers) { 358 return; 359 } 360 this.fullbuffers = fullbuffers; 361 if (animating) { 362 Enumeration enum_ = theConsumers.elements(); 363 while (enum_.hasMoreElements()) { 364 ImageConsumer ic = (ImageConsumer ) enum_.nextElement(); 365 ic.setHints(fullbuffers 366 ? (ImageConsumer.TOPDOWNLEFTRIGHT | 367 ImageConsumer.COMPLETESCANLINES) 368 : ImageConsumer.RANDOMPIXELORDER); 369 } 370 } 371 } 372 373 383 public void newPixels() { 384 newPixels(0, 0, width, height, true); 385 } 386 387 407 public synchronized void newPixels(int x, int y, int w, int h) { 408 newPixels(x, y, w, h, true); 409 } 410 411 434 public synchronized void newPixels(int x, int y, int w, int h, 435 boolean framenotify) { 436 if (animating) { 437 if (fullbuffers) { 438 x = y = 0; 439 w = width; 440 h = height; 441 } else { 442 if (x < 0) { 443 w += x; 444 x = 0; 445 } 446 if (x + w > width) { 447 w = width - x; 448 } 449 if (y < 0) { 450 h += y; 451 y = 0; 452 } 453 if (y + h > height) { 454 h = height - y; 455 } 456 } 457 if ((w <= 0 || h <= 0) && !framenotify) { 458 return; 459 } 460 Enumeration enum_ = theConsumers.elements(); 461 while (enum_.hasMoreElements()) { 462 ImageConsumer ic = (ImageConsumer ) enum_.nextElement(); 463 if (w > 0 && h > 0) { 464 sendPixels(ic, x, y, w, h); 465 } 466 if (framenotify && isConsumer(ic)) { 467 ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE); 468 } 469 } 470 } 471 } 472 473 487 public synchronized void newPixels(byte[] newpix, ColorModel newmodel, 488 int offset, int scansize) { 489 this.pixels = newpix; 490 this.model = newmodel; 491 this.pixeloffset = offset; 492 this.pixelscan = scansize; 493 newPixels(); 494 } 495 496 510 public synchronized void newPixels(int[] newpix, ColorModel newmodel, 511 int offset, int scansize) { 512 this.pixels = newpix; 513 this.model = newmodel; 514 this.pixeloffset = offset; 515 this.pixelscan = scansize; 516 newPixels(); 517 } 518 519 private void initConsumer(ImageConsumer ic) { 520 if (isConsumer(ic)) { 521 ic.setDimensions(width, height); 522 } 523 if (isConsumer(ic)) { 524 ic.setProperties(properties); 525 } 526 if (isConsumer(ic)) { 527 ic.setColorModel(model); 528 } 529 if (isConsumer(ic)) { 530 ic.setHints(animating 531 ? (fullbuffers 532 ? (ImageConsumer.TOPDOWNLEFTRIGHT | 533 ImageConsumer.COMPLETESCANLINES) 534 : ImageConsumer.RANDOMPIXELORDER) 535 : (ImageConsumer.TOPDOWNLEFTRIGHT | 536 ImageConsumer.COMPLETESCANLINES | 537 ImageConsumer.SINGLEPASS | 538 ImageConsumer.SINGLEFRAME)); 539 } 540 } 541 542 private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) { 543 int off = pixeloffset + pixelscan * y + x; 544 if (isConsumer(ic)) { 545 if (pixels instanceof byte[]) { 546 ic.setPixels(x, y, w, h, model, 547 ((byte[]) pixels), off, pixelscan); 548 } else { 549 ic.setPixels(x, y, w, h, model, 550 ((int[]) pixels), off, pixelscan); 551 } 552 } 553 } 554 } 555 | Popular Tags |