KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > colorchooser > SyntheticImage


1 /*
2  * @(#)SyntheticImage.java 1.24 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7  
8 package javax.swing.colorchooser;
9
10 import java.awt.*;
11 import java.awt.image.*;
12
13 /** A helper class to make computing synthetic images a little easier.
14  * All you need to do is define a subclass that overrides computeRow
15  * to compute a row of the image. It is passed the y coordinate of the
16  * row and an array into which to put the pixels in
17  * <a HREF="http://java.sun.com/products/jdk/1.1/docs/api/java.awt.image.ColorModel.html#getRGBdefault()">
18  * standard ARGB format</a>.
19  * <p>Normal usage looks something like this:
20  * <pre>&nbsp;Image i = createImage(new SyntheticImage(200, 100) {
21  * &nbsp; protected void computeRow(int y, int[] row) {
22  * &nbsp; for(int i = width; --i>=0; ) {
23  * &nbsp; int grey = i*255/(width-1);
24  * &nbsp; row[i] = (255<<24)|(grey<<16)|(grey<<8)|grey;
25  * &nbsp; }
26  * &nbsp; }
27  * &nbsp;}
28  * </pre>This creates a image 200 pixels wide and 100 pixels high
29  * that is a horizontal grey ramp, going from black on the left to
30  * white on the right.
31  * <p>
32  * If the image is to be a movie, override isStatic to return false,
33  * <i>y</i> cycling back to 0 is computeRow's signal that the next
34  * frame has started. It is acceptable (expected?) for computeRow(0,r)
35  * to pause until the appropriate time to start the next frame.
36  *
37  * @version 1.24 12/19/03
38  * @author James Gosling
39  */

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 JavaDoc 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) {}//Override if !isStatic
83
public void requestTopDownLeftRightResend(ImageConsumer ic){}
84     
85     protected volatile boolean aborted = false;
86 }
87
88 class SyntheticImageGenerator extends Thread JavaDoc {
89     ImageConsumer ic;
90     boolean useful;
91     SyntheticImageGenerator next;
92     SyntheticImage JavaDoc parent;
93     SyntheticImageGenerator(ImageConsumer ic, SyntheticImageGenerator next,
94         SyntheticImage JavaDoc 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 JavaDoc() {
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 JavaDoc doRun) {
140         java.security.AccessController.doPrivileged(
141             new java.security.PrivilegedAction JavaDoc() {
142                 public Object JavaDoc run() {
143                   doRun.run();
144                   return null;
145                 }
146             }
147         );
148     }
149 }
150
Popular Tags