1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: public class RenderableImageProducer implements ImageProducer, Runnable
52: {
53: private RenderableImage image;
54: private RenderContext context;
55: private ArrayList consumers = new ArrayList();
56:
57: public RenderableImageProducer(RenderableImage image, RenderContext context)
58: {
59: this.image = image;
60: this.context = context;
61: }
62:
63: public void setRenderContext(RenderContext context)
64: {
65: this.context = context;
66: }
67:
68: public void addConsumer(ImageConsumer consumer)
69: {
70: synchronized (consumers)
71: {
72: if (! consumers.contains(consumer))
73: consumers.add(consumer);
74: }
75: }
76:
77: public boolean isConsumer(ImageConsumer consumer)
78: {
79: synchronized (consumers)
80: {
81: return consumers.contains(consumer);
82: }
83: }
84:
85: public void removeConsumer(ImageConsumer consumer)
86: {
87: synchronized (consumers)
88: {
89: consumers.remove(consumer);
90: }
91: }
92:
93: public void startProduction(ImageConsumer consumer)
94: {
95: addConsumer(consumer);
96: Thread t = new Thread(this, "RenderableImageProducerWorker");
97: t.start();
98: }
99:
100: public void requestTopDownLeftRightResend(ImageConsumer consumer)
101: {
102:
103: }
104:
105: public void run()
106: {
107:
108:
109: synchronized (consumers)
110: {
111: RenderedImage newImage;
112: if (context == null)
113: newImage = image.createDefaultRendering();
114: else
115: newImage = image.createRendering(context);
116: Raster newData = newImage.getData();
117: ColorModel colorModel = newImage.getColorModel();
118: if (colorModel == null)
119: colorModel = ColorModel.getRGBdefault();
120: SampleModel sampleModel = newData.getSampleModel();
121: DataBuffer dataBuffer = newData.getDataBuffer();
122: int width = newData.getWidth();
123: int height = newData.getHeight();
124:
125:
126: Iterator it = consumers.iterator();
127: while (it.hasNext())
128: {
129: ImageConsumer target = (ImageConsumer) it.next();
130: target.setHints(ImageConsumer.COMPLETESCANLINES
131: | ImageConsumer.SINGLEFRAME
132: | ImageConsumer.SINGLEPASS
133: | ImageConsumer.TOPDOWNLEFTRIGHT);
134: target.setDimensions(width, height);
135: }
136:
137:
138: int[] newLine = new int[width];
139: int[] bands = new int[sampleModel.getNumBands()];
140: for (int y = 0; y < height; ++y)
141: {
142: for (int x = 0; x < width; ++x)
143: {
144: sampleModel.getPixel(x, y, bands, dataBuffer);
145: newLine[x] = colorModel.getDataElement(bands, 0);
146: }
147:
148:
149: it = consumers.iterator();
150: while (it.hasNext())
151: {
152: ImageConsumer target = (ImageConsumer) it.next();
153: target.setPixels(0, y, width, 1, colorModel, newLine, 0, width);
154: }
155: }
156:
157:
158: it = consumers.iterator();
159: while (it.hasNext())
160: {
161: ImageConsumer target = (ImageConsumer) it.next();
162: target.imageComplete(ImageConsumer.STATICIMAGEDONE);
163: }
164: }
165: }
166: }