Example usage for java.awt.print PrinterJob setPrintable

List of usage examples for java.awt.print PrinterJob setPrintable

Introduction

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

Prototype

public abstract void setPrintable(Printable painter);

Source Link

Document

Calls painter to render the pages.

Usage

From source file:JavaWorldPrintExample1.java

/**
 * Constructor: Example1//from  w ww.j a  va2  s .c  om
 * <p>
 *  
 */
public JavaWorldPrintExample1() {

    //--- Create a printerJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Set the printable class to this one since we
    //--- are implementing the Printable interface
    printJob.setPrintable(this);

    //--- Show a print dialog to the user. If the user
    //--- click the print button, then print otherwise
    //--- cancel the print job
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }

}

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

/**
 * Carry out the print operation for the component 
 *
 * @throws java.awt.print.PrinterException If there is an error in printing
 *//*www .  j  a va  2 s .  com*/
public void print() throws PrinterException {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);

    if (printJob.printDialog()) {
        try {
            log.debug("Calling PrintJob.print()");
            printJob.print();
            log.debug("End PrintJob.print()");
        } catch (PrinterException pe) {
            log.error("Error printing: " + pe);

            throw pe;
        }
    }
}

From source file:org.martus.client.swingui.actions.ActionMenuCharts.java

private boolean printToPrinter(JFreeChart chart) throws PrinterException {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(new PrintableChart(chart));
    HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    removeJavaLogoFromTitle(attributes);
    if (!printJob.printDialog(attributes))
        return false;

    printJob.print(attributes);/*from   w  ww .j a v  a2 s. c o m*/
    return true;
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.PeakAnnotationCalibrationPanel.java

public void onPrint() {
    PrinterJob pj = theWorkspace.getPrinterJob();
    if (pj == null)
        return;//from  w  ww  .j a va  2  s .  co  m

    try {
        pj.setPrintable(theChartPanel);
        if (pj.printDialog())
            pj.print();
    } catch (Exception e) {
        LogUtils.report(e);
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.AnnotationReportApplet.java

public void onPrint() {
    try {/* w w w .j  av a 2  s. co m*/
        PrinterJob pj = theCanvas.getWorkspace().getPrinterJob();
        if (pj != null) {
            pj.setPrintable(theCanvas);
            if (pj.printDialog())
                theCanvas.print(pj);
        }
    } catch (Exception e) {
        LogUtils.report(e);
    }
}

From source file:jhplot.gui.GHPanel.java

/**
 * Print the canvas/*from   w w w .  ja va  2 s  .c o 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:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * @param actionableTree/*from  w  w  w .ja  v  a 2s .  c  o m*/
 */
public void print(final JTree actionableTree) {
    /*if (true)
    {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(OrientationRequested.LANDSCAPE);
    aset.add(new Copies(2));
    aset.add(new JobName("My job", null));
            
    // Create a print job 
    PrinterJob pj = PrinterJob.getPrinterJob();       
    pj.setPrintable(this);
    // locate a print service that can handle the request 
    PrintService[] services =
        PrinterJob.lookupPrintServices();
            
    if (services.length > 0) {
        System.out.println("selected printer " + services[0].getName());
        try {
            pj.setPrintService(services[0]);
            pj.pageDialog(aset);
            if(pj.printDialog(aset)) {
                pj.print(aset);
            }
        } catch (PrinterException pe) { 
            System.err.println(pe);
        }
    }
    return;
    }*/

    DefaultMutableTreeNode clonedTree = GhostActionableTree
            .makeDeepCopy((DefaultMutableTreeNode) actionableTree.getModel().getRoot());
    GhostActionableTree printTree = new GhostActionableTree(this, new DefaultTreeModel(clonedTree));
    printTree.setRowHeight(ROW_HEIGHT);
    //printTree.setEditable(false);
    //printTree.setVisibleRowCount(15);
    ContainerTreeRenderer renderer = new ContainerTreeRenderer(null, false, false);
    renderer.setBGColor(Color.WHITE);
    renderer.setFont(getFont().deriveFont(8.0f));
    //renderer.setLeafIcon(IconManager.getIcon(CollectionObject.class.getSimpleName(), IconManager.IconSize.Std32));
    //renderer.setVerticalTextPosition(SwingConstants.CENTER);
    printTree.setCellRenderer(renderer);

    for (int row = 0; row < printTree.getRowCount(); row++) {
        printTree.expandRow(row);
    }

    PrintablePanel p = new PrintablePanel(new BorderLayout(), printTree);
    p.add(printTree, BorderLayout.CENTER);

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    //PrinterResolution        pr   = new PrinterResolution(300, 300, PrinterResolution.DPI);
    //MediaPrintableArea       mpa  = new MediaPrintableArea(8,21, 210-16, 296-42, MediaPrintableArea.MM);

    //aset.add( MediaSizeName.IS);
    //aset.add( pr );
    //aset.add( mpa );
    aset.add(new Copies(1));
    aset.add(OrientationRequested.PORTRAIT);
    aset.add(PrintQuality.HIGH);

    PrinterJob job = PrinterJob.getPrinterJob();
    /*PageFormat pageFormat = job.defaultPage();
    Paper      paper      = pageFormat.getPaper();
    paper.setSize(pageFormat.getWidth(), pageFormat.getHeight());
    paper.setImageableArea(
    0,
    0,
    pageFormat.getWidth(),
    pageFormat.getHeight()
    );
    //aset.add( Fidelity.FIDELITY_TRUE );
    pageFormat.setPaper(paper);
            
    Book book = new Book();
    book.append(p, pageFormat, 1);
    job.setPageable(book);*/

    job.setPrintable(p);
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print(aset);

        } catch (PrinterException ex) {
            ex.printStackTrace();
            /* The job did not successfully complete */
        }
    }
}

From source file:org.openscience.jmol.app.Jmol.java

/**
 * added print command, so that it can be used by RasmolScriptHandler
 **///  w  w w.j  a va2  s .c  o  m
public void print() {

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(display);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException e) {
            Logger.error("Error while printing", e);
        }
    }
}

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   ww  w.ja  v a  2  s. 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) 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  ww. 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) 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();
        }
    }
}