MCPcopy
hub / github.com/antlr/antlr4 / saveImage

Method saveImage

tool/src/org/antlr/v4/gui/GraphicsSupport.java:57–114  ·  view source on GitHub ↗

[The "BSD license"] Copyright (c) 2011 Cay Horstmann All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright n

(final JComponent comp, String fileName)

Source from the content-addressed store, hash-verified

55 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57 public static void saveImage(final JComponent comp, String fileName)
58 throws IOException, PrintException
59 {
60 if ( fileName.endsWith(".ps") || fileName.endsWith(".eps") ) {
61 DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
62 String mimeType = "application/postscript";
63 StreamPrintServiceFactory[] factories =
64 StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mimeType);
65 System.out.println(Arrays.toString(factories));
66 if (factories.length > 0) {
67 FileOutputStream out = new FileOutputStream(fileName);
68 PrintService service = factories[0].getPrintService(out);
69 SimpleDoc doc = new SimpleDoc(new Printable() {
70 @Override
71 public int print(Graphics g, PageFormat pf, int page) {
72 if (page >= 1) return Printable.NO_SUCH_PAGE;
73 else {
74 Graphics2D g2 = (Graphics2D) g;
75 g2.translate((pf.getWidth() - pf.getImageableWidth()) / 2,
76 (pf.getHeight() - pf.getImageableHeight()) / 2);
77 if ( comp.getWidth() > pf.getImageableWidth() ||
78 comp.getHeight() > pf.getImageableHeight() )
79 {
80 double sf1 = pf.getImageableWidth() / (comp.getWidth() + 1);
81 double sf2 = pf.getImageableHeight() / (comp.getHeight() + 1);
82 double s = Math.min(sf1, sf2);
83 g2.scale(s, s);
84 }
85
86 comp.paint(g);
87 return Printable.PAGE_EXISTS;
88 }
89 }
90 }, flavor, null);
91 DocPrintJob job = service.createPrintJob();
92 PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
93 job.print(doc, attributes);
94 out.close();
95 }
96 }
97 else {
98 // parrt: works with [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
99 Rectangle rect = comp.getBounds();
100 BufferedImage image = new BufferedImage(rect.width, rect.height,
101 BufferedImage.TYPE_INT_RGB);
102 Graphics2D g = (Graphics2D) image.getGraphics();
103 g.setColor(Color.WHITE);
104 g.fill(rect);
105// g.setColor(Color.BLACK);
106 comp.paint(g);
107 String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
108 boolean result = ImageIO.write(image, extension, new File(fileName));
109 if ( !result ) {
110 System.err.println("Now imager for " + extension);
111 }
112 g.dispose();
113 }
114 }

Callers 1

saveMethod · 0.95

Calls 7

printMethod · 0.80
paintMethod · 0.80
lastIndexOfMethod · 0.80
toStringMethod · 0.65
closeMethod · 0.45
fillMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected