List of usage examples for java.awt.print PageFormat getImageableY
public double getImageableY()
From source file:mavn.network.view.JUNGPanelAdapter.java
@Override public int print(java.awt.Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex) throws java.awt.print.PrinterException { if (pageIndex > 0) { return (Printable.NO_SUCH_PAGE); } else {/* w w w .j a v a 2s. c o m*/ java.awt.Graphics2D g2d = (java.awt.Graphics2D) graphics; vv.setDoubleBuffered(false); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); vv.paint(g2d); vv.setDoubleBuffered(true); 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; }/* w ww .j a v a 2 s. c o m*/ 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:PrintCanvas3D.java
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; }/*from w w w . j a va 2 s . c om*/ 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: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; }//www. j a v a 2 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. ja va2 s. co 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; }
From source file:org.fhaes.fhfilechecker.FrameViewOutput.java
@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { try {/*from w w w . j av a 2 s .c o m*/ // For catching IOException if (pageIndex != rememberedPageIndex) { // First time we've visited this page rememberedPageIndex = pageIndex; // If encountered EOF on previous page, done if (rememberedEOF) { return Printable.NO_SUCH_PAGE; } // Save current position in input file rememberedFilePointer = raf.getFilePointer(); } else { raf.seek(rememberedFilePointer); } g.setColor(Color.black); g.setFont(fnt); int x = (int) pf.getImageableX() + 10; int y = (int) pf.getImageableY() + 12; // Title line g.drawString("File: " + fileName + ", page: " + (pageIndex + 1), x, y); // Generate as many lines as will fit in imageable area y += 36; while (y + 12 < pf.getImageableY() + pf.getImageableHeight()) { String line = raf.readLine(); if (line == null) { rememberedEOF = true; break; } g.drawString(line, x, y); y += 12; } return Printable.PAGE_EXISTS; } catch (Exception e) { return Printable.NO_SUCH_PAGE; } }
From source file:PaginationExample.java
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { Font font = new Font("Serif", Font.PLAIN, 10); FontMetrics metrics = g.getFontMetrics(font); int lineHeight = metrics.getHeight(); if (pageBreaks == null) { initTextLines();// w w w . j a va2 s .c om int linesPerPage = (int) (pf.getImageableHeight() / lineHeight); int numBreaks = (textLines.length - 1) / linesPerPage; pageBreaks = new int[numBreaks]; for (int b = 0; b < numBreaks; b++) { pageBreaks[b] = (b + 1) * linesPerPage; } } if (pageIndex > pageBreaks.length) { return NO_SUCH_PAGE; } /* * 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 Since we are * drawing text we */ Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); /* * Draw each line that is on this page. Increment 'y' position by lineHeight * for each line. */ int y = 0; int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1]; int end = (pageIndex == pageBreaks.length) ? textLines.length : pageBreaks[pageIndex]; for (int line = start; line < end; line++) { y += lineHeight; g.drawString(textLines[line], 0, y); } /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; }
From source file:com.alvermont.terraj.util.io.PrintUtilities.java
/** * Print the component into a graphics context * /* ww w .java2 s . c om*/ * @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:be.fedict.eidviewer.gui.printing.IDPrintout.java
public int print(Graphics graphics, PageFormat pageFormat, int pageNumber) throws PrinterException { // we only support printing all in one single page if (pageNumber > 0) return Printable.NO_SUCH_PAGE; logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("width", pageFormat.getWidth()).append("height", pageFormat.getHeight()) .append("imageableWidth", pageFormat.getImageableWidth()) .append("imageableHeight", pageFormat.getImageableHeight()) .append("imageableX", pageFormat.getImageableX()).append("imageableY", pageFormat.getImageableY()) .append("orientation", pageFormat.getOrientation()) .append("paper.width", pageFormat.getPaper().getWidth()) .append("paper.height", pageFormat.getPaper().getHeight()) .append("paper.imageableWidth", pageFormat.getPaper().getImageableWidth()) .append("paper.imageableHeight", pageFormat.getPaper().getImageableHeight()) .append("paper.imageableX", pageFormat.getPaper().getImageableX()) .append("paper.imageableY", pageFormat.getPaper().getImageableY()).toString()); logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("clip.width", graphics.getClipBounds().width) .append("clip.height", graphics.getClipBounds().height).append("clip.x", graphics.getClipBounds().x) .append("clip.y", graphics.getClipBounds().y).toString()); // translate graphics2D with origin at top left first imageable location Graphics2D graphics2D = (Graphics2D) graphics; graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // keep imageable width and height as variables for clarity (we use them often) float imageableWidth = (float) pageFormat.getImageableWidth(); float imageableHeight = (float) pageFormat.getImageableHeight(); // Coat of Arms images are stored at approx 36 DPI, scale 1/2 to get to Java default of 72DPI AffineTransform coatOfArmsTransform = new AffineTransform(); coatOfArmsTransform.scale(0.5, 0.5); // photo images are stored at approx 36 DPI, scale 1/2 to get to Java default of 72DPI AffineTransform photoTransform = new AffineTransform(); photoTransform.scale(0.5, 0.5);//from w ww . ja v a 2 s .c om photoTransform.translate((imageableWidth * 2) - (photo.getWidth(this)), 0); // make sure foreground is black, and draw coat of Arms and photo at the top of the page // using the transforms to scale them to 72DPI. graphics2D.setColor(Color.BLACK); graphics2D.drawImage(coatOfArms, coatOfArmsTransform, null); graphics2D.drawImage(photo, photoTransform, null); // calculate some sizes that need to take into account the scaling of the graphics, to avoid dragging // those non-intuitive "/2" further along in the code. float headerHeight = (float) (coatOfArms.getHeight(this)) / 2; float coatOfArmsWidth = (float) (coatOfArms.getWidth(this)) / 2; float photoWidth = (float) (photo.getWidth(this)) / 2; float headerSpaceBetweenImages = imageableWidth - (coatOfArmsWidth + photoWidth + (SPACE_BETWEEN_ITEMS * 2)); logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("headerHeight", headerHeight) .append("coatOfArmsWidth", coatOfArmsWidth).append("photoWidth", photoWidth) .append("headerSpaceBetweenImages", headerSpaceBetweenImages).toString()); // get localised strings for card type. We'll take a new line every time a ";" is found in the resource String[] cardTypeStr = (bundle.getString("type_" + this.identity.getDocumentType().toString()) .toUpperCase()).split(";"); // if a "mention" is present, append it so it appears below the card type string, between brackets if (identity.getSpecialOrganisation() != SpecialOrganisation.UNSPECIFIED) { String mention = TextFormatHelper.getSpecialOrganisationString(bundle, identity.getSpecialOrganisation()); if (mention != null && !mention.isEmpty()) { String[] cardTypeWithMention = new String[cardTypeStr.length + 1]; System.arraycopy(cardTypeStr, 0, cardTypeWithMention, 0, cardTypeStr.length); cardTypeWithMention[cardTypeStr.length] = "(" + mention + ")"; cardTypeStr = cardTypeWithMention; } } // iterate from MAXIMAL_FONT_SIZE, calculating how much space would be required to fit the card type strings // stop when a font size is found where they all fit the space between the graphics in an orderly manner boolean sizeFound = false; int fontSize; for (fontSize = TITLE_MAXIMAL_FONT_SIZE; (fontSize >= MINIMAL_FONT_SIZE) && (!sizeFound); fontSize--) // count down slowly until we find one that fits nicely { logger.log(Level.FINE, "fontSize=" + fontSize + " sizeFound=" + sizeFound); graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize)); sizeFound = (ImageUtilities.getTotalStringWidth(graphics2D, cardTypeStr) < headerSpaceBetweenImages) && (ImageUtilities.getTotalStringHeight(graphics2D, cardTypeStr) < headerHeight); } // unless with extremely small papers, a size should always have been found. // draw the card type strings, centered, between the images at the top of the page if (sizeFound) { graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize + 1)); float cardTypeHeight = cardTypeStr.length * ImageUtilities.getStringHeight(graphics2D); float cardTypeBaseLine = ((headerHeight - cardTypeHeight) / 2) + ImageUtilities.getAscent(graphics2D); float cardTypeLineHeight = ImageUtilities.getStringHeight(graphics2D); logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("cardTypeHeight", cardTypeHeight).append("cardTypeBaseLine", cardTypeBaseLine) .append("cardTypeLineHeight", cardTypeLineHeight).toString()); for (int i = 0; i < cardTypeStr.length; i++) { float left = (coatOfArmsWidth + SPACE_BETWEEN_ITEMS + (headerSpaceBetweenImages - ImageUtilities.getStringWidth(graphics2D, cardTypeStr[i])) / 2); float leading = (float) cardTypeLineHeight * i; graphics2D.drawString(cardTypeStr[i], left, cardTypeBaseLine + leading); } } // populate idAttributes with all the information from identity and address // as well as date printed and some separators List<IdentityAttribute> idAttributes = populateAttributeList(); // draw a horizontal line just below the header (images + card type titles) graphics2D.drawLine(0, (int) headerHeight, (int) imageableWidth, (int) headerHeight); // calculate how much space is left between the header and the bottom of the imageable area headerHeight += 32; // take some distance from header float imageableDataHeight = imageableHeight - headerHeight; float totalDataWidth = 0, totalDataHeight = 0; float labelWidth, widestLabelWidth = 0; float valueWidth, widestValueWidth = 0; // iterate from MAXIMAL_FONT_SIZE, calculating how much space would be required to fit the information in idAttributes into // the space between the header and the bottom of the imageable area // stop when a font size is found where it all fits in an orderly manner sizeFound = false; for (fontSize = MAXIMAL_FONT_SIZE; (fontSize >= MINIMAL_FONT_SIZE) && (!sizeFound); fontSize--) // count down slowly until we find one that fits nicely { logger.log(Level.FINE, "fontSize=" + fontSize + " sizeFound=" + sizeFound); graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize)); widestLabelWidth = 0; widestValueWidth = 0; for (IdentityAttribute attribute : idAttributes) { if (attribute == SEPARATOR) continue; labelWidth = ImageUtilities.getStringWidth(graphics2D, attribute.getLabel()); valueWidth = ImageUtilities.getStringWidth(graphics2D, attribute.getValue()); if (labelWidth > widestLabelWidth) widestLabelWidth = labelWidth; if (valueWidth > widestValueWidth) widestValueWidth = valueWidth; } totalDataWidth = widestLabelWidth + SPACE_BETWEEN_ITEMS + widestValueWidth; totalDataHeight = ImageUtilities.getStringHeight(graphics2D) + (ImageUtilities.getStringHeight(graphics2D) * idAttributes.size()); if ((totalDataWidth < imageableWidth) && (totalDataHeight < imageableDataHeight)) sizeFound = true; } logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("widestLabelWidth", widestLabelWidth).append("widestValueWidth", widestValueWidth) .append("totalDataWidth", totalDataWidth).append("totalDataHeight", totalDataHeight).toString()); // unless with extremely small papers, a size should always have been found. // draw the identity, addess and date printed information, in 2 columns, centered inside the // space between the header and the bottom of the imageable area if (sizeFound) { graphics2D.setFont(new Font(FONT, Font.PLAIN, fontSize)); float labelsLeft = (imageableWidth - totalDataWidth) / 2; float valuesLeft = labelsLeft + widestLabelWidth + SPACE_BETWEEN_ITEMS; float dataLineHeight = ImageUtilities.getStringHeight(graphics2D); float dataTop = dataLineHeight + headerHeight + ((imageableDataHeight - totalDataHeight) / 2); float lineNumber = 0; logger.finest(new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("labelsLeft", labelsLeft) .append("valuesLeft", valuesLeft).append("dataLineHeight", dataLineHeight) .append("dataTop", dataTop).toString()); for (IdentityAttribute attribute : idAttributes) { if (attribute != SEPARATOR) // data { graphics2D.setColor(attribute.isRelevant() ? Color.BLACK : Color.LIGHT_GRAY); graphics2D.drawString(attribute.getLabel(), labelsLeft, dataTop + (lineNumber * dataLineHeight)); graphics2D.drawString(attribute.getValue(), valuesLeft, dataTop + (lineNumber * dataLineHeight)); } else // separator { int y = (int) (((dataTop + (lineNumber * dataLineHeight) + (dataLineHeight / 2))) - ImageUtilities.getAscent(graphics2D)); graphics2D.setColor(Color.BLACK); graphics2D.drawLine((int) labelsLeft, y, (int) (labelsLeft + totalDataWidth), y); } lineNumber++; } } // tell Java printing that all this makes for a page worth printing :-) return Printable.PAGE_EXISTS; }
From source file:org.gumtree.vis.awt.CompositePanel.java
@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { if (pageIndex != 0) { return NO_SUCH_PAGE; }//from w w w.j a v a2 s.c o m Graphics2D g2 = (Graphics2D) g; double x = pf.getImageableX(); double y = pf.getImageableY(); double w = pf.getImageableWidth(); double h = pf.getImageableHeight(); double screenWidth = getWidth(); double screenHeight = getHeight(); double widthRatio = w / screenWidth; double heightRatio = h / screenHeight; double overallRatio = 1; overallRatio = widthRatio < heightRatio ? widthRatio : heightRatio; // Rectangle2D printArea = new Rectangle2D.Double(x, y, screenWidth * overallRatio, // screenHeight * overallRatio); BufferedImage image = getImage(); g2.drawImage(image, (int) x, (int) y, (int) (screenWidth * overallRatio), (int) (screenHeight * overallRatio), null); // draw(g2, printArea, x, y); g2.dispose(); return PAGE_EXISTS; // XYPlot plot = (XYPlot) getChart().getPlot(); // Font domainFont = plot.getDomainAxis().getLabelFont(); // int domainSize = domainFont.getSize(); // Font rangeFont = plot.getRangeAxis().getLabelFont(); // int rangeSize = rangeFont.getSize(); // Font titleFont = getChart().getTitle().getFont(); // int titleSize = titleFont.getSize(); // Font domainScaleFont = plot.getDomainAxis().getTickLabelFont(); // int domainScaleSize = domainScaleFont.getSize(); // Font rangeScaleFont = plot.getRangeAxis().getTickLabelFont(); // int rangeScaleSize = rangeScaleFont.getSize(); // plot.getDomainAxis().setLabelFont(domainFont.deriveFont( // (float) (domainSize * overallRatio))); // plot.getRangeAxis().setLabelFont(rangeFont.deriveFont( // (float) (rangeSize * overallRatio))); // getChart().getTitle().setFont(titleFont.deriveFont( // (float) (titleSize * overallRatio))); // plot.getDomainAxis().setTickLabelFont(domainScaleFont.deriveFont( // (float) (domainScaleSize * overallRatio))); // plot.getRangeAxis().setTickLabelFont(rangeScaleFont.deriveFont( // (float) (rangeScaleSize * overallRatio))); // // Rectangle2D chartArea = (Rectangle2D) printArea.clone(); // getChart().getPadding().trim(chartArea); // AxisUtilities.trimTitle(chartArea, g2, getChart().getTitle(), getChart().getTitle().getPosition()); // // Axis scaleAxis = null; // Font scaleAxisFont = null; // int scaleAxisFontSize = 0; // for (Object object : getChart().getSubtitles()) { // Title title = (Title) object; // if (title instanceof PaintScaleLegend) { // scaleAxis = ((PaintScaleLegend) title).getAxis(); // scaleAxisFont = scaleAxis.getTickLabelFont(); // scaleAxisFontSize = scaleAxisFont.getSize(); // scaleAxis.setTickLabelFont(scaleAxisFont.deriveFont( // (float) (scaleAxisFontSize * overallRatio))); // } // AxisUtilities.trimTitle(chartArea, g2, title, title.getPosition()); // } // AxisSpace axisSpace = AxisUtilities.calculateAxisSpace( // getChart().getXYPlot(), g2, chartArea); // Rectangle2D dataArea = axisSpace.shrink(chartArea, null); // getChart().getXYPlot().getInsets().trim(dataArea); // getChart().getXYPlot().getAxisOffset().trim(dataArea); // //// Rectangle2D screenArea = getScreenDataArea(); //// Rectangle2D visibleArea = getVisibleRect(); //// Rectangle2D printScreenArea = new Rectangle2D.Double(screenArea.getMinX() * overallRatio + x, //// screenArea.getMinY() * overallRatio + y, //// printArea.getWidth() - visibleArea.getWidth() + screenArea.getWidth(), //// printArea.getHeight() - visibleArea.getHeight() + screenArea.getHeight()); // // getChart().draw(g2, printArea, getAnchor(), null); // ChartMaskingUtilities.drawMasks(g2, dataArea, // getMasks(), null, getChart(), overallRatio); // plot.getDomainAxis().setLabelFont(domainFont); // plot.getRangeAxis().setLabelFont(rangeFont); // getChart().getTitle().setFont(titleFont); // plot.getDomainAxis().setTickLabelFont(domainScaleFont); // plot.getRangeAxis().setTickLabelFont(rangeScaleFont); // if (scaleAxis != null) { // scaleAxis.setTickLabelFont(scaleAxisFont); // } // return PAGE_EXISTS; }