1 7 8 package javax.swing.colorchooser; 9 10 import java.awt.*; 11 import java.awt.image.*; 12 13 40 abstract class SyntheticImage implements ImageProducer { 41 private SyntheticImageGenerator root; 42 protected int width=10, height=100; 43 static final ColorModel cm = ColorModel.getRGBdefault(); 44 public static final int pixMask = 0xFF; 45 private Thread runner; 46 protected SyntheticImage() { } 47 protected SyntheticImage(int w, int h) { width = w; height = h; } 48 protected void computeRow(int y, int[] row) { 49 int p = 255-255*y/(height-1); 50 p = (pixMask<<24)|(p<<16)|(p<<8)|p; 51 for (int i = row.length; --i>=0; ) row[i] = p; 52 } 53 public synchronized void addConsumer(ImageConsumer ic){ 54 for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next) 55 if (ics.ic == ic) return; 56 root = new SyntheticImageGenerator(ic, root, this); 57 } 58 public synchronized boolean isConsumer(ImageConsumer ic){ 59 for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next) 60 if (ics.ic == ic) return true; 61 return false; 62 } 63 public synchronized void removeConsumer(ImageConsumer ic) { 64 SyntheticImageGenerator prev = null; 65 for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next) { 66 if (ics.ic == ic) { 67 ics.useful = false; 68 if (prev!=null) prev.next = ics.next; 69 else root = ics.next; 70 return; 71 } 72 prev = ics; 73 } 74 } 75 public synchronized void startProduction(ImageConsumer ic) { 76 addConsumer(ic); 77 for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next) 78 if (ics.useful && !ics.isAlive()) 79 ics.start(); 80 } 81 protected boolean isStatic() { return true; } 82 public void nextFrame(int param) {} public void requestTopDownLeftRightResend(ImageConsumer ic){} 84 85 protected volatile boolean aborted = false; 86 } 87 88 class SyntheticImageGenerator extends Thread { 89 ImageConsumer ic; 90 boolean useful; 91 SyntheticImageGenerator next; 92 SyntheticImage parent; 93 SyntheticImageGenerator(ImageConsumer ic, SyntheticImageGenerator next, 94 SyntheticImage parent) { 95 super("SyntheticImageGenerator"); 96 this.ic = ic; 97 this.next = next; 98 this.parent = parent; 99 useful = true; 100 setDaemon(true); 101 } 102 public void run() { 103 ImageConsumer ic = this.ic; 104 int w = parent.width; 105 int h = parent.height; 106 int hints = ic.SINGLEPASS|ic.COMPLETESCANLINES|ic.TOPDOWNLEFTRIGHT; 107 if (parent.isStatic()) 108 hints |= ic.SINGLEFRAME; 109 ic.setHints(hints); 110 ic.setDimensions(w, h); 111 ic.setProperties(null); 112 ic.setColorModel(parent.cm); 113 114 if (useful) { 115 int[] row=new int[w]; 116 doPrivileged( new Runnable () { 117 public void run() { 118 Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 119 } 120 }); 121 122 do { 123 for (int y = 0; y<h && useful; y++) { 124 parent.computeRow(y,row); 125 126 if (parent.aborted) { 127 ic.imageComplete(ic.IMAGEABORTED); 128 return; 129 } 130 131 ic.setPixels(0, y, w, 1, parent.cm, row, 0, w); 132 } 133 ic.imageComplete(parent.isStatic() ? ic.STATICIMAGEDONE 134 : ic.SINGLEFRAMEDONE ); 135 } while(!parent.isStatic() && useful); 136 } 137 } 138 139 private final static void doPrivileged(final Runnable doRun) { 140 java.security.AccessController.doPrivileged( 141 new java.security.PrivilegedAction () { 142 public Object run() { 143 doRun.run(); 144 return null; 145 } 146 } 147 ); 148 } 149 } 150 | Popular Tags |