Example usage for java.awt.print PageFormat getImageableY

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

Introduction

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

Prototype

public double getImageableY() 

Source Link

Document

Returns the y 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;
    }//from   w  w w  . ja va 2s.  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/*  w w  w . 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  . ja va 2 s . com
    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  ww .  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: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 v  a 2s .  co m
    g2.draw(r);

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

    g2.fill(s);

    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 {//from   ww w  . jav  a  2 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);
    }
}

From source file:com.alvermont.terraj.util.io.ImagePrinter.java

/**
 * Render an image for printing./*from w  w  w  .  jav a  2  s  .  c o m*/
 *
 * @param graphics The graphics context to print on
 * @param pageFormat The page format being used
 * @param pageIndex The page being printed
 * @throws java.awt.print.PrinterException If there is an error in printing
 * @return An indication of the result of page rendering for this page
 */
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
    final Graphics2D g2 = (Graphics2D) graphics;

    // we only expect to be printing 1 page as the image is scaled
    if (pageIndex >= 1) {
        return Printable.NO_SUCH_PAGE;
    }

    // translate the coordinate system to match up the image with 
    // the printable area
    g2.translate(getFormat().getImageableX(), pageFormat.getImageableY());

    final Rectangle componentBounds = new Rectangle(getImage().getWidth(), getImage().getHeight());
    g2.translate(-componentBounds.x, -componentBounds.y);

    // scale the image to fit
    scaleToFit(true);
    g2.scale(getScaleX(), getScaleY());

    // render the image
    g2.drawImage(getImage(), null, 0, 0);

    // done
    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;
    }//from   ww w . jav  a2 s .c  o 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.
 *//*  ww  w.  j a va2 s . c  om*/
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   www  .  j a v  a 2 s .  c om
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        print(g2d);
        return (Printable.PAGE_EXISTS);
    }
}