View Javadoc

1   // Copyright 2001 freehep
2   package org.freehep.graphicsio;
3   
4   import java.awt.Shape;
5   import java.awt.geom.AffineTransform;
6   import java.awt.geom.PathIterator;
7   import java.io.IOException;
8   
9   /**
10   * Implements some of the PathConstructor functionality
11   * 
12   * @author Mark Donszelmann
13   * @version $Id: AbstractPathConstructor.java 8584 2006-08-10 23:06:37Z duns $
14   */
15  public abstract class AbstractPathConstructor implements PathConstructor {
16  
17      protected double currentX, currentY;
18  
19      protected AbstractPathConstructor() {
20          currentX = 0;
21          currentY = 0;
22      }
23  
24      public void flush() throws IOException {
25          currentX = 0;
26          currentY = 0;
27      }
28  
29      public boolean addPath(Shape s) throws IOException {
30          return addPath(s, null);
31      }
32  
33      public boolean addPath(Shape s, AffineTransform transform)
34              throws IOException {
35          return addPath(this, s, transform);
36      }
37  
38      public static boolean addPath(PathConstructor out, Shape s,
39              AffineTransform transform) throws IOException {
40          PathIterator path = s.getPathIterator(transform);
41          double[] coords = new double[6];
42          double pathStartX = 0.;
43          double pathStartY = 0.;
44          while (!path.isDone()) {
45              int segType = path.currentSegment(coords);
46  
47              switch (segType) {
48              case PathIterator.SEG_MOVETO:
49                  out.move(coords[0], coords[1]);
50                  pathStartX = coords[0];
51                  pathStartY = coords[1];
52                  break;
53              case PathIterator.SEG_LINETO:
54                  out.line(coords[0], coords[1]);
55                 break;
56              case PathIterator.SEG_QUADTO:
57                  out.quad(coords[0], coords[1], coords[2], coords[3]);
58                  break;
59              case PathIterator.SEG_CUBICTO:
60                  out.cubic(coords[0], coords[1], coords[2], coords[3],
61                          coords[4], coords[5]);
62                  break;
63              case PathIterator.SEG_CLOSE:
64                  out.closePath(pathStartX, pathStartY);
65                  break;
66              }
67              // Move to the next segment.
68              path.next();
69          }
70          out.flush();
71          return (path.getWindingRule() == PathIterator.WIND_EVEN_ODD);
72      }
73  
74      public static boolean isEvenOdd(Shape s) {
75          return s.getPathIterator(null).getWindingRule() == PathIterator.WIND_EVEN_ODD;
76      }
77  }