Example usage for java.awt.print PageFormat getImageableX

List of usage examples for java.awt.print PageFormat getImageableX

Introduction

In this page you can find the example usage for java.awt.print PageFormat getImageableX.

Prototype

public double getImageableX() 

Source Link

Document

Returns the x coordinate of the upper left point of the imageable area of the Paper object associated with this PageFormat .

Usage

From source file:PrintUIWindow.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }//w w w .j  av  a 2  s .  c o m

    /*
     * User (0,0) is typically outside the imageable area, so we must translate
     * by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now print the window and its visible contents */
    frameToPrint.printAll(g);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

From source file:JavaWorldPrintExample1.java

/**
 * Method: print/*from w  ww .jav  a  2s.  co  m*/
 * <p>
 * 
 * This class is responsible for rendering a page using the provided
 * parameters. The result will be a grid where each cell will be half an
 * inch by half an inch.
 * 
 * @param g
 *            a value of type Graphics
 * @param pageFormat
 *            a value of type PageFormat
 * @param page
 *            a value of type int
 * @return a value of type int
 */
public int print(Graphics g, PageFormat pageFormat, int page) {

    int i;
    Graphics2D g2d;
    Line2D.Double line = new Line2D.Double();

    //--- Validate the page number, we only print the first page
    if (page == 0) {

        //--- Create a graphic2D object a set the default parameters
        g2d = (Graphics2D) g;
        g2d.setColor(Color.black);

        //--- Translate the origin to be (0,0)
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        //--- Print the vertical lines
        for (i = 0; i < pageFormat.getWidth(); i += INCH / 2) {
            line.setLine(i, 0, i, pageFormat.getHeight());
            g2d.draw(line);
        }

        //--- Print the horizontal lines
        for (i = 0; i < pageFormat.getHeight(); i += INCH / 2) {
            line.setLine(0, i, pageFormat.getWidth(), i);
            g2d.draw(line);
        }

        return (PAGE_EXISTS);
    } else
        return (NO_SUCH_PAGE);
}

From source file:BookTest.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    if (page > getPageCount(g2, pf))
        return Printable.NO_SUCH_PAGE;
    g2.translate(pf.getImageableX(), pf.getImageableY());

    drawPage(g2, pf, page);//from w  w  w  .  j  av  a  2s .com
    return Printable.PAGE_EXISTS;
}

From source file:MainClass.java

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    // pageIndex 0 corresponds to page number 1.
    if (pageIndex >= 1)
        return Printable.NO_SUCH_PAGE;

    PrinterGraphics p = (PrinterGraphics) g;

    System.out.println(p.getPrinterJob().getCopies());
    System.out.println(p.getPrinterJob().getJobName());

    Graphics2D g2 = (Graphics2D) g;

    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();

    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();

    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);

    g2.setColor(Color.red);/*from ww  w  .ja va2s  .c  om*/
    g2.draw(r);

    Shape s = new Ellipse2D.Double(xo + 4, yo + 4, 32, 32);

    g2.fill(s);

    return Printable.PAGE_EXISTS;
}

From source file:BookTest.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page >= 1)
        return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.black);/*from  w  w  w .j av a  2 s  .c o  m*/
    g2.translate(pf.getImageableX(), pf.getImageableY());
    FontRenderContext context = g2.getFontRenderContext();
    Font f = g2.getFont();
    TextLayout layout = new TextLayout(title, f, context);
    float ascent = layout.getAscent();
    g2.drawString(title, 0, ascent);
    return Printable.PAGE_EXISTS;
}

From source file:GraphEditorDemo.java

public int print(java.awt.Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex)
        throws java.awt.print.PrinterException {
    if (pageIndex > 0) {
        return (Printable.NO_SUCH_PAGE);
    } else {/* www  .j  av a2 s.  co m*/
        java.awt.Graphics2D g2d = (java.awt.Graphics2D) graphics;
        vv.setDoubleBuffered(false);
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        vv.paint(g2d);
        vv.setDoubleBuffered(true);

        return (Printable.PAGE_EXISTS);
    }
}

From source file:org.gumtree.vis.core.internal.SWTChartComposite.java

public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }/*  w w  w. j  a  va  2 s .  co m*/
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    getChart().draw(g2, new Rectangle2D.Double(x, y, w, h), null, null);
    return PAGE_EXISTS;

}

From source file:PrintableComponent.java

/**
 * This is the "callback" method that the PrinterJob will invoke.
 * This method is defined by the Printable interface.
 *//*from w  ww. j  a v  a 2 s  .  c o m*/
public int print(Graphics g, PageFormat format, int pagenum) {
    // The PrinterJob will keep trying to print pages until we return
    // this value to tell it that it has reached the end.
    if (pagenum > 0)
        return Printable.NO_SUCH_PAGE;

    // We're passed a Graphics object, but it can always be cast to Graphics2D
    Graphics2D g2 = (Graphics2D) g;

    // Use the top and left margins specified in the PageFormat Note
    // that the PageFormat methods are poorly named.  They specify
    // margins, not the actual imageable area of the printer.
    g2.translate(format.getImageableX(), format.getImageableY());

    // Tell the component to draw itself to the printer by passing in 
    // the Graphics2D object.  This will not work well if the component
    // has double-buffering enabled.
    c.paint(g2);

    // Return this constant to tell the PrinterJob that we printed the page.
    return Printable.PAGE_EXISTS;
}

From source file:org.schreibubi.JCombinations.ui.GridChartPanel.java

public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0)
        return (Printable.NO_SUCH_PAGE);
    else {//from  ww  w  .  j a  v  a  2 s  .co m
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        print(g2d);
        return (Printable.PAGE_EXISTS);
    }
}

From source file:mavn.network.view.JUNGPanelAdapter.java

@Override
public int print(java.awt.Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex)
        throws java.awt.print.PrinterException {
    if (pageIndex > 0) {
        return (Printable.NO_SUCH_PAGE);
    } else {//from www .  j  a v  a2 s  .  c o  m
        java.awt.Graphics2D g2d = (java.awt.Graphics2D) graphics;
        vv.setDoubleBuffered(false);
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        vv.paint(g2d);
        vv.setDoubleBuffered(true);

        return (Printable.PAGE_EXISTS);
    }
}