List of usage examples for java.awt.print PageFormat getImageableHeight
public double getImageableHeight()
From source file:BookTest.java
/** * Draws 1/2" crop marks in the corners of the page. * @param g2 the graphics context/*from w w w . j a v a 2s . com*/ * @param pf the page format */ public void drawCropMarks(Graphics2D g2, PageFormat pf) { final double C = 36; // crop mark length = 1/2 inch double w = pf.getImageableWidth(); double h = pf.getImageableHeight(); g2.draw(new Line2D.Double(0, 0, 0, C)); g2.draw(new Line2D.Double(0, 0, C, 0)); g2.draw(new Line2D.Double(w, 0, w, C)); g2.draw(new Line2D.Double(w, 0, w - C, 0)); g2.draw(new Line2D.Double(0, h, 0, h - C)); g2.draw(new Line2D.Double(0, h, C, h)); g2.draw(new Line2D.Double(w, h, w, h - C)); g2.draw(new Line2D.Double(w, h, w - C, h)); }
From source file:MainClass.java
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { // pageIndex 0 corresponds to page number 1. if (pageIndex >= 1) return Printable.NO_SUCH_PAGE; PrinterGraphics p = (PrinterGraphics) g; System.out.println(p.getPrinterJob().getCopies()); System.out.println(p.getPrinterJob().getJobName()); Graphics2D g2 = (Graphics2D) g; double w = pf.getImageableWidth(); double h = pf.getImageableHeight(); int xo = (int) pf.getImageableX(); int yo = (int) pf.getImageableY(); Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h); g2.setColor(Color.red);/*from w w w . j av a 2 s .com*/ g2.draw(r); Shape s = new Ellipse2D.Double(xo + 4, yo + 4, 32, 32); g2.fill(s); return Printable.PAGE_EXISTS; }
From source file:BookTest.java
public void drawPage(Graphics2D g2, PageFormat pf, int page) { if (message.equals("")) return;/*from w w w . j a va 2 s. com*/ page--; // account for cover page drawCropMarks(g2, pf); g2.clip(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight())); g2.translate(-page * pf.getImageableWidth(), 0); g2.scale(scale, scale); FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); TextLayout layout = new TextLayout(message, f, context); AffineTransform transform = AffineTransform.getTranslateInstance(0, layout.getAscent()); Shape outline = layout.getOutline(transform); g2.draw(outline); }
From source file:BookTest.java
/** * Gets the page count of this section./* ww w . j a v a 2 s . co m*/ * @param g2 the graphics context * @param pf the page format * @return the number of pages needed */ public int getPageCount(Graphics2D g2, PageFormat pf) { if (message.equals("")) return 0; FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); Rectangle2D bounds = f.getStringBounds(message, context); scale = pf.getImageableHeight() / bounds.getHeight(); double width = scale * bounds.getWidth(); int pages = (int) Math.ceil(width / pf.getImageableWidth()); return pages; }
From source file:com.alvermont.terraj.util.io.PrintUtilities.java
/** * Print the component into a graphics context * //from w w w .ja va 2 s . c o m * @param g The <code>Graphics</code> object to be used for printing. * This should be a <code>Graphics2D</code> instance * @param pf The page format to be used * @param pageIndex The number of the page being printed * @return An indication of whether the page existed. Silly people using * magic numbers. Oh well. */ public int print(Graphics g, PageFormat pf, int pageIndex) { int response = NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) g; // for faster printing, turn off double buffering disableDoubleBuffering(componentToBePrinted); Dimension d = componentToBePrinted.getSize(); //get size of document double panelWidth = d.width; //width in pixels double panelHeight = d.height; //height in pixels double pageHeight = pf.getImageableHeight(); //height of printer page double pageWidth = pf.getImageableWidth(); //width of printer page double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); // make sure not print empty pages if (pageIndex >= totalNumPages) { response = NO_SUCH_PAGE; } else { // shift Graphic to line up with beginning of print-imageable region g2.translate(pf.getImageableX(), pf.getImageableY()); // shift Graphic to line up with beginning of next page to print g2.translate(0f, -pageIndex * pageHeight); // scale the page so the width fits... g2.scale(scale, scale); componentToBePrinted.paint(g2); //repaint the page for printing enableDoubleBuffering(componentToBePrinted); response = Printable.PAGE_EXISTS; } return response; }
From source file:org.gumtree.vis.core.internal.SWTChartComposite.java
public int print(Graphics g, PageFormat pf, int pageIndex) { if (pageIndex != 0) { return NO_SUCH_PAGE; }/*from w w w .j a v a2 s . c om*/ Graphics2D g2 = (Graphics2D) g; double x = pf.getImageableX(); double y = pf.getImageableY(); double w = pf.getImageableWidth(); double h = pf.getImageableHeight(); getChart().draw(g2, new Rectangle2D.Double(x, y, w, h), null, null); return PAGE_EXISTS; }
From source file:PrintCanvas3D.java
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; }// w w w. j av a2 s . com Graphics2D g2d = (Graphics2D) g; //g2d.translate(pf.getImageableX(), pf.getImageableY()); AffineTransform t2d = new AffineTransform(); t2d.translate(pf.getImageableX(), pf.getImageableY()); double xscale = pf.getImageableWidth() / (double) bImage.getWidth(); double yscale = pf.getImageableHeight() / (double) bImage.getHeight(); double scale = Math.min(xscale, yscale); t2d.scale(scale, scale); try { g2d.drawImage(bImage, t2d, this); } catch (Exception ex) { ex.printStackTrace(); return Printable.NO_SUCH_PAGE; } return Printable.PAGE_EXISTS; }
From source file:fr.ign.cogit.geoxygene.appli.layer.LayerViewAwtPanel.java
@Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= 1) { return Printable.NO_SUCH_PAGE; }//from www. j a v a2 s. c om Graphics2D g2d = (Graphics2D) graphics; // translate to the upper left corner of the page format g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // translate to the middle of the page format g2d.translate(pageFormat.getImageableWidth() / 2, pageFormat.getImageableHeight() / 2); Dimension d = this.getSize(); double scale = Math.min(pageFormat.getImageableWidth() / d.width, pageFormat.getImageableHeight() / d.height); if (scale < 1.0) { g2d.scale(scale, scale); } // translate of half the size of the graphics to paint for it to be // centered g2d.translate(-d.width / 2.0, -d.height / 2.0); // copy the rendered layers into the graphics this.getRenderingManager().copyTo(g2d); return Printable.PAGE_EXISTS; }
From source file:playground.singapore.calibration.charts.CustomChartPanel.java
@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { System.err.println("PRINTING"); //Divide the current page format into sections based //on the layout instructions received in the constructor //a new pagelayout is created for each cell in the grid //that will then be passed along to the print method of //each chart panel. if (pageIndex != 0) { return NO_SUCH_PAGE; }/*w ww. ja v a2 s . c om*/ List<PageFormat> pageFormats = new ArrayList<PageFormat>(); //setup all the page formats needed for the grid cells. double x = pf.getImageableX(); double y = pf.getImageableY(); double cellWidth = pf.getImageableWidth() / layoutInstructions.getColumns(); double cellHeight = pf.getImageableHeight() / layoutInstructions.getRows(); for (int i = 1; i <= layoutInstructions.getRows(); i++) { double rowOffset = (i - 1) * cellHeight + y; for (int j = 1; j <= layoutInstructions.getColumns(); j++) { PageFormat format = new PageFormat(); Paper paper = new Paper(); double columnOffset = (j - 1) * cellWidth + x; paper.setImageableArea(columnOffset, rowOffset, cellWidth, cellHeight); format.setPaper(paper); pageFormats.add(format); } } //have each chartpanel print on the graphics context using its //particular PageFormat int size = Math.min(pageFormats.size(), panels.size()); for (int i = 0; i < size; i++) { panels.get(i).print(g, pageFormats.get(i), pageIndex); } return PAGE_EXISTS; }
From source file:playground.artemc.calibration.charts.CustomChartPanel.java
@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { System.err.println("PRINTING"); //Divide the current page format into sections based //on the layout instructions received in the constructor //a new pagelayout is created for each cell in the grid //that will then be passed along to the print method of //each chart panel. if (pageIndex != 0) { return NO_SUCH_PAGE; }//from w ww .j a va 2s. c o m List<PageFormat> pageFormats = new ArrayList<PageFormat>(); //setup all the page formats needed for the grid cells. double x = pf.getImageableX(); double y = pf.getImageableY(); double cellWidth = pf.getImageableWidth() / layoutInstructions.getColumns(); double cellHeight = pf.getImageableHeight() / layoutInstructions.getRows(); for (int i = 1; i <= layoutInstructions.getRows(); i++) { double rowOffset = (i - 1) * cellHeight + y; for (int j = 1; j <= layoutInstructions.getColumns(); j++) { PageFormat format = new PageFormat(); Paper paper = new Paper(); double columnOffset = (j - 1) * cellWidth + x; paper.setImageableArea(columnOffset, rowOffset, cellWidth, cellHeight); format.setPaper(paper); pageFormats.add(format); } } //have each chartpanel print on the graphics context using its //particular PageFormat int size = Math.min(pageFormats.size(), panels.size()); for (int i = 0; i < size; i++) { panels.get(i).print(g, pageFormats.get(i), pageIndex); } return PAGE_EXISTS; }