View Javadoc

1   // Copyright 2007, FreeHEP.
2   package org.freehep.graphicsio.emf.gdi;
3   
4   import org.freehep.graphicsio.emf.EMFTag;
5   import org.freehep.graphicsio.emf.EMFRenderer;
6   
7   import java.awt.Point;
8   import java.awt.Rectangle;
9   import java.awt.geom.GeneralPath;
10  
11  /**
12   * abstract parent for PolyPolygon drawing
13   *
14   * @author Steffen Greiffenberg
15   * @version $Id$
16   */
17  public abstract class AbstractPolyPolygon extends EMFTag {
18  
19      private Rectangle bounds;
20  
21      private int[] numberOfPoints;
22  
23      private Point[][] points;
24  
25      /**
26       * Constructs a EMFTag.
27       *
28       * @param id      id of the element
29       * @param version emf version in which this element was first supported
30       * @param bounds bounds of figure
31       * @param numberOfPoints number of points
32       * @param points points
33       */
34      protected AbstractPolyPolygon(
35          int id, int version,
36          Rectangle bounds,
37          int[] numberOfPoints,
38          Point[][] points) {
39  
40          super(id, version);
41          this.bounds = bounds;
42          this.numberOfPoints = numberOfPoints;
43          this.points = points;
44      }
45  
46      public String toString() {
47          return super.toString() +
48              "\n  bounds: " + bounds +
49              "\n  #polys: " + numberOfPoints.length;
50      }
51  
52      protected Rectangle getBounds() {
53          return bounds;
54      }
55  
56      protected int[] getNumberOfPoints() {
57          return numberOfPoints;
58      }
59  
60      protected Point[][] getPoints() {
61          return points;
62      }
63  
64      /**
65       * displays the tag using the renderer. The default behavior
66       * is to close and fill the polgygons for rendering.
67       *
68       * @param renderer EMFRenderer storing the drawing session data
69       */
70      public void render(EMFRenderer renderer) {
71          render(renderer,  true);
72      }
73  
74      /**
75       * displays the tag using the renderer
76       *
77       * @param renderer EMFRenderer storing the drawing session data
78       * @param closePath if true the path is closed and filled
79       */
80      protected void render(EMFRenderer renderer, boolean closePath) {
81          // create a GeneralPath containing GeneralPathes
82          GeneralPath path = new GeneralPath(
83              renderer.getWindingRule());
84  
85          // iterate the polgons
86          Point p;
87          for (int polygon = 0; polygon < numberOfPoints.length; polygon++) {
88  
89              // create a new member of path
90              GeneralPath gp = new GeneralPath(
91                  renderer.getWindingRule());
92              for (int point = 0; point < numberOfPoints[polygon]; point ++) {
93                  // add a point to gp
94                  p = points[polygon][point];
95                  if (point > 0) {
96                      gp.lineTo((float) p.getX(),  (float)p.getY());
97                  } else {
98                      gp.moveTo((float) p.getX(),  (float)p.getY());
99                  }
100             }
101 
102             // close the member, add it to path
103             if (closePath) {
104                 gp.closePath();
105             }
106 
107             path.append(gp, false);
108         }
109 
110         // draw the complete path
111         if (closePath) {
112             renderer.fillAndDrawOrAppend(path);
113         } else {
114             renderer.drawOrAppend(path);
115         }
116     }
117 }