2 * PrintableComponent.java
4 package net.sf.openrocket.gui.print;
6 import javax.swing.JPanel;
7 import java.awt.Graphics;
8 import java.awt.Graphics2D;
9 import java.awt.print.PageFormat;
10 import java.awt.print.Printable;
11 import java.awt.print.PrinterException;
14 * Common interface for components we want to print. Used by PageFitPrintStrategy
16 * @author Jason Blood <dyster2000@gmail.com>
18 public class PrintableComponent extends JPanel implements Printable, Comparable<PrintableComponent> {
21 * The printing offsets.
23 private int offsetX = 0;
24 private int offsetY = 0;
29 public PrintableComponent() {
34 * From the java.awt.print.Printable interface.
36 * Prints the page at the specified index into the specified {@link java.awt.Graphics} context in the specified
37 * format. A <code>PrinterJob</code> calls the <code>Printable</code> interface to request that a page be rendered
38 * into the context specified by <code>graphics</code>. The format of the page to be drawn is specified by
39 * <code>pageFormat</code>. The zero based index of the requested page is specified by <code>pageIndex</code>. If
40 * the requested page does not exist then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. The
41 * <code>Graphics</code> class or subclass implements the {@link java.awt.print.PrinterGraphics} interface to
42 * provide additional information. If the <code>Printable</code> object aborts the print job then it throws a
43 * {@link java.awt.print.PrinterException}.
45 * Note: This is not currently used in OpenRocket. It's only here for reference.
47 * @param graphics the context into which the page is drawn
48 * @param pageFormat the size and orientation of the page being drawn
49 * @param pageIndex the zero based index of the page to be drawn
51 * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
54 * @throws java.awt.print.PrinterException
55 * thrown when the print job is terminated.
58 public int print (final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
59 throws PrinterException {
61 Graphics2D g2d = (Graphics2D) graphics;
62 PrintUtilities.translateToJavaOrigin(g2d, pageFormat);
63 PrintUtilities.disableDoubleBuffering(this);
65 PrintUtilities.enableDoubleBuffering(this);
66 return Printable.PAGE_EXISTS;
70 * Set the offset this component will be printed to the page.
71 * @param x X offset to print at.
72 * @param y Y offset to print at.
74 public void setPrintOffset(int x, int y) {
80 * Get the X offset this component will be printed to the page.
81 * @return X offset to print at.
83 public int getOffsetX() {
88 * Get the Y offset this component will be printed to the page.
89 * @return Y offset to print at.
91 public int getOffsetY() {
97 * Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer
98 * as this object is less than, equal to, or greater than the specified object.
100 * Bin packing theory says that trying to fit the biggest items first may have a better outcome. So this is sorted
101 * in size descending order, with width taking precedence over height.
103 * @param other the object to be compared.
105 * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the
108 * @throws NullPointerException if the specified object is null
109 * @throws ClassCastException if the specified object's type prevents it from being compared to this object.
112 public int compareTo(final PrintableComponent other) {
113 int widthDiff = other.getWidth() - getWidth();
117 else if (widthDiff < 0) {
120 return other.getHeight() - getHeight();