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   import org.freehep.graphicsio.emf.EMFConstants;
7   import org.freehep.graphicsio.emf.EMFOutputStream;
8   
9   import java.awt.geom.Arc2D;
10  import java.awt.Rectangle;
11  import java.awt.Point;
12  import java.awt.Shape;
13  import java.io.IOException;
14  
15  /**
16   * @author Steffen Greiffenberg
17   * @version $Id$
18   */
19  public abstract class AbstractArc extends EMFTag {
20  
21      private Rectangle bounds;
22  
23      private Point start, end;
24  
25      protected AbstractArc(int id, int version, Rectangle bounds, Point start, Point end) {
26          super(id, version);
27          this.bounds = bounds;
28          this.start = start;
29          this.end = end;
30      }
31  
32      public void write(int tagID, EMFOutputStream emf) throws IOException {
33          emf.writeRECTL(bounds);
34          emf.writePOINTL(start);
35          emf.writePOINTL(end);
36      }
37  
38      public String toString() {
39          return super.toString() +
40              "\n  bounds: " + bounds +
41              "\n  start: " + start +
42              "\n  end: " + end;
43      }
44  
45      /**
46       * creates a shape based on bounds, start and end
47       *
48       * @param renderer EMFRenderer storing the drawing session data
49       * @param arcType type of arc, e.g. {@link Arc2D#OPEN}
50       * @return shape to render 
51       */
52      protected Shape getShape(EMFRenderer renderer, int arcType) {
53          // normalize start and end point to a circle
54          double nx0 = start.getX() / bounds.getWidth();
55  
56          // double ny0 = arc.getStart().y / arc.getBounds().height;
57          double nx1 = end.getX() / bounds.getWidth();
58  
59          // double ny1 = arc.getEnd().y / arc.getBounds().height;
60          // calculate angle of start point
61          double alpha0, alpha1;
62          if (renderer.getArcDirection() == EMFConstants.AD_CLOCKWISE) {
63              alpha0 = Math.acos(nx0);
64              alpha1 = Math.acos(nx1);
65          } else {
66              alpha0 = Math.acos(nx1);
67              alpha1 = Math.acos(nx0);
68          }
69  
70          return new Arc2D.Double(
71              start.getX(),
72              start.getY(),
73              bounds.getWidth(),
74              bounds.getHeight(),
75              alpha0,
76              alpha1 - alpha0,
77              arcType);
78      }
79  }