/** * Starts the print operation. This is quite expensive, and is kicked off in a separate thread. */ public void print() { final PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); // make a local copy of the object to be printed, for use by the print thread // below... final Component printMe = getPrintComponent(); Thread worker = new Thread() { public void run() { if (job.printDialog()) { try { // need to make sure that no other thread tries to // run a print job and set printCompnent at the same // time... synchronized (this) { printComponent = printMe; job.print(); printComponent = null; } } catch (Exception ex) { log.warning("error printing: " + ex); } } } }; worker.start(); }
/** * Show a dialog for printing the current drawing. * * @param fff the parent frame which will be used for dialogs and message boxes. * @param CCr the CircuitPanel containing the drawing to be exported. */ public void printDrawing(JFrame fff, CircuitPanel CCr) { cc = CCr; DialogPrint dp = new DialogPrint(fff); dp.setMirror(printMirror); dp.setFit(printFitToPage); dp.setBW(printBlackWhite); dp.setLandscape(printLandscape); dp.setVisible(true); // Get some information about the printing options. printMirror = dp.getMirror(); printFitToPage = dp.getFit(); printLandscape = dp.getLandscape(); printBlackWhite = dp.getBW(); Vector<LayerDesc> ol = cc.dmp.getLayers(); if (dp.shouldPrint()) { if (printBlackWhite) { Vector<LayerDesc> v = new Vector<LayerDesc>(); // Here we create an alternative array of layers in // which all colors are pitch black. This may be // useful for PCB's. for (int i = 0; i < LayerDesc.MAX_LAYERS; ++i) v.add( new LayerDesc( new ColorSwing(Color.black), ((LayerDesc) ol.get(i)).getVisible(), "B/W", ((LayerDesc) ol.get(i)).getAlpha())); cc.dmp.setLayers(v); } PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // Set the correct printing orientation. if (printLandscape) { aset.add(OrientationRequested.LANDSCAPE); } else { aset.add(OrientationRequested.PORTRAIT); } job.print(aset); } catch (PrinterException ex) { // The job did not successfully complete JOptionPane.showMessageDialog(fff, Globals.messages.getString("Print_uncomplete")); } } cc.dmp.setLayers(ol); } }
// Graphically print the component to the printer protected synchronized void printComponent(Component c) { printTarget = c; PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(this); pj.printDialog(); try { pj.print(); } catch (Exception printException) { printException.printStackTrace(); } }
/** print the canvas scaled with factor <code>scale</code> */ public void print(double scale) { printScale = scale; PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(this); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } }