public Print2DPrinterJob() {

                /* Construct the print request specification.
                * The print data is a Printable object.
                * the request additonally specifies a job name, 2 copies, and
                * landscape orientation of the media.
                */
                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);
                        }
                }
        }
示例#2
0
  /**
   * 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);
    }
  }