List of usage examples for java.awt Graphics2D scale
public abstract void scale(double sx, double sy);
From source file:com.alvermont.terraj.util.io.ImagePrinter.java
/** * Render an image for printing.//www . j a v a 2 s . co m * * @param graphics The graphics context to print on * @param pageFormat The page format being used * @param pageIndex The page being printed * @throws java.awt.print.PrinterException If there is an error in printing * @return An indication of the result of page rendering for this page */ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { final Graphics2D g2 = (Graphics2D) graphics; // we only expect to be printing 1 page as the image is scaled if (pageIndex >= 1) { return Printable.NO_SUCH_PAGE; } // translate the coordinate system to match up the image with // the printable area g2.translate(getFormat().getImageableX(), pageFormat.getImageableY()); final Rectangle componentBounds = new Rectangle(getImage().getWidth(), getImage().getHeight()); g2.translate(-componentBounds.x, -componentBounds.y); // scale the image to fit scaleToFit(true); g2.scale(getScaleX(), getScaleY()); // render the image g2.drawImage(getImage(), null, 0, 0); // done 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 w ww . j av a 2s . com*/ 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:BookTest.java
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; PageFormat pageFormat = book.getPageFormat(currentPage); double xoff; // x offset of page start in window double yoff; // y offset of page start in window double scale; // scale factor to fit page in window double px = pageFormat.getWidth(); double py = pageFormat.getHeight(); double sx = getWidth() - 1; double sy = getHeight() - 1; if (px / py < sx / sy) // center horizontally {/*ww w .jav a 2 s. co m*/ scale = sy / py; xoff = 0.5 * (sx - scale * px); yoff = 0; } else // center vertically { scale = sx / px; xoff = 0; yoff = 0.5 * (sy - scale * py); } g2.translate((float) xoff, (float) yoff); g2.scale((float) scale, (float) scale); // draw page outline (ignoring margins) Rectangle2D page = new Rectangle2D.Double(0, 0, px, py); g2.setPaint(Color.white); g2.fill(page); g2.setPaint(Color.black); g2.draw(page); Printable printable = book.getPrintable(currentPage); try { printable.print(g2, pageFormat, currentPage); } catch (PrinterException e) { g2.draw(new Line2D.Double(0, 0, px, py)); g2.draw(new Line2D.Double(px, 0, 0, py)); } }
From source file:com.alvermont.terraj.util.io.PrintUtilities.java
/** * Print the component into a graphics context * //from ww w . j a v a 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:com.epiq.bitshark.ui.FrequencyDomainMouseMarker.java
/** * Draws the annotation.//w ww . j ava2 s .c o m * * @param g2 the graphics device. * @param plot the plot. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param rendererIndex the renderer index. * @param info if supplied, this info object will be populated with * entity information. */ @Override public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) { if (!visible) { return; } PlotOrientation orientation = plot.getOrientation(); RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation); RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation); float j2DX = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge); float j2DY = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge); Rectangle2D displayArea = new Rectangle2D.Double(j2DX - this.displayWidth / 2.0, j2DY - this.displayHeight / 2.0, this.displayWidth, this.displayHeight); // here we change the AffineTransform so we can draw the annotation // to a larger area and scale it down into the display area // afterwards, the original transform is restored AffineTransform savedTransform = g2.getTransform(); Rectangle2D drawArea = new Rectangle2D.Double(0.0, 0.0, this.displayWidth * this.drawScaleFactor, this.displayHeight * this.drawScaleFactor); g2.scale(1 / this.drawScaleFactor, 1 / this.drawScaleFactor); g2.translate((j2DX - this.displayWidth / 2.0) * this.drawScaleFactor, (j2DY - this.displayHeight / 2.0) * this.drawScaleFactor); draw(g2, drawArea); g2.setTransform(savedTransform); String toolTip = getToolTipText(); String url = getURL(); if (toolTip != null || url != null) { addEntity(info, displayArea, rendererIndex, toolTip, url); } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
public byte[] printImage(float scale, CanvasCommon canvas, Dimension graphsize, String format) throws IOException { int h_margin = 72, v_margin = 72; BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); if (scale > 0) g2.scale(scale, scale); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, image.getWidth(), image.getHeight()); // canvas.paint(g2); Color bgsave = canvas.getBackground(); boolean edsave = canvas.editable; canvas.editable = false;// ww w .j av a 2s . co m canvas.setBackground(Color.white); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); canvas.paintComponent(g2); canvas.setBackground(bgsave); canvas.editable = edsave; g2.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, format, baos); image = null; Runtime r = Runtime.getRuntime(); r.gc(); return baos.toByteArray(); }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
public void printImage(String filename, float scale, CanvasCommon canvas, Dimension graphsize) { try {/* w w w .ja v a 2 s. c om*/ int h_margin = 72, v_margin = 72; BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); if (scale > 0) g2.scale(scale, scale); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, image.getWidth(), image.getHeight()); // canvas.paint(g2); Color bgsave = canvas.getBackground(); canvas.setBackground(Color.white); canvas.paintComponent(g2); canvas.setBackground(bgsave); g2.dispose(); ImageIO.write(image, "jpeg", new File(filename)); image = null; Runtime r = Runtime.getRuntime(); r.gc(); } catch (IOException e) { System.err.println(e); } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
private void printGraphPdf(DocWriter writer, CanvasCommon canvas, Graph process, Rectangle page_size, String type, String filename, Chapter chapter, int chapter_number) throws Exception { Dimension graphsize = process.getGraphSize(); // we create a fontMapper and read all the fonts in the font directory DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories();// w ww. j a v a 2 s. co m mapper.insertDirectory("c:\\winnt\\fonts"); // mapper.insertDirectory("c:\\windows\\fonts"); // we create a template and a Graphics2D object that corresponds with it int w, h; float scale; if ((float) graphsize.width < page_size.getWidth() * 0.8 && (float) graphsize.height < page_size.getHeight() * 0.8 || type.equals(HTML)) { w = graphsize.width + 36; h = graphsize.height + 36; scale = -1f; } else { scale = page_size.getWidth() * 0.8f / (float) graphsize.width; if (scale > page_size.getHeight() * 0.8f / (float) graphsize.height) scale = page_size.getHeight() * 0.8f / (float) graphsize.height; w = (int) (graphsize.width * scale) + 36; h = (int) (graphsize.height * scale) + 36; } Image img; int zoomSave = process.zoom; process.zoom = 100; Color bgsave = canvas.getBackground(); boolean edsave = canvas.editable; canvas.editable = false; canvas.setBackground(Color.white); if (type.equals(PDF)) { PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createGraphics(w, h, mapper); if (scale > 0) g2.scale(scale, scale); tp.setWidth(w); tp.setHeight(h); canvas.paintComponent(g2); g2.dispose(); // cb.addTemplate(tp, 50, 400); img = new ImgTemplate(tp); } else { String imgfilename = filename + "." + process.getName() + "_ch" + chapter_number + ".jpg"; printImage(imgfilename, -1f, canvas, graphsize); img = Image.getInstance(imgfilename); if (scale > 0) img.scalePercent(scale * 100); } process.zoom = zoomSave; canvas.setBackground(bgsave); canvas.editable = edsave; if (img != null) chapter.add(img); }
From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO_SUCH_PAGE; } else {//from www . j a v a 2 s.c o m Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.white); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); Dimension td = this.getPreferredSize(); double sx = pageFormat.getImageableWidth() / td.width; double sy = pageFormat.getImageableHeight() / td.height; double s = Math.min(sx, sy); if (s < 1.) g2d.scale(s, s); RepaintManager.currentManager(this).setDoubleBufferingEnabled(false); this.paint(g2d); RepaintManager.currentManager(this).setDoubleBufferingEnabled(true); return PAGE_EXISTS; } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
public void printImagePdf(String filename, DesignerCanvas canvas, Dimension graphsize) { try {// www. j av a 2 s . c om DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); mapper.insertDirectory("c:\\winnt\\fonts"); // mapper.insertDirectory("c:\\windows\\fonts"); // we create a template and a Graphics2D object that corresponds // with it int margin = 72; // 1 inch float scale = 0.5f; boolean multiple_page = true; Rectangle page_size; if (multiple_page) { page_size = PageSize.LETTER.rotate(); } else { page_size = new Rectangle((int) (graphsize.getWidth() * scale) + margin, (int) (graphsize.getHeight() * scale) + margin); } Document document = new Document(page_size); DocWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); document.setPageSize(page_size); int image_w = (int) page_size.getWidth() - margin; int image_h = (int) page_size.getHeight() - margin; boolean edsave = canvas.editable; canvas.editable = false; Color bgsave = canvas.getBackground(); canvas.setBackground(Color.white); if (multiple_page) { int horizontal_pages = (int) (graphsize.width * scale) / image_w + 1; int vertical_pages = (int) (graphsize.height * scale) / image_h + 1; for (int i = 0; i < horizontal_pages; i++) { for (int j = 0; j < vertical_pages; j++) { Image img; PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(image_w, image_h); Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper); tp.setWidth(image_w); tp.setHeight(image_h); g2.scale(scale, scale); g2.translate(-i * image_w / scale, -j * image_h / scale); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); document.add(img); } } } else { Image img; PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(image_w, image_h); Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper); tp.setWidth(image_w); tp.setHeight(image_h); g2.scale(scale, scale); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); document.add(img); } canvas.setBackground(bgsave); canvas.editable = edsave; document.close(); } catch (Exception e) { e.printStackTrace(); } }