4 package net.sf.openrocket.gui.print;
6 import net.sf.openrocket.rocketcomponent.FinSet;
7 import net.sf.openrocket.util.Coordinate;
10 import java.awt.Graphics;
11 import java.awt.Graphics2D;
12 import java.awt.Image;
13 import java.awt.geom.GeneralPath;
14 import java.awt.image.BufferedImage;
17 * This class allows for a FinSet to be printable. It does so by decorating an existing finset (which will not be
18 * modified) and rendering it within a JPanel. The JPanel is not actually visualized on a display, but instead renders
19 * it to a print device.
21 public class PrintableFinSet extends PrintableComponent {
24 * The object that represents the shape (outline) of the fin. This gets drawn onto the Swing component.
26 protected GeneralPath polygon = null;
29 * The minimum X coordinate.
33 * The minimum Y coordinate.
40 * @param fs the finset to print
42 public PrintableFinSet (FinSet fs) {
43 this(fs.getFinPointsWithTab());
47 * Construct a fin set from a set of points.
49 * @param points an array of points.
51 public PrintableFinSet (Coordinate[] points) {
56 * Initialize the fin set polygon and set the size of the component.
58 * @param points an array of points.
60 private void init (Coordinate[] points) {
62 polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, points.length);
68 for (Coordinate point : points) {
69 final long x = (long)PrintUnit.METERS.toPoints(point.x);
70 final long y = (long)PrintUnit.METERS.toPoints(point.y);
71 minX = (int) Math.min(x, minX);
72 minY = (int) Math.min(y, minY);
73 maxX = (int) Math.max(x, maxX);
74 maxY = (int) Math.max(y, maxY);
79 setSize(maxX - minX, maxY - minY);
83 * Returns a generated image of the fin set. May then be used wherever AWT images can be used, or converted to
84 * another image/picture format and used accordingly.
86 * @return an awt image of the fin set
88 public Image createImage () {
89 int width = getWidth();
90 int height = getHeight();
91 // Create a buffered image in which to draw
92 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
93 // Create a graphics contents on the buffered image
94 Graphics2D g2d = bufferedImage.createGraphics();
96 g2d.setBackground(Color.white);
97 g2d.clearRect(0, 0, width, height);
99 // Graphics context no longer needed so dispose it
101 return bufferedImage;
105 * Render the fin set onto the graphics context. This is done by creating a GeneralPath component that follows the
106 * outline of the fin set coordinates to create a polygon, which is then drawn onto the graphics context.
107 * Through-the-wall fin tabs are supported if they are present.
109 * @param g the Java2D graphics context
112 public void paintComponent(Graphics g) {
113 Graphics2D g2d = (Graphics2D) g;
118 int marginX = this.getOffsetX();
119 int marginY = this.getOffsetY();
121 // The minimum X/Y can be negative (primarily only Y due to fin tabs; rarely (never) X, but protect both anyway).
122 if (minX < marginX) {
123 x = marginX + Math.abs(minX);
125 if (minY < marginY) {
126 y = marginY + Math.abs(minY);
130 g2d.setPaint(TemplateProperties.getFillColor());
132 g2d.setPaint(TemplateProperties.getLineColor());