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  import org.freehep.graphicsio.PathConstructor;
16  
17  /***
18   * Forwards all drawing to a given pathconstructor, as objects are drawn into 
19   * this Graphics2D context.
20   *
21   * FIXME WIRED-257 should maybe move to FreeHEP...
22   *
23   * @author Mark Donszelmann
24   * @version $Id: PathGraphics2D.java 653 2005-03-01 01:12:16Z duns $
25   */
26  public class PathGraphics2D
27      extends AbstractVectorGraphicsIO {
28      /*================================================================================
29       * Table of Contents:
30       * ------------------
31       * 1. Constructors & Factory Methods
32       * 2. Document Settings
33       * 3. Header, Trailer, Multipage & Comments
34       *    3.1 Header & Trailer
35       *    3.2 MultipageDocument methods
36       * 4. Create & Dispose
37       * 5. Drawing Methods
38       *    5.1. shapes (draw/fill)
39       *         5.1.1. lines, rectangles, round rectangles
40       *         5.1.2. polylines, polygons
41       *         5.1.3. ovals, arcs
42       *         5.1.4. shapes
43       *    5.2. Images
44       *    5.3. Strings
45       * 6. Transformations
46       * 7. Clipping
47       * 8. Graphics State / Settings
48       *    8.1. stroke/linewidth
49       *    8.2. paint/color
50       *    8.3. font
51       *    8.4. rendering hints
52       * 9. Auxiliary
53       * 10. Private/Utility Methos
54       *================================================================================*/
55  
56      private PathConstructor pathConstructor;
57  //    private AffineTransform transform = new AffineTransform();
58  
59      /*================================================================================
60       * 1. Constructors & Factory Methods
61       *================================================================================*/
62      /***
63       * Create a path graphics context with dimension 800x600.
64       */
65      public PathGraphics2D(PathConstructor pathConstructor) {
66          super(new Dimension(800,600), false);
67          // Create a graphics context with given imageBounds.
68          // This constructor is used by the user to create the initial graphics context.
69          // doRestoreOnDispose is used to call writeGraphicsRestore(),
70          // when the graphics context is being disposed off.
71          
72          this.pathConstructor = pathConstructor;
73      }
74  
75      /***
76       * Create a sub context.
77       */
78      protected PathGraphics2D(PathGraphics2D graphics, boolean doRestoreOnDispose) {
79          super(graphics, doRestoreOnDispose);
80          // Create a graphics context from a given graphics context.
81          // This constructor is used by the system to clone a given graphics context.
82          // doRestoreOnDispose is used to call writeGraphicsRestore(),
83          // when the graphics context is being disposed off.
84          pathConstructor = graphics.pathConstructor;
85      }
86  
87      /*================================================================================
88       | 2. Document Settings
89       *================================================================================*/
90  
91      /*================================================================================
92       | 3. Header, Trailer, Multipage & Comments
93       *================================================================================*/
94      /* 3.1 Header & Trailer */
95      public void writeHeader() throws IOException {
96          writeWarning(getClass()+": writeHeader() not implemented.");
97          // Write out the header to the output stream.
98      }
99  
100     public void writeBackground() throws IOException {
101         writeWarning(getClass()+": writeBackground() not implemented.");
102         // Write out the background to the output stream.
103     }
104 
105     public void writeTrailer() throws IOException {
106         writeWarning(getClass()+": writeTrailer() not implemented.");
107         // Write out the trailer to the output stream.
108     }
109 
110     public void closeStream() throws IOException {
111         writeWarning(getClass()+": closeStream() not implemented.");
112         // Close the output stream.
113     }
114 
115     /* 3.2 MultipageDocument methods */
116 
117     /*================================================================================
118      * 4. Create & Dispose
119      *================================================================================*/
120 
121     public Graphics create() {
122         // Create a new graphics context from the current one.
123         try {
124             // Save the current context for restore later.
125             writeGraphicsSave();
126         } catch (IOException e) {
127         }
128         // The correct graphics context should be created.
129         return new PathGraphics2D(this, false);
130     }
131 
132     public Graphics create(double x, double y, double width, double height) {
133         // Create a new graphics context from the current one.
134         try {
135             // Save the current context for restore later.
136             writeGraphicsSave();
137         } catch (IOException e) {
138         }
139         // The correct graphics context should be created.
140         VectorGraphics graphics = new PathGraphics2D(this, false);
141         graphics.clipRect(x, y, width, height);
142         return graphics;
143     }
144 
145     protected void writeGraphicsSave() throws IOException {
146         writeWarning(getClass()+": writeGraphicsSave() not implemented.");
147         // Write a graphics context save.
148         // If the output format does not support this, keep a stack yourself.
149     }
150 
151     protected void writeGraphicsRestore() throws IOException {
152         writeWarning(getClass()+": writeGraphicsRestore() not implemented.");
153         // Write a graphics context restore.
154         // If the output format does not support this, keep a stack yourself.
155     }
156 
157     /*================================================================================
158      | 5. Drawing Methods
159      *================================================================================*/
160     /* 5.1.4. shapes */
161 
162     public void draw(Shape shape) {
163         try {
164             pathConstructor.addPath(shape);
165         } catch (IOException e) {
166             // never happens
167         }
168     }
169 
170     public void fill(Shape shape) {
171         draw(shape);
172     }
173 
174     public void fillAndDraw(Shape shape, Color fillColor) {
175         draw(shape);
176     }
177 
178     protected void drawSymbol(VectorGraphics g, double x, double y, double size, int symbol) {
179         try {
180             pathConstructor.move(x, y);
181             pathConstructor.line(x, y);
182         } catch (IOException e) {
183             writeWarning(getClass()+": drawSymbol(...) threw "+e.getMessage());
184         }
185     }
186 
187     protected void fillSymbol(VectorGraphics g, double x, double y, double size, int symbol) {
188         drawSymbol(g, x, y, size, symbol);
189     }
190 
191     /* 5.2. Images */
192     public void copyArea(int x, int y, int width, int height, int dx, int dy) {
193         writeWarning(getClass()+": copyArea(int, int, int, int, int, int) not implemented.");
194         // Mostly unimplemented.
195     }
196 
197     protected void writeImage(RenderedImage image, AffineTransform xform, Color bkg) throws IOException {
198         writeWarning(getClass()+": writeImage(RenderedImage, AffineTransform, Color) not implemented.");
199         // Write out the image.
200     }
201 
202     /* 5.3. Strings */
203     protected void writeString(String string, double x, double y) throws IOException {
204         // Write out the string.
205     }
206 
207     public void drawString(AttributedCharacterIterator iterator, float x, float y) {
208         // Write out the string.
209     }
210 
211     public void drawGlyphVector(GlyphVector g, float x, float y) {
212         // Write out the string.
213     }
214 
215     /*================================================================================
216      | 6. Transformations
217      *================================================================================*/
218     protected void writeTransform(AffineTransform t) throws IOException {
219         writeWarning(getClass()+": writeTransform(AffineTransform) not implemented.");
220         // Write out the transform to be applied to the internal transform of the output
221         // format.
222         // You can also use the currentTransform.
223     }
224 
225     /*================================================================================
226      | 7. Clipping
227      *================================================================================*/
228     protected void writeClip(Rectangle2D r2d) throws IOException {
229         writeWarning(getClass()+": writeClip(Rectangle2D) not implemented.");
230         // Write out the clip rectangle.
231     }
232 
233     protected void writeClip(Shape s) throws IOException {
234         writeWarning(getClass()+": writeClip(Shape) not implemented.");
235         // Write out the clip shape.
236     }
237 
238     /*================================================================================
239      | 8. Graphics State
240      *================================================================================*/
241     /* 8.1. stroke/linewidth */
242     protected void writeWidth(float width) throws IOException {
243     }
244 
245     protected void writeCap(int cap) throws IOException {
246     }
247 
248     protected void writeJoin(int join) throws IOException {
249     }
250 
251     protected void writeMiterLimit(float limit) throws IOException {
252     }
253 
254     protected void writeDash(double[] dash, double phase) throws IOException {
255     }
256 
257     /* 8.2. paint/color */
258     public void setPaintMode() {
259     }
260 
261     public void setXORMode(Color c1) {
262     }
263 
264     protected void writePaint(Color p) throws IOException {
265     }
266 
267     protected void writePaint(GradientPaint p) throws IOException {
268     }
269 
270     protected void writePaint(TexturePaint p) throws IOException {
271     }
272 
273     protected void writePaint(Paint p) throws IOException {
274     }
275 
276     /* 8.3. font */
277     /* 8.4. rendering hints */
278 
279     /*================================================================================
280      | 9. Auxiliary
281      *================================================================================*/
282     public GraphicsConfiguration getDeviceConfiguration() {
283         writeWarning(getClass()+": getDeviceConfiguration() not implemented.");
284         // Mostly unimplemented
285         return null;
286     }
287 
288     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
289         writeWarning(getClass()+": hit(Rectangle, Shape, boolean) not implemented.");
290         // Mostly unimplemented
291         return false;
292     }
293 
294     public void writeComment(String comment) throws IOException {
295         writeWarning(getClass()+": writeComment(String) not implemented.");
296         // Write out the comment.
297     }
298 
299     public void writeWarning(String warning) {
300         System.err.println(warning);
301     }
302 
303     public String toString() {
304         return "PathGraphics2D: "+pathConstructor;
305     }
306 }