lose embedded source jars from upstream branch
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / AbstractPrintableTransition.java
1 package net.sf.openrocket.gui.print;
2
3 import net.sf.openrocket.rocketcomponent.Transition;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.image.BufferedImage;
8
9 public abstract class AbstractPrintableTransition extends JPanel {
10     /**
11      * The stroke of the transition arc.
12      */
13     private final static BasicStroke thinStroke = new BasicStroke(1.0f);
14
15     /**
16      * The X margin.
17      */
18     protected int marginX = (int) PrintUnit.INCHES.toPoints(0.25f);
19
20     /**
21      * The Y margin.
22      */
23     protected int marginY = (int) PrintUnit.INCHES.toPoints(0.25f);
24
25     /**
26      * Constructor. Initialize this printable with the component to be printed.
27      *
28      * @param isDoubleBuffered  a boolean, true for double-buffering
29      * @param transition  the component to be printed
30      */
31     public AbstractPrintableTransition(boolean isDoubleBuffered, Transition transition) {
32         super(isDoubleBuffered);
33         setBackground(Color.white);
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(Transition 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 fin set
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
84         draw(g2);
85     }
86 }