10e5c803e37f3554fe8f4dc28bb3d3ff63621b7f
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / PrintableComponent.java
1 /*
2  * PrintableComponent.java
3  */
4 package net.sf.openrocket.gui.print;
5
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8 import java.awt.print.PageFormat;
9 import java.awt.print.Printable;
10 import java.awt.print.PrinterException;
11
12 import javax.swing.JPanel;
13
14 /**
15  * Common interface for components we want to print. Used by PageFitPrintStrategy
16  *
17  * @author Jason Blood <dyster2000@gmail.com>
18  */
19 public class PrintableComponent extends JPanel implements Printable {
20
21     /**
22      * The printing offsets
23      */
24     private int offsetX = 0;
25     private int offsetY = 0;
26
27     /**
28      * Constructor.
29      */
30     public PrintableComponent() {
31         super(false);
32     }
33
34     /**
35      * From the java.awt.print.Printable interface.
36      * <p/>
37      * Prints the page at the specified index into the specified {@link java.awt.Graphics} context in the specified
38      * format. A <code>PrinterJob</code> calls the <code>Printable</code> interface to request that a page be rendered
39      * into the context specified by <code>graphics</code>.  The format of the page to be drawn is specified by
40      * <code>pageFormat</code>.  The zero based index of the requested page is specified by <code>pageIndex</code>. If
41      * the requested page does not exist then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. The
42      * <code>Graphics</code> class or subclass implements the {@link java.awt.print.PrinterGraphics} interface to
43      * provide additional information.  If the <code>Printable</code> object aborts the print job then it throws a
44      * {@link java.awt.print.PrinterException}.
45      * <p/>
46      * Note: This is not currently used in OpenRocket.  It's only here for reference.
47      *
48      * @param graphics   the context into which the page is drawn
49      * @param pageFormat the size and orientation of the page being drawn
50      * @param pageIndex  the zero based index of the page to be drawn
51      *
52      * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
53      *         non-existent page.
54      *
55      * @throws java.awt.print.PrinterException
56      *          thrown when the print job is terminated.
57      */
58     @Override
59     public int print (final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
60             throws PrinterException {
61
62         Graphics2D g2d = (Graphics2D) graphics;
63         PrintUtilities.translateToJavaOrigin(g2d, pageFormat);
64         PrintUtilities.disableDoubleBuffering(this);
65         paint(g2d);
66         PrintUtilities.enableDoubleBuffering(this);
67         return Printable.PAGE_EXISTS;
68     }
69
70         /**
71          * Set the offset this component will be printed to the page
72          * @param x     X offset to print at.
73          * @param y     Y offset to print at.
74          */
75         public void setPrintOffset(int x, int y) {
76         offsetX = x;
77         offsetY = y;
78         }
79
80         /**
81          * Get the X offset this component will be printed to the page
82          * @return X offset to print at.
83          */
84         public int getOffsetX() {
85                 return offsetX;
86         }
87
88         /**
89          * Get the Y offset this component will be printed to the page
90          * @return Y offset to print at.
91          */
92         public int getOffsetY() {
93                 return offsetY;
94         }
95 }