1
2 package hep.wired.heprep.util;
3
4 import java.awt.*;
5 import java.awt.font.*;
6 import java.awt.geom.*;
7 import java.awt.image.*;
8 import java.io.*;
9 import java.text.*;
10 import java.util.*;
11
12 import org.freehep.graphics2d.VectorGraphics;
13 import org.freehep.graphics2d.TagString;
14 import org.freehep.graphicsio.AbstractVectorGraphicsIO;
15
16 /***
17 * Calculates the bounding box as objects are drawn into this Graphics2D context.
18 *
19 * FIXME WIRED-394, fairly incomplete!
20 * FIXME WIRED-257 should maybe move to FreeHEP...
21 *
22 * @author Mark Donszelmann
23 * @version $Id: BoundingBoxGraphics2D.java 653 2005-03-01 01:12:16Z duns $
24 */
25 public class BoundingBoxGraphics2D
26 extends AbstractVectorGraphicsIO {
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 private Rectangle2D boundingBox = null;
56 private boolean excludeStrings = false;
57
58
59 /***
60 * Returns the current bounding box.
61 */
62 public Rectangle2D getBoundingBox() {
63 return boundingBox;
64 }
65
66 private void union(Shape shape) {
67 Rectangle2D bounds = shape.getBounds2D();
68
69
70 if ((bounds.getWidth() <= 0) && (bounds.getHeight() <=0)) return;
71
72 boundingBox = (boundingBox == null) ? bounds : bounds.createUnion(boundingBox);
73 }
74
75
76
77
78 /***
79 * Create a bounding box graphics context with dimension 800x600.
80 */
81 public BoundingBoxGraphics2D() {
82 this(false);
83 }
84
85 public BoundingBoxGraphics2D(boolean excludeStrings) {
86 super(new Dimension(800,600), false);
87 this.excludeStrings = excludeStrings;
88
89
90
91
92 }
93
94 /***
95 * Create a sub context.
96 */
97 protected BoundingBoxGraphics2D(BoundingBoxGraphics2D graphics, boolean doRestoreOnDispose) {
98 super(graphics, doRestoreOnDispose);
99 System.out.println(getClass()+": Cloned");
100
101
102
103
104 boundingBox = graphics.boundingBox;
105 excludeStrings = graphics.excludeStrings;
106
107 }
108
109
110
111
112
113
114
115
116
117 public void writeHeader() throws IOException {
118 writeWarning(getClass()+": writeHeader() not implemented.");
119
120 }
121
122 public void writeBackground() throws IOException {
123 writeWarning(getClass()+": writeBackground() not implemented.");
124
125 }
126
127 public void writeTrailer() throws IOException {
128 writeWarning(getClass()+": writeTrailer() not implemented.");
129
130 }
131
132 public void closeStream() throws IOException {
133 writeWarning(getClass()+": closeStream() not implemented.");
134
135 }
136
137
138
139
140
141
142
143 public Graphics create() {
144
145 try {
146
147 writeGraphicsSave();
148 } catch (IOException e) {
149 }
150
151 return new BoundingBoxGraphics2D(this, false);
152 }
153
154 public Graphics create(double x, double y, double width, double height) {
155
156 try {
157
158 writeGraphicsSave();
159 } catch (IOException e) {
160 }
161
162 VectorGraphics graphics = new BoundingBoxGraphics2D(this, false);
163 graphics.clipRect(x, y, width, height);
164 return graphics;
165 }
166
167 protected void writeGraphicsSave() throws IOException {
168 writeWarning(getClass()+": writeGraphicsSave() not implemented.");
169
170
171 }
172
173 protected void writeGraphicsRestore() throws IOException {
174 writeWarning(getClass()+": writeGraphicsRestore() not implemented.");
175
176
177 }
178
179
180
181
182
183
184 public void draw(Shape shape) {
185 union(shape);
186 }
187
188 public void fill(Shape shape) {
189 union(shape);
190 }
191
192 public void fillAndDraw(Shape shape, Color fillColor) {
193 union(shape);
194 }
195
196
197 public void copyArea(int x, int y, int width, int height, int dx, int dy) {
198 writeWarning(getClass()+": copyArea(int, int, int, int, int, int) not implemented.");
199
200 }
201
202 protected void writeImage(RenderedImage image, AffineTransform xform, Color bkg) throws IOException {
203 writeWarning(getClass()+": writeImage(RenderedImage, AffineTransform, Color) not implemented.");
204
205 }
206
207
208 protected void writeString(String string, double x, double y) throws IOException {
209 writeWarning(getClass()+": drawString(String, double, double) not implemented.");
210
211 }
212
213 public void drawString(AttributedCharacterIterator iterator, float x, float y) {
214 writeWarning(getClass()+": drawString(AttributedCharacterIterator, float, float) not implemented.");
215
216 }
217
218 public void drawGlyphVector(GlyphVector g, float x, float y) {
219 writeWarning(getClass()+": drawGlyphVector(GlyphVector, float, float) not implemented.");
220
221 }
222
223 public void drawString(TagString str, double x, double y) {
224 if (!excludeStrings) super.drawString(str, x, y);
225 }
226
227 public void drawString(String str,
228 double x, double y,
229 int horizontal, int vertical) {
230 if (!excludeStrings) super.drawString(str, x, y, horizontal, vertical);
231 }
232
233 public void drawString(TagString str,
234 double x, double y,
235 int horizontal, int vertical) {
236 if (!excludeStrings) super.drawString(str, x, y, horizontal, vertical);
237 }
238
239 public void drawString(String str,
240 double x, double y,
241 int horizontal, int vertical,
242 boolean framed, Color frameColor, double frameWidth,
243 boolean banner, Color bannerColor) {
244 if (!excludeStrings) super.drawString(str, x, y, horizontal, vertical, framed, frameColor, frameWidth, banner, bannerColor);
245 }
246
247 public void drawString(TagString str,
248 double x, double y,
249 int horizontal, int vertical,
250 boolean framed, Color frameColor, double frameWidth,
251 boolean banner, Color bannerColor) {
252 if (!excludeStrings) super.drawString(str, x, y, horizontal, vertical, framed, frameColor, frameWidth, banner, bannerColor);
253 }
254
255
256
257
258 protected void writeTransform(AffineTransform t) throws IOException {
259 writeWarning(getClass()+": writeTransform(AffineTransform) not implemented.");
260
261
262
263 }
264
265
266
267
268 protected void writeClip(Rectangle2D r2d) throws IOException {
269 writeWarning(getClass()+": writeClip(Rectangle2D) not implemented.");
270
271 }
272
273 protected void writeClip(Shape s) throws IOException {
274 writeWarning(getClass()+": writeClip(Shape) not implemented.");
275
276 }
277
278
279
280
281
282 protected void writeWidth(float width) throws IOException {
283 }
284
285 protected void writeCap(int cap) throws IOException {
286 }
287
288 protected void writeJoin(int join) throws IOException {
289 }
290
291 protected void writeMiterLimit(float limit) throws IOException {
292 }
293
294 protected void writeDash(double[] dash, double phase) throws IOException {
295 }
296
297
298 public void setPaintMode() {
299 }
300
301 public void setXORMode(Color c1) {
302 }
303
304 protected void writePaint(Color p) throws IOException {
305 }
306
307 protected void writePaint(GradientPaint p) throws IOException {
308 }
309
310 protected void writePaint(TexturePaint p) throws IOException {
311 }
312
313 protected void writePaint(Paint p) throws IOException {
314 }
315
316
317
318
319
320
321
322 public GraphicsConfiguration getDeviceConfiguration() {
323 writeWarning(getClass()+": getDeviceConfiguration() not implemented.");
324
325 return null;
326 }
327
328 public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
329 writeWarning(getClass()+": hit(Rectangle, Shape, boolean) not implemented.");
330
331 return false;
332 }
333
334 public void writeComment(String comment) throws IOException {
335 writeWarning(getClass()+": writeComment(String) not implemented.");
336
337 }
338
339 public void writeWarning(String warning) {
340 System.err.println(warning);
341 }
342
343 public String toString() {
344 return "BoundingBoxGraphics2D: "+boundingBox;
345 }
346 }