1 package net.sf.openrocket.gui.print;
3 import java.awt.BasicStroke;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
8 import java.awt.RenderingHints;
9 import java.awt.image.BufferedImage;
11 public abstract class AbstractPrintable<T> extends PrintableComponent {
15 public final static BasicStroke thinStroke = new BasicStroke(1.0f);
20 public final static BasicStroke thickStroke = new BasicStroke(4.0f);
23 * Constructor. Initialize this printable with the component to be printed.
25 * @param isDoubleBuffered a boolean, true for double-buffering
26 * @param transition the component to be printed
28 public AbstractPrintable(boolean isDoubleBuffered, T transition) {
33 * Initialize the printable.
35 * @param component the component
37 protected abstract void init(T component);
40 * Draw the component onto the graphics context.
42 * @param g2 the graphics context
44 protected abstract void draw(Graphics2D g2);
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.
50 * @return an awt image of the printable component
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();
60 g2d.setBackground(Color.white);
61 g2d.clearRect(0, 0, width, height);
63 // Graphics context no longer needed so dispose it
69 public void paintComponent(Graphics g) {
70 Graphics2D g2 = (Graphics2D) g;
71 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
72 RenderingHints.VALUE_ANTIALIAS_ON);
74 g2.setColor(Color.BLACK);
75 g2.setStroke(thinStroke);
76 g2.translate(getOffsetX(), getOffsetY());