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:com.rapidminer.gui.plotter.charts.AbstractChartPanel.java

/**
 * Prints the chart on a single page./*from   w  w  w  .  j a v  a 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.
 */

@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();
    this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null);
    return PAGE_EXISTS;

}

From source file:lu.fisch.unimozer.Diagram.java

private void printHeaderFooter(Graphics g, PageFormat pageFormat, int page, String className) {
    int origPage = page + 1;

    // Add header
    g.setColor(Color.BLACK);/*from   ww w.  j a v  a2 s  .c  o  m*/
    int xOffset = (int) pageFormat.getImageableX();
    int topOffset = (int) pageFormat.getImageableY() + 20;
    int bottom = (int) (pageFormat.getImageableY() + pageFormat.getImageableHeight());
    // header line
    g.drawLine(xOffset, topOffset - 8, xOffset + (int) pageFormat.getImageableWidth(), topOffset - 8);
    // footer line
    g.drawLine(xOffset, bottom - 11, xOffset + (int) pageFormat.getImageableWidth(), bottom - 11);
    g.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 10));

    Graphics2D gg = (Graphics2D) g;
    String pageString = "Page " + origPage;
    int tw = (int) gg.getFont().getStringBounds(pageString, gg.getFontRenderContext()).getWidth();
    // footer text
    g.drawString(pageString, xOffset + (int) pageFormat.getImageableWidth() - tw, bottom - 2);

    //System.err.println("Printing: "+directoryName);
    if (directoryName != null) {
        g.setFont(new Font(g.getFont().getFontName(), Font.ITALIC, 10));
        String filename = directoryName;
        if (!className.equals(""))
            filename += System.getProperty("file.separator") + className + ".java";
        // header text
        g.drawString(filename, xOffset, bottom - 2);
        File f = new File(filename);
        //System.err.println("Printing: "+filename);
        if (f.exists()) {
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            java.util.Date date = new java.util.Date();
            date.setTime(f.lastModified());
            String myDate = dateFormat.format(date);
            int w = (int) gg.getFont().getStringBounds(myDate, gg.getFontRenderContext()).getWidth();
            // header text
            g.drawString("File last modified on " + myDate, xOffset, topOffset - 10);
        }
    }
}

From source file:lu.fisch.unimozer.Diagram.java

@Override
public int print(Graphics g, PageFormat pageFormat, int page) throws PrinterException {
    /*//  w  w w.  j a va 2 s . c om
        // clone paper
        Paper originalPaper = pageFormat.getPaper();
        Paper paper = new Paper();
        // resize it
        paper.setSize(originalPaper.getWidth(), originalPaper.getHeight());
        paper.setImageableArea(
                originalPaper.getImageableX(),
                originalPaper.getImageableY()+30,
                originalPaper.getImageableWidth(),
                originalPaper.getImageableHeight()-60);
        // apply it
        pageFormat.setPaper(paper);
    */
    /*                Paper paper = new Paper();
        paper.setSize(pageFormat.getWidth(),pageFormat.getHeight());
                
        double paddingLeftRight = pageFormat.getImageableX();
        double paddingTopBottom = pageFormat.getImageableY()+30;
        if (pageFormat.getOrientation()==PageFormat.LANDSCAPE)
        {
            paddingLeftRight = 60;
            paddingTopBottom = 60;
        }
        paper.setImageableArea(paddingLeftRight,
                               paddingTopBottom,
                               pageFormat.getWidth()-2*paddingLeftRight,
                               pageFormat.getHeight()-2*paddingTopBottom);
        pageFormat.setPaper(paper);*/

    if (page == 0) {
        pageList.clear();
        if (printOptions.printCode() == true) {
            /*Set<String> set = classes.keySet();
            Iterator<String> itr = set.iterator();
            while (itr.hasNext())
            {
              String str = itr.next();*/

            /* let's try this one ... */
            for (Entry<String, MyClass> entry : classes.entrySet()) {
                // get the actual class ...
                String str = entry.getKey();

                MyClass thisClass = classes.get(str);
                CodeEditor edit = new CodeEditor();
                edit.setFont(new Font(edit.getFont().getName(), Font.PLAIN, printOptions.getFontSize()));

                String code = "";
                // get code, depending on JavaDoc filter
                if (printOptions.printJavaDoc())
                    code = thisClass.getContent().getText();
                else
                    code = thisClass.getJavaCodeCommentless();

                // filter double lines
                if (printOptions.filterDoubleLines()) {
                    StringList sl = StringList.explode(code, "\n");
                    sl.removeDoubleEmptyLines();
                    code = sl.getText();
                }

                //edit.setDiagram(diagram);
                edit.setCode(code);

                // resize the picture
                PageFormat pf = (PageFormat) pageFormat.clone();
                Paper pa = pf.getPaper();
                pa.setImageableArea(pa.getImageableX(), pa.getImageableY() + 20, pa.getImageableWidth(),
                        pa.getImageableHeight() - 40);
                pf.setPaper(pa);

                //sheets = new Vector<BufferedImage>();
                int p = 0;
                int result = 0;
                do {
                    /*BufferedImage img = new BufferedImage((int) pageFormat.getImageableWidth(),(int) pageFormat.getImageableHeight(),BufferedImage.TYPE_INT_RGB );
                    img.getGraphics().setColor(Color.WHITE);
                    img.getGraphics().fillRect(0,0,(int) pageFormat.getImageableWidth(),(int) pageFormat.getImageableHeight());*/

                    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
                    result = edit.print(img.createGraphics(), pf, p);
                    if (result == PAGE_EXISTS) {
                        //sheets.add(img);
                        pageList.add(str);
                        p++;
                    }
                } while (result == PAGE_EXISTS);

                //edit.print(g, pf, p);
                edit = null;
                System.gc();
            }
        }
    }

    if (page == 0 && printOptions.printDiagram() == true) {
        Graphics2D g2d = (Graphics2D) g;

        int yOffset = (int) pageFormat.getImageableY();

        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        double sX = (pageFormat.getImageableWidth() - 1) / getDiagramWidth();
        double sY = (pageFormat.getImageableHeight() - 1 - 40) / getDiagramHeight();
        double sca = Math.min(sX, sY);
        if (sca > 1) {
            sca = 1;
        }
        g2d.translate(0, 20);
        g2d.scale(sca, sca);

        paint(g2d);

        g2d.scale(1 / sca, 1 / sca);
        g2d.translate(0, -(20));
        g2d.translate(-pageFormat.getImageableX(), -pageFormat.getImageableY());

        printHeaderFooter(g2d, pageFormat, page, new String());

        PageFormat pf = (PageFormat) pageFormat.clone();
        Paper pa = pf.getPaper();
        pa.setImageableArea(pa.getImageableX(), pa.getImageableY() + 20, pa.getImageableWidth(),
                pa.getImageableHeight() - 40);
        pf.setPaper(pa);

        // reset the paper
        //pageFormat.setPaper(originalPaper);
        return (PAGE_EXISTS);
    } else {
        int origPage = page;

        if (printOptions.printDiagram() == true)
            page--;

        if (page >= pageList.size() || printOptions.printCode() == false)
            return (NO_SUCH_PAGE);
        else {
            String mc = pageList.get(page);
            page--;
            int p = 0;
            while (page >= 0) {
                if (pageList.get(page).equals(mc))
                    p++;
                page--;
            }
            MyClass thisClass = classes.get(mc);

            CodeEditor edit = new CodeEditor();
            edit.setFont(new Font(edit.getFont().getName(), Font.PLAIN, printOptions.getFontSize()));

            String code = "";
            // get code, depending on JavaDoc filter
            if (printOptions.printJavaDoc())
                code = thisClass.getContent().getText();
            else
                code = thisClass.getJavaCodeCommentless();

            // filter double lines
            if (printOptions.filterDoubleLines()) {
                StringList sl = StringList.explode(code, "\n");
                sl.removeDoubleEmptyLines();
                code = sl.getText();
            }

            edit.setCode(code);

            printHeaderFooter(g, pageFormat, origPage, thisClass.getShortName());

            PageFormat pf = (PageFormat) pageFormat.clone();
            Paper pa = pf.getPaper();
            pa.setImageableArea(pa.getImageableX(), pa.getImageableY() + 20, pa.getImageableWidth(),
                    pa.getImageableHeight() - 40);
            pf.setPaper(pa);

            edit.print(g, pf, p);
            edit = null;
            System.gc();

            // reset the paper
            //pageFormat.setPaper(originalPaper);
            return (PAGE_EXISTS);
        }
    }
}

From source file:org.gephi.ui.components.ReportSelection.java

/**
 *
 * @param graphics//from w  w w .  j  av a  2s .  c o m
 * @param pageFormat
 * @param pageIndex
 * @return
 */
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {

    boolean last = false;
    try {

        View rootView = displayPane.getUI().getRootView(displayPane);

        double scaleX = pageFormat.getImageableWidth() / displayPane.getMinimumSize().getWidth();

        scaleX = Math.min(scaleX, 1.0);
        double scaleY = scaleX;

        int end = (int) (pageIndex * ((1.0f / scaleY) * (double) pageFormat.getImageableHeight()));
        Rectangle allocation = new Rectangle(0, -end, (int) pageFormat.getImageableWidth(),
                (int) pageFormat.getImageableHeight());
        ((Graphics2D) graphics).scale(scaleX, scaleY);

        graphics.setClip((int) (pageFormat.getImageableX() / scaleX),
                (int) (pageFormat.getImageableY() / scaleY), (int) (pageFormat.getImageableWidth() / scaleX),
                (int) (pageFormat.getImageableHeight() / scaleY));

        ((Graphics2D) graphics).translate(((Graphics2D) graphics).getClipBounds().getX(),
                ((Graphics2D) graphics).getClipBounds().getY());

        rootView.paint(graphics, allocation);

        last = end > displayPane.getUI().getPreferredSize(displayPane).getHeight();

        if ((last)) {
            return Printable.NO_SUCH_PAGE;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Printable.PAGE_EXISTS;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.bundle.layout.PageDefinitionReadHandler.java

/**
 * Handles the page format./*w w  w.  ja v  a2 s  .  co m*/
 *
 * @param atts
 *          the attributes.
 * @throws SAXException
 *           if a parser error occurs or the validation failed.
 * @noinspection SuspiciousNameCombination
 */
private PageFormat configurePageSizeAndMargins(final Attributes atts, PageFormat format) throws SAXException {
    // (1) Grab the existing default ...
    float defTopMargin = (float) format.getImageableY();
    float defBottomMargin = (float) (format.getHeight() - format.getImageableHeight() - format.getImageableY());
    float defLeftMargin = (float) format.getImageableX();
    float defRightMargin = (float) (format.getWidth() - format.getImageableWidth() - format.getImageableX());

    // (2) Now configure the new paper-size
    format = configurePageSize(format, atts);

    // (3) Reconfigure margins as requested
    defTopMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-top"), defTopMargin);
    defBottomMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-bottom"), defBottomMargin);
    defLeftMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-left"), defLeftMargin);
    defRightMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-right"), defRightMargin);

    final Paper p = format.getPaper();
    switch (format.getOrientation()) {
    case PageFormat.PORTRAIT:
        PageFormatFactory.getInstance().setBorders(p, defTopMargin, defLeftMargin, defBottomMargin,
                defRightMargin);
        break;
    case PageFormat.REVERSE_LANDSCAPE:
        PageFormatFactory.getInstance().setBorders(p, defLeftMargin, defBottomMargin, defRightMargin,
                defTopMargin);
        break;
    case PageFormat.LANDSCAPE:
        PageFormatFactory.getInstance().setBorders(p, defRightMargin, defTopMargin, defLeftMargin,
                defBottomMargin);
        break;
    default:
        // will not happen..
        throw new IllegalArgumentException("Unexpected paper orientation.");
    }

    format.setPaper(p);
    return format;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.ext.readhandlers.PageReadHandler.java

/**
 * Handles the page format.//  w  w w .j a v  a 2 s  .  c o  m
 *
 * @param atts
 *          the attributes.
 * @throws SAXException
 *           if a parser error occurs or the validation failed.
 * @noinspection SuspiciousNameCombination
 */
private void handlePageFormat(final Attributes atts) throws SAXException {
    final MasterReport report = (MasterReport) getRootHandler()
            .getHelperObject(ReportParserUtil.HELPER_OBJ_REPORT_NAME);

    // grab the default page definition ...
    PageFormat format = report.getPageDefinition().getPageFormat(0);
    float defTopMargin = (float) format.getImageableY();
    float defBottomMargin = (float) (format.getHeight() - format.getImageableHeight() - format.getImageableY());
    float defLeftMargin = (float) format.getImageableX();
    float defRightMargin = (float) (format.getWidth() - format.getImageableWidth() - format.getImageableX());

    format = createPageFormat(format, atts);

    defTopMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.TOPMARGIN_ATT), defTopMargin);
    defBottomMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.BOTTOMMARGIN_ATT),
            defBottomMargin);
    defLeftMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.LEFTMARGIN_ATT),
            defLeftMargin);
    defRightMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.RIGHTMARGIN_ATT),
            defRightMargin);

    final Paper p = format.getPaper();
    switch (format.getOrientation()) {
    case PageFormat.PORTRAIT:
        PageFormatFactory.getInstance().setBorders(p, defTopMargin, defLeftMargin, defBottomMargin,
                defRightMargin);
        break;
    case PageFormat.LANDSCAPE:
        // right, top, left, bottom
        PageFormatFactory.getInstance().setBorders(p, defRightMargin, defTopMargin, defLeftMargin,
                defBottomMargin);
        break;
    case PageFormat.REVERSE_LANDSCAPE:
        PageFormatFactory.getInstance().setBorders(p, defLeftMargin, defBottomMargin, defRightMargin,
                defTopMargin);
        break;
    default:
        // will not happen..
        throw new IllegalArgumentException("Unexpected paper orientation.");
    }

    format.setPaper(p);
    pageFormat = format;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.simple.readhandlers.JFreeReportReadHandler.java

/**
 * Starts parsing.// w  w w .ja va2s. c  o m
 *
 * @param attrs
 *          the attributes.
 * @throws org.xml.sax.SAXException
 *           if there is a parsing error.
 * @noinspection SuspiciousNameCombination
 */
protected void startParsing(final PropertyAttributes attrs) throws SAXException {
    RootXmlReadHandler rootHandler = getRootHandler();
    final Object maybeReport = rootHandler.getHelperObject(ReportParserUtil.HELPER_OBJ_REPORT_NAME);
    final MasterReport report;
    if (maybeReport instanceof MasterReport == false) {
        // replace it ..
        report = new MasterReport();
        report.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.SOURCE, rootHandler.getSource());
    } else {
        report = (MasterReport) maybeReport;
    }

    final int groupCount = report.getGroupCount();
    for (int i = 0; i < groupCount; i++) {
        final Group g = report.getGroup(i);
        if (g instanceof RelationalGroup) {
            groupList.add((RelationalGroup) g);
        } else {
            throw new ParseException("The existing report contains non-default groups. "
                    + "This parser cannot handle such a construct.");
        }
    }

    final RootXmlReadHandler parser = rootHandler;
    if (ReportParserUtil.isIncluded(parser) == false) {
        final String query = attrs.getValue(getUri(), "query");
        if (query != null) {
            report.setQuery(query);
        }

        final String name = attrs.getValue(getUri(), JFreeReportReadHandler.NAME_ATT);
        if (name != null) {
            report.setName(name);
        }

        PageFormat format = report.getPageDefinition().getPageFormat(0);
        float defTopMargin = (float) format.getImageableY();
        float defBottomMargin = (float) (format.getHeight() - format.getImageableHeight()
                - format.getImageableY());
        float defLeftMargin = (float) format.getImageableX();
        float defRightMargin = (float) (format.getWidth() - format.getImageableWidth()
                - format.getImageableX());

        format = createPageFormat(format, attrs);

        defTopMargin = ParserUtil.parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.TOPMARGIN_ATT),
                defTopMargin);
        defBottomMargin = ParserUtil
                .parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.BOTTOMMARGIN_ATT), defBottomMargin);
        defLeftMargin = ParserUtil.parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.LEFTMARGIN_ATT),
                defLeftMargin);
        defRightMargin = ParserUtil.parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.RIGHTMARGIN_ATT),
                defRightMargin);

        final Paper p = format.getPaper();
        switch (format.getOrientation()) {
        case PageFormat.PORTRAIT:
            PageFormatFactory.getInstance().setBorders(p, defTopMargin, defLeftMargin, defBottomMargin,
                    defRightMargin);
            break;
        case PageFormat.LANDSCAPE:
            // right, top, left, bottom
            PageFormatFactory.getInstance().setBorders(p, defRightMargin, defTopMargin, defLeftMargin,
                    defBottomMargin);
            break;
        case PageFormat.REVERSE_LANDSCAPE:
            PageFormatFactory.getInstance().setBorders(p, defLeftMargin, defBottomMargin, defRightMargin,
                    defTopMargin);
            break;
        default:
            throw new IllegalStateException("Unexpected paper orientation.");
        }

        final int pageSpan = ParserUtil.parseInt(attrs.getValue(getUri(), JFreeReportReadHandler.PAGESPAN_ATT),
                1);

        format.setPaper(p);
        report.setPageDefinition(new SimplePageDefinition(format, pageSpan, 1));
    }
    if (rootHandler.getHelperObject(ReportParserUtil.HELPER_OBJ_LEGACY_STYLES) instanceof HashMap == false) {
        rootHandler.setHelperObject(ReportParserUtil.HELPER_OBJ_LEGACY_STYLES,
                new HashMap<String, ElementStyleSheet>());
    }
    rootHandler.setHelperObject(ReportParserUtil.HELPER_OBJ_REPORT_NAME, report);

    final String useMinChunkWidth = attrs.getValue(getUri(), "use-min-chunkwidth");
    if (useMinChunkWidth != null) {
        report.getStyle().setStyleProperty(ElementStyleKeys.USE_MIN_CHUNKWIDTH,
                ReportParserUtil.parseBoolean(useMinChunkWidth, getLocator()));
    }

    report.setCompatibilityLevel(ClassicEngineBoot.computeVersionId(3, 8, 0));
    this.report = report;
}

From source file:org.pentaho.reporting.engine.classic.core.util.PageFormatFactory.java

public static String printPageFormat(final PageFormat pf) {
    StringBuffer b = new StringBuffer();
    b.append("PageFormat={width=");
    b.append(pf.getWidth());/*from   ww w  .  ja v a2  s . co  m*/
    b.append(", height=");
    b.append(pf.getHeight());
    b.append(", imageableX=");
    b.append(pf.getImageableX());
    b.append(", imageableY=");
    b.append(pf.getImageableY());
    b.append(", imageableWidth=");
    b.append(pf.getImageableWidth());
    b.append(", imageableHeight=");
    b.append(pf.getImageableHeight());
    b.append(", orientation=").append(pf.getOrientation());
    b.append(", paper=");
    b.append(printPaper(pf.getPaper()));
    b.append("}");
    return b.toString();
}

From source file:org.rdv.viz.image.HighResImageViz.java

/**
 * Print the displayed image. If no image is being displayed, this will method
 * will do nothing.// w w  w  . ja  va  2 s  . com
 */
private void printImage() {
    // get the displayed image
    final Image displayedImage = getDisplayedImage();
    if (displayedImage == null) {
        return;
    }

    // setup a print job
    PrinterJob printJob = PrinterJob.getPrinterJob();

    // set the renderer for the image
    printJob.setPrintable(new Printable() {
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
            //we only have one page to print
            if (pageIndex != 0) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2d = (Graphics2D) g;

            // move to corner of imageable page
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            // get page dimensions
            double pageWidth = pageFormat.getImageableWidth();
            double pageHeight = pageFormat.getImageableHeight();

            // get image dimensions
            int imageWidth = displayedImage.getWidth(null);
            int imageHeight = displayedImage.getHeight(null);

            // get scale factor for image
            double widthScale = pageWidth / imageWidth;
            double heightScale = pageHeight / imageHeight;
            double scale = Math.min(widthScale, heightScale);

            // draw image with width and height scaled to page
            int scaledWidth = (int) (scale * imageWidth);
            int scaledHeight = (int) (scale * imageHeight);
            g2d.drawImage(displayedImage, 0, 0, scaledWidth, scaledHeight, null);

            return Printable.PAGE_EXISTS;
        }

    });

    // set the job name to the channel name (plus jpg extension)
    // this is used as a hint for a file name when printing to file
    String channelName = (String) seriesList_.getChannels().iterator().next();
    String jobName = channelName.replace("/", " - ");
    if (!jobName.endsWith(".jpg")) {
        jobName += ".jpg";
    }
    printJob.setJobName(jobName);

    // show the print dialog and print if ok clicked
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (PrinterException pe) {
            JOptionPane.showMessageDialog(null, "Failed to print image.", "Print Image Error",
                    JOptionPane.ERROR_MESSAGE);
            pe.printStackTrace();
        }
    }
}

From source file:org.rdv.viz.image.ImageViz.java

/**
 * Print the displayed image. If no image is being displayed, this will method
 * will do nothing.//from w w  w  . j a v  a  2s.  co m
 */
private void printImage() {
    // get the displayed image
    final Image displayedImage = getDisplayedImage();
    if (displayedImage == null) {
        return;
    }

    // setup a print job
    PrinterJob printJob = PrinterJob.getPrinterJob();

    // set the renderer for the image
    printJob.setPrintable(new Printable() {
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
            //we only have one page to print
            if (pageIndex != 0) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2d = (Graphics2D) g;

            // move to corner of imageable page
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            // get page dimensions
            double pageWidth = pageFormat.getImageableWidth();
            double pageHeight = pageFormat.getImageableHeight();

            // get image dimensions
            int imageWidth = displayedImage.getWidth(null);
            int imageHeight = displayedImage.getHeight(null);

            // get scale factor for image
            double widthScale = pageWidth / imageWidth;
            double heightScale = pageHeight / imageHeight;
            double scale = Math.min(widthScale, heightScale);

            // draw image with width and height scaled to page
            int scaledWidth = (int) (scale * imageWidth);
            int scaledHeight = (int) (scale * imageHeight);
            g2d.drawImage(displayedImage, 0, 0, scaledWidth, scaledHeight, null);

            return Printable.PAGE_EXISTS;
        }

    });

    // set the job name to the channel name (plus jpg extension)
    // this is used as a hint for a file name when printing to file
    String channelName = (String) channels.iterator().next();
    String jobName = channelName.replace("/", " - ");
    if (!jobName.endsWith(".jpg")) {
        jobName += ".jpg";
    }
    printJob.setJobName(jobName);

    // show the print dialog and print if ok clicked
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (PrinterException pe) {
            JOptionPane.showMessageDialog(null, "Failed to print image.", "Print Image Error",
                    JOptionPane.ERROR_MESSAGE);
            pe.printStackTrace();
        }
    }
}