List of usage examples for java.awt.print PrinterJob getPrinterJob
public static PrinterJob getPrinterJob()
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 www . j a v a2 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) 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(); } } }
From source file:org.sanjose.util.JRPrinterAWT.java
/** * *//*from w w w. ja va 2s. c o m*/ private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog, PrintService pService) throws JRException { boolean isOK = true; if (firstPageIndex < 0 || firstPageIndex > lastPageIndex || lastPageIndex >= jasperPrint.getPages().size()) { throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of " + jasperPrint.getPages().size()); } pageOffset = firstPageIndex; PrinterJob printJob = PrinterJob.getPrinterJob(); // fix for bug ID 6255588 from Sun bug database initPrinterJobFields(printJob); try { printJob.setPrintService(pService); } catch (PrinterException e) { e.printStackTrace(); throw new JRException(e.getMessage()); } PageFormat pageFormat = printJob.defaultPage(); Paper paper = pageFormat.getPaper(); printJob.setJobName("JasperReports - " + jasperPrint.getName()); switch (jasperPrint.getOrientationValue()) { case LANDSCAPE: { pageFormat.setOrientation(PageFormat.LANDSCAPE); paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth()); paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth()); break; } case PORTRAIT: default: { pageFormat.setOrientation(PageFormat.PORTRAIT); paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight()); paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight()); } } pageFormat.setPaper(paper); Book book = new Book(); book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1); printJob.setPageable(book); try { if (withPrintDialog) { if (printJob.printDialog()) { printJob.print(); } else { isOK = false; } } else { printJob.print(); } } catch (Exception ex) { throw new JRException("Error printing report.", ex); } return isOK; }
From source file:pcgen.gui2.dialog.PrintPreviewDialog.java
@Override public void actionPerformed(ActionEvent e) { if (SHEET_COMMAND.equals(e.getActionCommand())) { new PreviewLoader((URI) sheetBox.getSelectedItem()).execute(); } else if (PAGE_COMMAND.equals(e.getActionCommand())) { previewPanel.setPage(pageBox.getSelectedIndex()); } else if (ZOOM_COMMAND.equals(e.getActionCommand())) { Double zoom = (Double) zoomBox.getSelectedItem(); previewPanel.setScaleFactor(zoom); } else if (ZOOM_IN_COMMAND.equals(e.getActionCommand())) { Double zoom = (Double) zoomBox.getSelectedItem(); zoomBox.setSelectedItem(zoom * ZOOM_MULTIPLIER); } else if (ZOOM_OUT_COMMAND.equals(e.getActionCommand())) { Double zoom = (Double) zoomBox.getSelectedItem(); zoomBox.setSelectedItem(zoom / ZOOM_MULTIPLIER); } else if (PRINT_COMMAND.equals(e.getActionCommand())) { PrinterJob printerJob = PrinterJob.getPrinterJob(); printerJob.setPageable(pageable); if (printerJob.printDialog()) { try { printerJob.print();//from w ww. ja va2 s .co m dispose(); } catch (PrinterException ex) { String message = "Could not print " + character.getNameRef().get(); Logging.errorPrint(message, ex); frame.showErrorMessage(Constants.APPLICATION_NAME, message); } } } else if (CANCEL_COMMAND.equals(e.getActionCommand())) { dispose(); } }
From source file:processing.app.Editor.java
/** * Handler for File → Page Setup. *///w w w .ja v a 2s . c o m public void handlePageSetup() { PrinterJob printerJob = PrinterJob.getPrinterJob(); if (pageFormat == null) { pageFormat = printerJob.defaultPage(); } pageFormat = printerJob.pageDialog(pageFormat); }
From source file:processing.app.Editor.java
/** * Handler for File → Print./*from w w w. j a v a2 s . c o m*/ */ public void handlePrint() { statusNotice(_("Printing...")); //printerJob = null; PrinterJob printerJob = PrinterJob.getPrinterJob(); if (pageFormat != null) { //System.out.println("setting page format " + pageFormat); printerJob.setPrintable(textarea, pageFormat); } else { printerJob.setPrintable(textarea); } // set the name of the job to the code name printerJob.setJobName(sketch.getCurrentCode().getPrettyName()); if (printerJob.printDialog()) { try { printerJob.print(); statusNotice(_("Done printing.")); } catch (PrinterException pe) { statusError(_("Error while printing.")); pe.printStackTrace(); } } else { statusNotice(_("Printing canceled.")); } //printerJob = null; // clear this out? }
From source file:Samples.Advanced.GraphEditorDemo.java
/** * a driver for this demo//from ww w . j a v a2 s . c om */ @SuppressWarnings("serial") public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final GraphEditorDemo demo = new GraphEditorDemo(); JMenu menu = new JMenu("File"); menu.add(new AbstractAction("Make Image") { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(demo); if (option == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); demo.writeJPEGImage(file); } } }); menu.add(new AbstractAction("Print") { public void actionPerformed(ActionEvent e) { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(demo); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } } }); JPopupMenu.setDefaultLightWeightPopupEnabled(false); JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); JMenu matrixMenu = new JMenu(); matrixMenu.setText("Matrix"); matrixMenu.setIcon(null); matrixMenu.setPreferredSize(new Dimension(80, 20)); menuBar.add(matrixMenu); JMenuItem copyMatrix = new JMenuItem("Copy Matrix to clipboard", KeyEvent.VK_C); copyMatrix.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); menuBar.add(copyMatrix); frame.setJMenuBar(menuBar); frame.getContentPane().add(demo); copyMatrix.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Vem textTransfer = new Vem(); //Set up Matrix List<Integer[]> graphMatrix = new ArrayList<Integer[]>(); int MatrixSize = graph.getVertexCount(); Integer[] activeV = new Integer[MatrixSize]; int count = 0; int activeVPos = 0; while (activeVPos < MatrixSize) { if (graph.containsVertex(count)) { activeV[activeVPos] = count; activeVPos++; } count++; } // sgv.g.getVertices().toArray() ((Integer[])(sgv.g.getVertices().toArray())) for (int i = 0; i < MatrixSize; i++) { Integer[] tempArray = new Integer[MatrixSize]; for (int j = 0; j < MatrixSize; j++) { if (graph.findEdge(activeV[i], activeV[j]) != null) { tempArray[j] = 1; } else { tempArray[j] = 0; } } graphMatrix.add(tempArray); } //graphMatrix.add(new Integer[]{1, 2, 3}); //graphMatrix.add(new Integer[]{4, 5 , 6, 7}); //System.out.println(matrixToString(graphMatrix)); //System.out.println(matrixToMathematica(graphMatrix)); textTransfer.setClipboardContents("" + matrixToMathematica(graphMatrix)); System.out.println("Clipboard contains:" + textTransfer.getClipboardContents()); } }); frame.pack(); frame.setVisible(true); }
From source file:Samples.Advanced.GraphEditorDemo2.java
/** * a driver for this demo/*from ww w . j av a 2 s .co m*/ */ @SuppressWarnings("serial") public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final GraphEditorDemo2 demo = new GraphEditorDemo2(); JMenu menu = new JMenu("File"); menu.add(new AbstractAction("Make Image") { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(demo); if (option == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); demo.writeJPEGImage(file); } } }); menu.add(new AbstractAction("Print") { public void actionPerformed(ActionEvent e) { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(demo); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } } }); JPopupMenu.setDefaultLightWeightPopupEnabled(false); JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); frame.setJMenuBar(menuBar); frame.getContentPane().add(demo); frame.pack(); frame.setVisible(true); }