X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fgui%2Fprint%2FPrintableComponent.java;fp=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fgui%2Fprint%2FPrintableComponent.java;h=de345fb9c57beafbb61c8975c347b23bdd5fe0d9;hb=9349577cdfdff682b2aabd6daa24fdc3a7449b58;hp=0000000000000000000000000000000000000000;hpb=30ba0a882f0c061176ba14dbf86d3d6fad096c02;p=debian%2Fopenrocket diff --git a/core/src/net/sf/openrocket/gui/print/PrintableComponent.java b/core/src/net/sf/openrocket/gui/print/PrintableComponent.java new file mode 100644 index 00000000..de345fb9 --- /dev/null +++ b/core/src/net/sf/openrocket/gui/print/PrintableComponent.java @@ -0,0 +1,122 @@ +/* + * PrintableComponent.java + */ +package net.sf.openrocket.gui.print; + +import javax.swing.JPanel; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.print.PageFormat; +import java.awt.print.Printable; +import java.awt.print.PrinterException; + +/** + * Common interface for components we want to print. Used by PageFitPrintStrategy + * + * @author Jason Blood + */ +public class PrintableComponent extends JPanel implements Printable, Comparable { + + /** + * The printing offsets. + */ + private int offsetX = 0; + private int offsetY = 0; + + /** + * Constructor. + */ + public PrintableComponent() { + super(false); + } + + /** + * From the java.awt.print.Printable interface. + *

+ * Prints the page at the specified index into the specified {@link java.awt.Graphics} context in the specified + * format. A PrinterJob calls the Printable interface to request that a page be rendered + * into the context specified by graphics. The format of the page to be drawn is specified by + * pageFormat. The zero based index of the requested page is specified by pageIndex. If + * the requested page does not exist then this method returns NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. The + * Graphics class or subclass implements the {@link java.awt.print.PrinterGraphics} interface to + * provide additional information. If the Printable object aborts the print job then it throws a + * {@link java.awt.print.PrinterException}. + *

+ * Note: This is not currently used in OpenRocket. It's only here for reference. + * + * @param graphics the context into which the page is drawn + * @param pageFormat the size and orientation of the page being drawn + * @param pageIndex the zero based index of the page to be drawn + * + * @return PAGE_EXISTS if the page is rendered successfully or NO_SUCH_PAGE if pageIndex specifies a + * non-existent page. + * + * @throws java.awt.print.PrinterException + * thrown when the print job is terminated. + */ + @Override + public int print (final Graphics graphics, final PageFormat pageFormat, final int pageIndex) + throws PrinterException { + + Graphics2D g2d = (Graphics2D) graphics; + PrintUtilities.translateToJavaOrigin(g2d, pageFormat); + PrintUtilities.disableDoubleBuffering(this); + paint(g2d); + PrintUtilities.enableDoubleBuffering(this); + return Printable.PAGE_EXISTS; + } + + /** + * Set the offset this component will be printed to the page. + * @param x X offset to print at. + * @param y Y offset to print at. + */ + public void setPrintOffset(int x, int y) { + offsetX = x; + offsetY = y; + } + + /** + * Get the X offset this component will be printed to the page. + * @return X offset to print at. + */ + public int getOffsetX() { + return offsetX; + } + + /** + * Get the Y offset this component will be printed to the page. + * @return Y offset to print at. + */ + public int getOffsetY() { + return offsetY; + } + + + /** + * Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer + * as this object is less than, equal to, or greater than the specified object. + * + * Bin packing theory says that trying to fit the biggest items first may have a better outcome. So this is sorted + * in size descending order, with width taking precedence over height. + * + * @param other the object to be compared. + * + * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the + * specified object. + * + * @throws NullPointerException if the specified object is null + * @throws ClassCastException if the specified object's type prevents it from being compared to this object. + */ + @Override + public int compareTo(final PrintableComponent other) { + int widthDiff = other.getWidth() - getWidth(); + if (widthDiff > 0) { + return 1; + } + else if (widthDiff < 0) { + return -1; + } + return other.getHeight() - getHeight(); + } +}