739a7d9194df327045f72837f92bdc16e77597ac
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / AbstractPrintable.java
1 package net.sf.openrocket.gui.print;
2
3 import java.awt.*;
4 import java.awt.image.BufferedImage;
5
6 public abstract class AbstractPrintable<T> extends PrintableComponent {
7     /**
8      * A thin stroke.
9      */
10     public final static BasicStroke thinStroke = new BasicStroke(1.0f);
11
12     /**
13      * A thick stroke.
14      */
15     public final static BasicStroke thickStroke = new BasicStroke(4.0f);
16
17     /**
18      * The X margin.
19      */
20     protected int marginX = (int) PrintUnit.INCHES.toPoints(0.25f);
21
22     /**
23      * The Y margin.
24      */
25     protected int marginY = (int) PrintUnit.INCHES.toPoints(0.25f);
26
27     /**
28      * Constructor. Initialize this printable with the component to be printed.
29      *
30      * @param isDoubleBuffered  a boolean, true for double-buffering
31      * @param transition  the component to be printed
32      */
33     public AbstractPrintable(boolean isDoubleBuffered, T transition) {
34         init(transition);
35     }
36
37     /**
38      * Compute the basic values of each arc of the transition/shroud.  This is adapted from
39      * <a href="http://www.rocketshoppe.com/info/Transitions.pdf">The Properties of
40      * Model Rocket Body Tube Transitions, by J.R. Brohm</a>
41      *
42      * @param component the transition component
43      */
44     protected abstract void init(T component);
45
46     /**
47      * Draw the component onto the graphics context.
48      *
49      * @param g2 the graphics context
50      */
51     protected abstract void draw(Graphics2D g2);
52
53     /**
54      * Returns a generated image of the transition.  May then be used wherever AWT images can be used, or converted to
55      * another image/picture format and used accordingly.
56      *
57      * @return an awt image of the transition
58      */
59     public Image createImage() {
60         int width = getWidth() + marginX;
61         int height = getHeight() + marginY;
62         // Create a buffered image in which to draw
63         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
64         // Create a graphics contents on the buffered image
65         Graphics2D g2d = bufferedImage.createGraphics();
66         // Draw graphics
67         g2d.setBackground(Color.white);
68         g2d.clearRect(0, 0, width, height);
69         paintComponent(g2d);
70         // Graphics context no longer needed so dispose it
71         g2d.dispose();
72         return bufferedImage;
73     }
74
75     @Override
76     public void paintComponent(Graphics g) {
77         Graphics2D g2 = (Graphics2D) g;
78         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
79                 RenderingHints.VALUE_ANTIALIAS_ON);
80
81         g2.setColor(Color.BLACK);
82         g2.setStroke(thinStroke);
83                 g2.translate(getOffsetX(), getOffsetY());
84
85         draw(g2);
86     }
87 }