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