4 package net.sf.openrocket.gui.print;
6 import net.sf.openrocket.rocketcomponent.FinSet;
7 import net.sf.openrocket.util.Coordinate;
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;
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.
25 public class PrintableFinSet extends JPanel implements Printable {
28 * The object that represents the shape (outline) of the fin. This gets drawn onto the Swing component.
30 protected GeneralPath polygon = null;
35 private int marginX = 25;
39 private int marginY = 25;
44 * @param fs the finset to print
46 public PrintableFinSet (FinSet fs) {
47 this(fs.getFinPointsWithTab());
51 * Construct a fin set from a set of points.
53 * @param points an array of points.
55 public PrintableFinSet (Coordinate[] points) {
58 setBackground(Color.white);
62 * Initialize the fin set polygon and set the size of the component.
64 * @param points an array of points.
66 private void init (Coordinate[] points) {
68 polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, points.length);
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);
88 marginX += Math.abs(minX);
91 marginY += Math.abs(minY);
93 setSize(maxX - minX + marginX, maxY - minY + marginY);
97 * Get the X-axis margin value.
99 * @return margin, in points
101 protected double getMarginX () {
106 * Get the Y-axis margin value.
108 * @return margin, in points
110 protected double getMarginY () {
115 * From the java.awt.print.Printable interface.
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}.
126 * Note: This is not currently used in OpenRocket. It's only here for reference.
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
132 * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
135 * @throws java.awt.print.PrinterException
136 * thrown when the print job is terminated.
139 public int print (final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
140 throws PrinterException {
142 Graphics2D g2d = (Graphics2D) graphics;
143 PrintUtilities.translateToJavaOrigin(g2d, pageFormat);
144 PrintUtilities.disableDoubleBuffering(this);
146 PrintUtilities.enableDoubleBuffering(this);
147 return Printable.PAGE_EXISTS;
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.
154 * @return an awt image of the fin set
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();
164 g2d.setBackground(Color.white);
165 g2d.clearRect(0, 0, width, height);
167 // Graphics context no longer needed so dispose it
169 return bufferedImage;
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.
177 * @param g the Java2D graphics context
180 public void paintComponent (Graphics g) {
181 super.paintComponent(g);
182 Graphics2D g2d = (Graphics2D) g;
184 g2d.translate(marginX, marginY);
185 g2d.setPaint(TemplateProperties.getFillColor());
187 g2d.setPaint(TemplateProperties.getLineColor());