08d3f12c3058e419a7aa945f87dd7a8eb1e6d0c8
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / PrintableCenteringRing.java
1 package net.sf.openrocket.gui.print;
2
3 import net.sf.openrocket.rocketcomponent.CenteringRing;
4
5 import java.awt.Color;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8 import java.awt.Shape;
9 import java.awt.geom.Ellipse2D;
10
11 /**
12  * This class creates a renderable centering ring.  It depends only on AWT/Swing and can be called from other
13  * actors (like iText handlers) to render the centering ring on different graphics contexts.
14  */
15 public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
16     /**
17      * If the component to be drawn is a centering ring, save a reference to it.
18      */
19     private CenteringRing target;
20
21     /**
22      * The X margin.
23      */
24     protected int marginX = (int) PrintUnit.INCHES.toPoints(0.25f);
25
26     /**
27      * The Y margin.
28      */
29     protected int marginY = (int) PrintUnit.INCHES.toPoints(0.25f);
30
31     /**
32      * The line length of the cross hairs.
33      */
34     private final int lineLength = 10;
35
36     /**
37      * Construct a printable nose cone.
38      *
39      * @param theRing the component to print
40      */
41     public PrintableCenteringRing(CenteringRing theRing) {
42         super(false, theRing);
43     }
44
45     /**
46      * @param component the centering ring component
47      */
48     @Override
49     protected void init(final CenteringRing component) {
50
51         target = component;
52
53         double radius = target.getOuterRadius();
54         setSize((int) PrintUnit.METERS.toPoints(2 * radius) + marginX,
55                 (int) PrintUnit.METERS.toPoints(2 * radius) + marginY);
56     }
57
58     /**
59      * Draw a centering ring.
60      *
61      * @param g2 the graphics context
62      */
63     @Override
64     protected void draw(Graphics2D g2) {
65         double radius = PrintUnit.METERS.toPoints(target.getOuterRadius());
66
67         Color original = g2.getBackground();
68         double x = marginX;
69         double y = marginY;
70         Shape outerCircle = new Ellipse2D.Double(x, y, radius * 2, radius * 2);
71         g2.setColor(Color.lightGray);
72         g2.fill(outerCircle);
73         g2.setColor(Color.black);
74         g2.draw(outerCircle);
75         x += radius;
76         y += radius;
77
78         double innerRadius = PrintUnit.METERS.toPoints(target.getInnerRadius());
79         Shape innerCircle = new Ellipse2D.Double(x - innerRadius, y - innerRadius, innerRadius * 2, innerRadius * 2);
80         g2.setColor(original);
81         g2.fill(innerCircle);
82         g2.setColor(Color.black);
83         g2.draw(innerCircle);
84
85         drawCross(g2, (int) x, (int) y, lineLength, lineLength);
86     }
87
88     /**
89      * Draw the center cross-hair.
90      *
91      * @param g  the graphics context
92      * @param x  the x coordinate of the center point
93      * @param y  the y coordinate of the center point
94      * @param width the width in pixels of the horizontal hair
95      * @param height the width in pixels of the vertical hair
96      */
97     private void drawCross(Graphics g, int x, int y, int width, int height) {
98         g.setColor(Color.black);
99         ((Graphics2D) g).setStroke(thinStroke);
100         g.drawLine(x - width / 2, y, x + width / 2, y);
101         g.drawLine(x, y - height / 2, x, y + height / 2);
102     }
103
104 }