private void saveView(File saveTo, JPanel view) { BufferedImage image = new BufferedImage(view.getPreferredSize().width, view.getPreferredSize().height, BufferedImage.TYPE_4BYTE_ABGR); view.print(image.getGraphics()); try { ImageIO.write(image, "png", saveTo); } catch (IOException e) { //Handle exception } }
JPanel panel = new JPanel (); panel.setLayout(new FlowLayout()); panel.setBackground(Color.WHITE); panel.add(vv); Properties p = new Properties(); p.setProperty("PageSize","A4"); // vv is the VirtualizationViewer VectorGraphics g = new PDFGraphics2D(new File("Network.pdf"), vv); g.setProperties(p); g.startExport(); panel.print(g); g.endExport();
@Override public void print(final Graphics g) { try { preparePrinting(); super.print(g); } finally { isPrinting = false; } }
private static BufferedImage renderImage(JPanel panel) { JFrame frame = new JFrame(); frame.setUndecorated(true); frame.getContentPane().add(panel); frame.pack(); BufferedImage bi = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = bi.createGraphics(); panel.print(graphics); graphics.dispose(); frame.dispose(); return bi; }
void takePicture(JPanel panel) { BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB); panel.print(img.getGraphics()); // or: panel.printAll(...); try { ImageIO.write(img, "jpg", new File("panel.jpg")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public BufferedImage createImage(JPanel panel) { int w = panel.getWidth(); int h = panel.getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); panel.print(g); return bi; }
public BufferedImage createImage(JPanel panel) { int w = panel.getWidth(); int h = panel.getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); panel.print(g); return bi; }
/** * Prints the panel with all its components. * <p> * {@inheritDoc} */ @Override public void print (Graphics g) { super.print(g); // prints the container only (i.e. the background) // print components Component[] comps = getComponents(); for (int i=0; i < comps.length; i++) { Component c = comps[i]; Point loc = c.getLocation(); g.translate(loc.x, loc.y); if (c.isVisible()) { c.print(g); // if this a PrintPanel, go recursively down } g.translate(-loc.x, -loc.y); } }
PdfTemplate template = contentByte.createTemplate(500, 500); Graphics2D g2 = template.createGraphics(500, 500); panel.print(g2); g2.dispose(); contentByte.addTemplate(template, 30, 300);
public static void main(String[] args) throws Exception { JPanel yourPanel = new YourPanel(); // run with -Djava.awt.headless=true Headless headless = new Headless(yourPanel); headless.pack(); headless.setVisible(true); File out = new File("YourPanel.pdf"); VectorGraphics graphics = new PDFGraphics2D(out, yourPanel); graphics.startExport(); yourPanel.print(graphics); graphics.endExport(); }