View Javadoc

1   // Copyright 2004-2005, FreeHEP
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       * Table of Contents:
29       * ------------------
30       * 1. Constructors & Factory Methods
31       * 2. Document Settings
32       * 3. Header, Trailer, Multipage & Comments
33       *    3.1 Header & Trailer
34       *    3.2 MultipageDocument methods
35       * 4. Create & Dispose
36       * 5. Drawing Methods
37       *    5.1. shapes (draw/fill)
38       *         5.1.1. lines, rectangles, round rectangles
39       *         5.1.2. polylines, polygons
40       *         5.1.3. ovals, arcs
41       *         5.1.4. shapes
42       *    5.2. Images
43       *    5.3. Strings
44       * 6. Transformations
45       * 7. Clipping
46       * 8. Graphics State / Settings
47       *    8.1. stroke/linewidth
48       *    8.2. paint/color
49       *    8.3. font
50       *    8.4. rendering hints
51       * 9. Auxiliary
52       * 10. Private/Utility Methos
53       *================================================================================*/
54  
55      private Rectangle2D boundingBox = null;
56      private boolean excludeStrings = false;
57  //    private AffineTransform transform = new AffineTransform();
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          // ignore invalid shapes
70          if ((bounds.getWidth() <= 0) && (bounds.getHeight() <=0)) return;
71          
72          boundingBox = (boundingBox == null) ? bounds : bounds.createUnion(boundingBox);
73      }
74  
75      /*================================================================================
76       * 1. Constructors & Factory Methods
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          // Create a graphics context with given imageBounds.
89          // This constructor is used by the user to create the initial graphics context.
90          // doRestoreOnDispose is used to call writeGraphicsRestore(),
91          // when the graphics context is being disposed off.
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         // Create a graphics context from a given graphics context.
101         // This constructor is used by the system to clone a given graphics context.
102         // doRestoreOnDispose is used to call writeGraphicsRestore(),
103         // when the graphics context is being disposed off.
104         boundingBox = graphics.boundingBox;
105         excludeStrings = graphics.excludeStrings;
106 //        transform = graphics.transform;
107     }
108 
109     /*================================================================================
110      | 2. Document Settings
111      *================================================================================*/
112 
113     /*================================================================================
114      | 3. Header, Trailer, Multipage & Comments
115      *================================================================================*/
116     /* 3.1 Header & Trailer */
117     public void writeHeader() throws IOException {
118         writeWarning(getClass()+": writeHeader() not implemented.");
119         // Write out the header to the output stream.
120     }
121 
122     public void writeBackground() throws IOException {
123         writeWarning(getClass()+": writeBackground() not implemented.");
124         // Write out the background to the output stream.
125     }
126 
127     public void writeTrailer() throws IOException {
128         writeWarning(getClass()+": writeTrailer() not implemented.");
129         // Write out the trailer to the output stream.
130     }
131 
132     public void closeStream() throws IOException {
133         writeWarning(getClass()+": closeStream() not implemented.");
134         // Close the output stream.
135     }
136 
137     /* 3.2 MultipageDocument methods */
138 
139     /*================================================================================
140      * 4. Create & Dispose
141      *================================================================================*/
142 
143     public Graphics create() {
144         // Create a new graphics context from the current one.
145         try {
146             // Save the current context for restore later.
147             writeGraphicsSave();
148         } catch (IOException e) {
149         }
150         // The correct graphics context should be created.
151         return new BoundingBoxGraphics2D(this, false);
152     }
153 
154     public Graphics create(double x, double y, double width, double height) {
155         // Create a new graphics context from the current one.
156         try {
157             // Save the current context for restore later.
158             writeGraphicsSave();
159         } catch (IOException e) {
160         }
161         // The correct graphics context should be created.
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         // Write a graphics context save.
170         // If the output format does not support this, keep a stack yourself.
171     }
172 
173     protected void writeGraphicsRestore() throws IOException {
174         writeWarning(getClass()+": writeGraphicsRestore() not implemented.");
175         // Write a graphics context restore.
176         // If the output format does not support this, keep a stack yourself.
177     }
178 
179     /*================================================================================
180      | 5. Drawing Methods
181      *================================================================================*/
182     /* 5.1.4. shapes */
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     /* 5.2. Images */
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         // Mostly unimplemented.
200     }
201 
202     protected void writeImage(RenderedImage image, AffineTransform xform, Color bkg) throws IOException {
203         writeWarning(getClass()+": writeImage(RenderedImage, AffineTransform, Color) not implemented.");
204         // Write out the image.
205     }
206 
207     /* 5.3. Strings */
208     protected void writeString(String string, double x, double y) throws IOException {
209         writeWarning(getClass()+": drawString(String, double, double) not implemented.");
210         // Write out the string.
211     }
212 
213     public void drawString(AttributedCharacterIterator iterator, float x, float y) {
214         writeWarning(getClass()+": drawString(AttributedCharacterIterator, float, float) not implemented.");
215         // Write out the string.
216     }
217 
218     public void drawGlyphVector(GlyphVector g, float x, float y) {
219         writeWarning(getClass()+": drawGlyphVector(GlyphVector, float, float) not implemented.");
220         // Write out the string.
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      | 6. Transformations
257      *================================================================================*/
258     protected void writeTransform(AffineTransform t) throws IOException {
259         writeWarning(getClass()+": writeTransform(AffineTransform) not implemented.");
260         // Write out the transform to be applied to the internal transform of the output
261         // format.
262         // You can also use the currentTransform.
263     }
264 
265     /*================================================================================
266      | 7. Clipping
267      *================================================================================*/
268     protected void writeClip(Rectangle2D r2d) throws IOException {
269         writeWarning(getClass()+": writeClip(Rectangle2D) not implemented.");
270         // Write out the clip rectangle.
271     }
272 
273     protected void writeClip(Shape s) throws IOException {
274         writeWarning(getClass()+": writeClip(Shape) not implemented.");
275         // Write out the clip shape.
276     }
277 
278     /*================================================================================
279      | 8. Graphics State
280      *================================================================================*/
281     /* 8.1. stroke/linewidth */
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     /* 8.2. paint/color */
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     /* 8.3. font */
317     /* 8.4. rendering hints */
318 
319     /*================================================================================
320      | 9. Auxiliary
321      *================================================================================*/
322     public GraphicsConfiguration getDeviceConfiguration() {
323         writeWarning(getClass()+": getDeviceConfiguration() not implemented.");
324         // Mostly unimplemented
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         // Mostly unimplemented
331         return false;
332     }
333 
334     public void writeComment(String comment) throws IOException {
335         writeWarning(getClass()+": writeComment(String) not implemented.");
336         // Write out the comment.
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 }