Example usage for java.awt.print Printable Printable

List of usage examples for java.awt.print Printable Printable

Introduction

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

Prototype

Printable

Source Link

Usage

From source file:MainClass.java

public static void main(String[] args) {
    try {//from   w w  w.  ja va2  s  . c  o m
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setJobName("Graphics Demo Printout");
        pjob.setCopies(1);
        pjob.setPrintable(new Printable() {
            public int print(Graphics pg, PageFormat pf, int pageNum) {
                if (pageNum > 0) // we only print one page
                    return Printable.NO_SUCH_PAGE; // ie., end of job

                pg.drawString("www.java2s.com", 10, 10);

                return Printable.PAGE_EXISTS;
            }
        });

        if (pjob.printDialog() == false) // choose printer
            return;
        pjob.print();
    } catch (PrinterException pe) {
        pe.printStackTrace();
    }
}

From source file:jhplot.gui.GHPanel.java

/**
 * Print the canvas// ww  w .  j  a v a2 s . co m
 * 
 */
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:org.rdv.viz.image.HighResImageViz.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 2  s .  c  om
 */
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./*w w w  .j av  a2s.  c  o 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();
        }
    }
}