]> git.gag.com Git - debian/openrocket/blob - src/net/sf/openrocket/gui/print/PrintableFinSet.java
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@136 180e2...
[debian/openrocket] / src / net / sf / openrocket / gui / print / PrintableFinSet.java
1 /*
2  * PrintableFinSet.java
3  */
4 package net.sf.openrocket.gui.print;
5
6 import net.sf.openrocket.rocketcomponent.FinSet;
7 import net.sf.openrocket.util.Coordinate;
8
9 import javax.swing.JPanel;
10 import java.awt.Color;
11 import java.awt.Graphics;
12 import java.awt.Graphics2D;
13 import java.awt.Image;
14 import java.awt.geom.GeneralPath;
15 import java.awt.image.BufferedImage;
16 import java.awt.print.PageFormat;
17 import java.awt.print.Printable;
18 import java.awt.print.PrinterException;
19
20 /**
21  * This class allows for a FinSet to be printable.  It does so by decorating an existing finset (which will not be
22  * modified) and rendering it within a JPanel.  The JPanel is not actually visualized on a display, but instead renders
23  * it to a print device.
24  */
25 public class PrintableFinSet extends JPanel implements Printable {
26
27     /**
28      * The object that represents the shape (outline) of the fin.  This gets drawn onto the Swing component.
29      */
30     protected GeneralPath polygon = null;
31
32     /**
33      * The X margin.
34      */
35     private int marginX = 25;
36     /**
37      * The Y margin.
38      */
39     private int marginY = 25;
40
41     /**
42      * Constructor.
43      *
44      * @param fs the finset to print
45      */
46     public PrintableFinSet (FinSet fs) {
47         this(fs.getFinPointsWithTab());
48     }
49
50     /**
51      * Construct a fin set from a set of points.
52      *
53      * @param points an array of points.
54      */
55     public PrintableFinSet (Coordinate[] points) {
56         super(false);
57         init(points);
58         setBackground(Color.white);
59     }
60
61     /**
62      * Initialize the fin set polygon and set the size of the component.
63      *
64      * @param points an array of points.
65      */
66     private void init (Coordinate[] points) {
67
68         polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, points.length);
69         polygon.moveTo(0, 0);
70
71         int minX = 0;
72         int minY = 0;
73         int maxX = 0;
74         int maxY = 0;
75
76         for (Coordinate point : points) {
77             final long x = PrintUnit.METERS.toPoints(point.x);
78             final long y = PrintUnit.METERS.toPoints(point.y);
79             minX = (int) Math.min(x, minX);
80             minY = (int) Math.min(y, minY);
81             maxX = (int) Math.max(x, maxX);
82             maxY = (int) Math.max(y, maxY);
83             polygon.lineTo(x, y);
84         }
85         polygon.closePath();
86
87         if (minX < 0) {
88             marginX += Math.abs(minX);
89         }
90         if (minY < 0) {
91             marginY += Math.abs(minY);
92         }
93         setSize(maxX - minX + marginX, maxY - minY + marginY);
94     }
95
96     /**
97      * Get the X-axis margin value.
98      *
99      * @return margin, in points
100      */
101     protected double getMarginX () {
102         return marginX;
103     }
104
105     /**
106      * Get the Y-axis margin value.
107      *
108      * @return margin, in points
109      */
110     protected double getMarginY () {
111         return marginY;
112     }
113
114     /**
115      * From the java.awt.print.Printable interface.
116      * <p/>
117      * Prints the page at the specified index into the specified {@link java.awt.Graphics} context in the specified
118      * format. A <code>PrinterJob</code> calls the <code>Printable</code> interface to request that a page be rendered
119      * into the context specified by <code>graphics</code>.  The format of the page to be drawn is specified by
120      * <code>pageFormat</code>.  The zero based index of the requested page is specified by <code>pageIndex</code>. If
121      * the requested page does not exist then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. The
122      * <code>Graphics</code> class or subclass implements the {@link java.awt.print.PrinterGraphics} interface to
123      * provide additional information.  If the <code>Printable</code> object aborts the print job then it throws a
124      * {@link java.awt.print.PrinterException}.
125      * <p/>
126      * Note: This is not currently used in OpenRocket.  It's only here for reference.
127      *
128      * @param graphics   the context into which the page is drawn
129      * @param pageFormat the size and orientation of the page being drawn
130      * @param pageIndex  the zero based index of the page to be drawn
131      *
132      * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
133      *         non-existent page.
134      *
135      * @throws java.awt.print.PrinterException
136      *          thrown when the print job is terminated.
137      */
138     @Override
139     public int print (final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
140             throws PrinterException {
141
142         Graphics2D g2d = (Graphics2D) graphics;
143         PrintUtilities.translateToJavaOrigin(g2d, pageFormat);
144         PrintUtilities.disableDoubleBuffering(this);
145         paint(g2d);
146         PrintUtilities.enableDoubleBuffering(this);
147         return Printable.PAGE_EXISTS;
148     }
149
150     /**
151      * Returns a generated image of the fin set.  May then be used wherever AWT images can be used, or converted to
152      * another image/picture format and used accordingly.
153      *
154      * @return an awt image of the fin set
155      */
156     public Image createImage () {
157         int width = getWidth() + marginX;
158         int height = getHeight() + marginY;
159         // Create a buffered image in which to draw 
160         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
161         // Create a graphics contents on the buffered image 
162         Graphics2D g2d = bufferedImage.createGraphics();
163         // Draw graphics 
164         g2d.setBackground(Color.white);
165         g2d.clearRect(0, 0, width, height);
166         paintComponent(g2d);
167         // Graphics context no longer needed so dispose it 
168         g2d.dispose();
169         return bufferedImage;
170     }
171
172     /**
173      * Render the fin set onto the graphics context.  This is done by creating a GeneralPath component that follows the
174      * outline of the fin set coordinates to create a polygon, which is then drawn onto the graphics context.
175      * Through-the-wall fin tabs are supported if they are present.
176      *
177      * @param g the Java2D graphics context
178      */
179     @Override
180     public void paintComponent (Graphics g) {
181         super.paintComponent(g);
182         Graphics2D g2d = (Graphics2D) g;
183
184         g2d.translate(marginX, marginY);
185         g2d.setPaint(TemplateProperties.getFillColor());
186         g2d.fill(polygon);
187         g2d.setPaint(TemplateProperties.getLineColor());
188         g2d.draw(polygon);
189     }
190
191 }