Example usage for java.awt.print PageFormat getImageableWidth

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

Introduction

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

Prototype

public double getImageableWidth() 

Source Link

Document

Returns the width, in 1/72nds of an inch, of the imageable area of the page.

Usage

From source file:org.gumtree.vis.awt.CompositePanel.java

@Override
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }/*from   w  ww .j  av a 2s  .  c o m*/
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    double screenWidth = getWidth();
    double screenHeight = getHeight();
    double widthRatio = w / screenWidth;
    double heightRatio = h / screenHeight;
    double overallRatio = 1;
    overallRatio = widthRatio < heightRatio ? widthRatio : heightRatio;
    //        Rectangle2D printArea = new Rectangle2D.Double(x, y, screenWidth * overallRatio, 
    //              screenHeight * overallRatio);
    BufferedImage image = getImage();
    g2.drawImage(image, (int) x, (int) y, (int) (screenWidth * overallRatio),
            (int) (screenHeight * overallRatio), null);
    //        draw(g2, printArea, x, y);
    g2.dispose();
    return PAGE_EXISTS;

    //        XYPlot plot = (XYPlot) getChart().getPlot();
    //        Font domainFont = plot.getDomainAxis().getLabelFont();
    //        int domainSize = domainFont.getSize();
    //        Font rangeFont = plot.getRangeAxis().getLabelFont();
    //        int rangeSize = rangeFont.getSize();
    //        Font titleFont = getChart().getTitle().getFont();
    //        int titleSize = titleFont.getSize();
    //        Font domainScaleFont = plot.getDomainAxis().getTickLabelFont();
    //        int domainScaleSize = domainScaleFont.getSize();
    //        Font rangeScaleFont = plot.getRangeAxis().getTickLabelFont();
    //        int rangeScaleSize = rangeScaleFont.getSize();
    //        plot.getDomainAxis().setLabelFont(domainFont.deriveFont(
    //              (float) (domainSize * overallRatio)));
    //        plot.getRangeAxis().setLabelFont(rangeFont.deriveFont(
    //              (float) (rangeSize * overallRatio)));
    //        getChart().getTitle().setFont(titleFont.deriveFont(
    //              (float) (titleSize * overallRatio)));
    //        plot.getDomainAxis().setTickLabelFont(domainScaleFont.deriveFont(
    //              (float) (domainScaleSize * overallRatio)));
    //        plot.getRangeAxis().setTickLabelFont(rangeScaleFont.deriveFont(
    //              (float) (rangeScaleSize * overallRatio)));
    //        
    //        Rectangle2D chartArea = (Rectangle2D) printArea.clone();
    //        getChart().getPadding().trim(chartArea);
    //        AxisUtilities.trimTitle(chartArea, g2, getChart().getTitle(), getChart().getTitle().getPosition());
    //        
    //        Axis scaleAxis = null;
    //        Font scaleAxisFont = null;
    //        int scaleAxisFontSize = 0;
    //        for (Object object : getChart().getSubtitles()) {
    //           Title title = (Title) object;
    //           if (title instanceof PaintScaleLegend) {
    //              scaleAxis = ((PaintScaleLegend) title).getAxis();
    //              scaleAxisFont = scaleAxis.getTickLabelFont();
    //              scaleAxisFontSize = scaleAxisFont.getSize();
    //              scaleAxis.setTickLabelFont(scaleAxisFont.deriveFont(
    //                    (float) (scaleAxisFontSize * overallRatio)));
    //           }
    //           AxisUtilities.trimTitle(chartArea, g2, title, title.getPosition());
    //        }
    //        AxisSpace axisSpace = AxisUtilities.calculateAxisSpace(
    //              getChart().getXYPlot(), g2, chartArea);
    //        Rectangle2D dataArea = axisSpace.shrink(chartArea, null);
    //        getChart().getXYPlot().getInsets().trim(dataArea);
    //        getChart().getXYPlot().getAxisOffset().trim(dataArea);
    //        
    ////        Rectangle2D screenArea = getScreenDataArea();
    ////        Rectangle2D visibleArea = getVisibleRect();
    ////        Rectangle2D printScreenArea = new Rectangle2D.Double(screenArea.getMinX() * overallRatio + x, 
    ////              screenArea.getMinY() * overallRatio + y, 
    ////              printArea.getWidth() - visibleArea.getWidth() + screenArea.getWidth(), 
    ////              printArea.getHeight() - visibleArea.getHeight() + screenArea.getHeight());
    //
    //        getChart().draw(g2, printArea, getAnchor(), null);
    //        ChartMaskingUtilities.drawMasks(g2, dataArea, 
    //              getMasks(), null, getChart(), overallRatio);
    //        plot.getDomainAxis().setLabelFont(domainFont);
    //        plot.getRangeAxis().setLabelFont(rangeFont);
    //        getChart().getTitle().setFont(titleFont);
    //        plot.getDomainAxis().setTickLabelFont(domainScaleFont);
    //        plot.getRangeAxis().setTickLabelFont(rangeScaleFont);
    //        if (scaleAxis != null) {
    //           scaleAxis.setTickLabelFont(scaleAxisFont);
    //        }
    //        return PAGE_EXISTS;
}

From source file:org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram.java

/**
 * Prints the chart on a single page.//ww w  .j  ava 2  s . c o  m
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing 
 *                   gets print.
 *
 * @return The result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null);
    return PAGE_EXISTS;

}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
    if (pageIndex > 0) {
        return NO_SUCH_PAGE;
    } else {//w  ww. j a  v a2 s  .co  m
        Graphics2D g2d = (Graphics2D) g;
        g2d.setBackground(Color.white);
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        Dimension td = this.getPreferredSize();
        double sx = pageFormat.getImageableWidth() / td.width;
        double sy = pageFormat.getImageableHeight() / td.height;
        double s = Math.min(sx, sy);
        if (s < 1.)
            g2d.scale(s, s);

        RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
        this.paint(g2d);
        RepaintManager.currentManager(this).setDoubleBufferingEnabled(true);

        return PAGE_EXISTS;
    }
}

From source file:jhplot.gui.GHPanel.java

/**
 * Print the canvas//from  w ww  . java  2  s.c  om
 * 
 */
public void printGraph() {

    if (isBorderShown())
        showBorders(false);
    CanvasPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    Thread t = new Thread() {
        public void run() {
            try {
                PrinterJob prnJob = PrinterJob.getPrinterJob();
                // set the Printable to the PrinterJob
                prnJob.setPrintable(new Printable() {
                    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
                        if (pageIndex == 0) {
                            Graphics2D g2d = (Graphics2D) graphics;
                            double ratioX = pageFormat.getImageableWidth() / CanvasPanel.getSize().width;
                            double ratioY = pageFormat.getImageableHeight() / CanvasPanel.getSize().height;
                            double factor = Math.min(ratioX, ratioY);
                            g2d.scale(factor, factor);
                            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                            disableDoubleBuffering(CanvasPanel);
                            CanvasPanel.print(g2d);
                            enableDoubleBuffering(CanvasPanel);
                            return Printable.PAGE_EXISTS;
                        }
                        return Printable.NO_SUCH_PAGE;
                    }
                });

                if (prnJob.printDialog()) {
                    JHPlot.showStatusBarText("Printing..");
                    prnJob.print();
                }
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    };
    t.start();
    CanvasPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}

From source file:PageFormatFactory.java

/**
 * Logs the page format./* w  w w  . j  a  v a2  s  .c om*/
 *
 * @param pf the page format.
 */
public static void logPageFormat(final PageFormat pf) {
    System.out.println("PageFormat: Width: " + pf.getWidth() + " Height: " + pf.getHeight());
    System.out.println("PageFormat: Image: X " + pf.getImageableX() + " Y " + pf.getImageableY() + " W: "
            + pf.getImageableWidth() + " H: " + pf.getImageableHeight());
    System.out.println("PageFormat: Margins: X " + pf.getImageableX() + " Y " + pf.getImageableY() + " X2: "
            + (pf.getImageableWidth() + pf.getImageableX()) + " Y2: "
            + (pf.getImageableHeight() + pf.getImageableY()));
}

From source file:org.tsho.dmc2.core.chart.jfree.DmcChartPanel.java

/**
 * Prints the chart on a single page.//from   w w  w .  j a v a  2  s .  c om
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing gets print.
 *
 * @return the result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    chart.draw(g2, new Rectangle2D.Double(x, y, w, h), null);
    return PAGE_EXISTS;

}

From source file:MyJava3D.java

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
    if (pi >= 1) {
        return Printable.NO_SUCH_PAGE;
    }/* w  ww  .  j a v  a  2s  . c  o  m*/

    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    g2d.translate(pf.getImageableWidth() / 2, pf.getImageableHeight() / 2);

    Dimension d = getSize();

    double scale = Math.min(pf.getImageableWidth() / d.width, pf.getImageableHeight() / d.height);
    if (scale < 1.0) {
        g2d.scale(scale, scale);
    }

    g2d.translate(-d.width / 2.0, -d.height / 2.0);

    if (bimg == null) {
        Graphics2D g2 = createGraphics2D(d.width, d.height, null, g2d);
        render(d.width, d.height, g2);
        g2.dispose();
    } else {
        g2d.drawImage(bimg, 0, 0, this);
    }

    return Printable.PAGE_EXISTS;
}

From source file:org.gumtree.vis.awt.JChartPanel.java

/**
 * Prints the chart on a single page.//from  w  ww . j a  v  a2s  .  c om
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing
 *                   gets print.
 *
 * @return The result of printing.
 */
@Override
public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    double screenWidth = getWidth();
    double screenHeight = getHeight();
    double widthRatio = w / screenWidth;
    double heightRatio = h / screenHeight;
    double overallRatio = 1;
    overallRatio = widthRatio < heightRatio ? widthRatio : heightRatio;
    Rectangle2D printArea = new Rectangle2D.Double(x, y, screenWidth * overallRatio,
            screenHeight * overallRatio);
    draw(g2, printArea, 0, 0);
    return PAGE_EXISTS;
}

From source file:com.isti.traceview.common.TraceViewChartPanel.java

/**
 * Prints the chart on a single page./*from w w w  .j a v a2s. c om*/
 * 
 * @param g
 *            the graphics context.
 * @param pf
 *            the page format to use.
 * @param pageIndex
 *            the index of the page. If not <code>0</code>, nothing gets print.
 * @return The result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null);
    return PAGE_EXISTS;
}

From source file:PageFormatFactory.java

public Insets getPageMargins(final PageFormat format) {

    final int marginLeft = (int) format.getImageableX();
    final int marginRight = (int) (format.getWidth() - format.getImageableWidth() - format.getImageableX());
    final int marginTop = (int) (format.getImageableY());
    final int marginBottom = (int) (format.getHeight() - format.getImageableHeight() - format.getImageableY());
    return new Insets(marginTop, marginLeft, marginBottom, marginRight);
}