View Javadoc
1   package com.github.davidmoten.xuml;
2   
3   import java.awt.Graphics;
4   import java.awt.Graphics2D;
5   import java.awt.image.BufferedImage;
6   import java.awt.print.PageFormat;
7   import java.awt.print.Printable;
8   import java.awt.print.PrinterException;
9   import java.awt.print.PrinterJob;
10  import java.io.File;
11  import java.io.FileNotFoundException;
12  import java.io.FileOutputStream;
13  import java.io.IOException;
14  import java.io.OutputStream;
15  
16  import javax.imageio.ImageIO;
17  import javax.swing.JPanel;
18  
19  public final class Panels {
20  
21      public static void saveImage(JPanel panel, File file) {
22          try (FileOutputStream fos = new FileOutputStream(file)) {
23              saveImage(panel, fos);
24          } catch (FileNotFoundException e) {
25              throw new RuntimeException(e);
26          } catch (IOException e) {
27              throw new RuntimeException(e);
28          }
29      }
30  
31      public static void saveImage(JPanel panel, OutputStream os) {
32          BufferedImage bi = new BufferedImage(panel.getPreferredSize().width,
33                  panel.getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
34          Graphics2D g2 = bi.createGraphics();
35          panel.setDoubleBuffered(false);
36          panel.paint(g2);
37          panel.setDoubleBuffered(true);
38          g2.dispose();
39          try {
40              ImageIO.write(bi, "png", os);
41          } catch (IOException e) {
42              throw new RuntimeException(e);
43          }
44      }
45  
46      public static void print(final JPanel panel) throws PrinterException {
47          PrinterJob pj = PrinterJob.getPrinterJob();
48          pj.setJobName("State Diagram");
49          pj.setCopies(1);
50          PageFormat format = pj.defaultPage();
51          if (panel.getPreferredSize().getWidth() > panel.getPreferredSize().getHeight())
52              format.setOrientation(PageFormat.LANDSCAPE);
53          else
54              format.setOrientation(PageFormat.PORTRAIT);
55  
56          pj.setPrintable(new Printable() {
57              @Override
58              public int print(Graphics pg, PageFormat pf, int pageNum) {
59                  if (pageNum > 0)
60                      return Printable.NO_SUCH_PAGE;
61                  Graphics2D g2 = (Graphics2D) pg;
62                  double w;
63                  double h;
64                  if (pf.getOrientation() == PageFormat.LANDSCAPE) {
65                      w = pf.getPaper().getImageableHeight();
66                      h = pf.getPaper().getImageableWidth();
67                  } else {
68                      w = pf.getPaper().getImageableWidth();
69                      h = pf.getPaper().getImageableHeight();
70                  }
71                  double scalex = w / panel.getPreferredSize().getWidth();
72                  double scaley = h / panel.getPreferredSize().getHeight();
73                  double scale = Math.min(scalex, scaley);
74  
75                  g2.translate(pf.getImageableX(), pf.getImageableY());
76                  g2.scale(scale, scale);
77                  panel.setDoubleBuffered(false);
78                  panel.paint(g2);
79                  panel.setDoubleBuffered(true);
80                  return Printable.PAGE_EXISTS;
81              }
82          });
83          if (pj.printDialog() == false)
84              return;
85          pj.print();
86      }
87  
88  }