1 package net.sf.openrocket.gui.print;
3 import net.sf.openrocket.rocketcomponent.Transition;
6 import java.awt.geom.Arc2D;
7 import java.awt.geom.GeneralPath;
8 import java.awt.geom.Line2D;
9 import java.awt.geom.Path2D;
10 import java.awt.geom.Point2D;
13 * This class allows for a Transition to be printable. It does so by decorating an existing transition (which will not be
14 * modified) and rendering it within a JPanel. The JPanel is not actually visualized on a display, but instead renders
15 * it to a print device.
17 * Note: Currently nose cones are only supported by drawing the 2D projection of the profile. A more useful approach
18 * may be to draw a myriahedral projection that can be cut out and bent to form the shape.
20 public class PrintableTransition extends AbstractPrintableTransition {
25 private final static float dash1[] = {4.0f};
27 * The dashed stroke for glue tab.
29 private final static BasicStroke dashed = new BasicStroke(1.0f,
31 BasicStroke.JOIN_MITER,
35 * The layout is an outer arc, an inner arc, and two lines one either endpoints that connect the arcs.
36 * Most of the math involves transposing geometric cartesian coordinates to the Java AWT coordinate system.
43 private Path2D glueTab1;
46 * The alignment marks.
48 private Line2D tick1, tick2;
51 * The x coordinates for the two ticks drawn at theta degrees.
53 private int tick3X, tick4X;
56 * The angle, in degrees.
61 * The x,y coordinates for where the virtual circle center is located.
63 private int circleCenterX, circleCenterY;
68 * @param transition the transition to print
70 public PrintableTransition(Transition transition) {
71 super(false, transition);
75 protected void init(Transition component) {
77 double r1 = component.getAftRadius();
78 double r2 = component.getForeRadius();
80 //Regardless of orientation, we have the convention of R1 as the smaller radius. Flip if different.
83 r2 = component.getAftRadius();
85 double len = component.getLength();
87 double tmp = Math.sqrt(v * v + len * len);
88 double factor = tmp / v;
90 theta = (float) (360d * v / tmp);
92 int r1InPoints = (int) PrintUnit.METERS.toPoints(r1 * factor);
93 int r2InPoints = (int) PrintUnit.METERS.toPoints(r2 * factor);
97 int y = tabOffset + marginY;
99 Arc2D.Double outerArc = new Arc2D.Double();
100 Arc2D.Double innerArc = new Arc2D.Double();
102 //If the arcs are more than 3/4 of a circle, then assume the height (y) is the same as the radius of the bigger arc.
106 //If the arc is between 1/2 and 3/4 of a circle, then compute the actual height based upon the angle and radius
108 else if (theta >= 180) {
109 double thetaRads = Math.toRadians(theta - 180);
110 y += (int) ((Math.cos(thetaRads) * r2InPoints) * Math.tan(thetaRads));
114 circleCenterX = r2InPoints + x;
116 //Create the larger arc.
117 outerArc.setArcByCenter(circleCenterX, circleCenterY, r2InPoints, 180, theta, Arc2D.OPEN);
119 //Create the smaller arc.
120 innerArc.setArcByCenter(circleCenterX, circleCenterY, r1InPoints, 180, theta, Arc2D.OPEN);
122 //Create the line between the start of the larger arc and the start of the smaller arc.
123 Path2D.Double line = new Path2D.Double();
124 line.setWindingRule(Path2D.WIND_NON_ZERO);
126 final int width = r2InPoints - r1InPoints;
127 line.lineTo(width + x, y);
129 //Create the line between the endpoint of the larger arc and the endpoint of the smaller arc.
130 Path2D.Double closingLine = new Path2D.Double();
131 closingLine.setWindingRule(Path2D.WIND_NON_ZERO);
132 Point2D innerArcEndPoint = innerArc.getEndPoint();
133 closingLine.moveTo(innerArcEndPoint.getX(), innerArcEndPoint.getY());
134 Point2D outerArcEndPoint = outerArc.getEndPoint();
135 closingLine.lineTo(outerArcEndPoint.getX(), outerArcEndPoint.getY());
137 //Add all shapes to the polygon path.
138 gp = new Path2D.Float(GeneralPath.WIND_EVEN_ODD, 4);
139 gp.append(line, false);
140 gp.append(outerArc, false);
141 gp.append(closingLine, false);
142 gp.append(innerArc, false);
144 //Create the glue tab.
145 glueTab1 = new Path2D.Float(GeneralPath.WIND_EVEN_ODD, 4);
146 glueTab1.moveTo(x, y);
147 glueTab1.lineTo(x + tabOffset, y - tabOffset);
148 glueTab1.lineTo(width + x - tabOffset, y - tabOffset);
149 glueTab1.lineTo(width + x, y);
151 //Create tick marks for alignment, 1/4 of the width in from either edge
152 int fromEdge = width / 4;
153 final int tickLength = 8;
155 tick1 = new Line2D.Float(x + fromEdge, y, x + fromEdge, y + tickLength);
157 tick2 = new Line2D.Float(x + width - fromEdge, y, x + width - fromEdge, y + tickLength);
159 tick3X = r2InPoints - fromEdge;
160 tick4X = r1InPoints + fromEdge;
162 setSize(gp.getBounds().width, gp.getBounds().height + tabOffset);
166 * Draw alignment marks on an angle.
168 * @param g2 the graphics context
169 * @param x the center of the circle's x coordinate
170 * @param y the center of the circle's y
171 * @param line the line to draw
172 * @param theta the angle
174 private void drawAlignmentMarks(Graphics2D g2, int x, int y, Line2D.Float line, float theta) {
176 g2.rotate(Math.toRadians(-theta));
178 g2.rotate(Math.toRadians(theta));
179 g2.translate(-x, -y);
185 * @param g2 the graphics context
187 protected void draw(Graphics2D g2) {
192 drawAlignmentMarks(g2, circleCenterX,
194 new Line2D.Float(-tick3X, 0, -tick3X, -8),
196 drawAlignmentMarks(g2, circleCenterX,
198 new Line2D.Float(-tick4X, 0, -tick4X, -8),
201 g2.setStroke(dashed);