public static void printRaw (PrintJob _job, Graphics _g, JPanel _p) { _p.printAll(_g); /* Component[] c=_p.getComponents(); for(int i=0;i<c.length;i++) if(c[i].isVisible()) { int x=c[i].getX(); int y=c[i].getY(); System.err.println("_g="+_g+" "+x+" "+y); _g.translate(x,y); c[i].print(_g); _g.translate(-x,-y); } */ }
JPanel panel = new JPanel(new BorderLayout(), true); panel.add(BorderLayout.NORTH, table1); panel.add(BorderLayout.SOUTH, table2); panel.printAll(g);
/** * Exports the plot to an image file. * @param file the destination file. * @throws IOException if an error occurs during writing. */ public void save(File file) throws IOException { BufferedImage bi = new BufferedImage(contentPane.getWidth(), contentPane.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bi.createGraphics(); contentPane.printAll(g2d); ImageIO.write(bi, FileChooser.getExtension(file), file); }
@Override public int print(java.awt.Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { // We have only one page, and 'page' is zero-based return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; // User (0,0) is typically outside the imageable area, so we must // translate by the X and Y values in the PageFormat to avoid clipping g2d.translate(pf.getImageableX(), pf.getImageableY()); // Scale plots to paper size. double scaleX = pf.getImageableWidth() / contentPane.getWidth(); double scaleY = pf.getImageableHeight() / contentPane.getHeight(); g2d.scale(scaleX, scaleY); // Disable double buffering RepaintManager currentManager = RepaintManager.currentManager(this); currentManager.setDoubleBufferingEnabled(false); // Now we perform our rendering contentPane.printAll(g); // Enable double buffering currentManager.setDoubleBufferingEnabled(true); // tell the caller that this page is part of the printed document return PAGE_EXISTS; }