title: Fit/Scale JComponent to page being printed
body: <p>I'm trying to scale my component so it can fit on a single printed page (portrait or landscape)</p>
  
  <pre><code> gDiagram.getComponent()
  </code></pre>
  
  <p>is the component (JPanel) I'm trying to print. </p>
  
  <p>Here's what I've got so far based on <a href="https://stackoverflow.com/questions/750310/how-can-i-print-a-single-jpanels-contents">How can I print a single JPanel&#39;s contents?</a></p>
  
  <pre><code>/**
   * Prints the diagram.
   */
  public void printDiagram() {
      PrinterJob pj = PrinterJob.getPrinterJob();
      pj.setJobName(" Print Component ");
  
      pj.setPrintable(new Printable() {
          @Override
          public int print(Graphics g, PageFormat pf, int pageNumber)
                  throws PrinterException {
              // TODO Auto-generated method stub
              if (pageNumber &gt; 0) {
                  return Printable.NO_SUCH_PAGE;
              }
  
              Graphics2D g2 = (Graphics2D) g;
              g2.translate(pf.getImageableX(), pf.getImageableY());
  
              double sx = pf.getImageableWidth() / gDiagram.getComponent().getWidth();
              double sy = pf.getImageableHeight() / gDiagram.getComponent().getHeight();
  
              gDiagram.getComponent().paint(g2);
              g2.scale(sx, sy);
              return Printable.PAGE_EXISTS;
          }
      });
  
      if (!pj.printDialog()) {
          return;
      }
      try {
          pj.print();
      } catch (PrinterException ex) {
          System.out.println(ex);
      }
  }
  </code></pre>
  
  <p>I'm not too familiar with graphics, so any help would be appreciated </p>