public void exportGraphic(Picture picture, OutputStream out) throws IOException { int width = picture.getPictureWidth(); int height = picture.getPictureHeight(); Document doc = new Document(new com.lowagie.text.Rectangle(width, height)); try { PdfWriter pWriter = PdfWriter.getInstance(doc, out); doc.open(); Graphics2D g = pWriter.getDirectContent().createGraphics(width, height); picture.paintPicture(g); g.dispose(); doc.close(); } catch (DocumentException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } }
public void exportGraphic(Picture picture, OutputStream out) throws IOException { if (!isSupported_) { throw new IOException("Graphics export to " + formatName_ + " not supported"); } /* Create an image buffer on which to paint. */ int w = picture.getPictureWidth(); int h = picture.getPictureHeight(); int imageType = transparentBg_ ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; BufferedImage image = new BufferedImage(w, h, imageType); Graphics2D g2 = image.createGraphics(); /* Clear the background. Failing to do this can leave junk. */ Color color = g2.getColor(); Composite compos = g2.getComposite(); if (transparentBg_) { /* Attempt to clear to transparent white, but this doesn't * seem to work well, at least for PNG (looks like * transparent black). */ g2.setComposite(AlphaComposite.Src); g2.setColor(new Color(1f, 1f, 1f, 0f)); } else { g2.setColor(Color.WHITE); } g2.fillRect(0, 0, w, h); g2.setColor(color); g2.setComposite(compos); /* Paint the graphics to the buffer. */ picture.paintPicture(g2); /* Export. */ boolean done = ImageIO.write(image, formatName_, out); out.flush(); g2.dispose(); if (!done) { throw new IOException( "No handler for format " + formatName_ + " (surprising - thought there was)"); } }
public void exportGraphic(Picture picture, OutputStream out) throws IOException { /* Scale to a pixel size which makes the bounding box sit * sensibly on an A4 or letter page. EpsGraphics2D default * scale is 72dpi. */ int width = picture.getPictureWidth(); int height = picture.getPictureHeight(); double padfrac = 0.05; double xdpi = width / 6.0; double ydpi = height / 9.0; double scale; int pad; if (xdpi > ydpi) { scale = 72.0 / xdpi; pad = (int) Math.ceil(width * padfrac * scale); } else { scale = 72.0 / ydpi; pad = (int) Math.ceil(height * padfrac * scale); } int xlo = -pad; int ylo = -pad; int xhi = (int) Math.ceil(scale * width) + pad; int yhi = (int) Math.ceil(scale * height) + pad; /* Construct a graphics object which will write postscript * down this stream. */ EpsGraphics2D g2 = new FixedEpsGraphics2D("Plot", out, xlo, ylo, xhi, yhi); g2.scale(scale, scale); /* Do the drawing. */ picture.paintPicture(g2); /* Note this close call *must* be made, otherwise the * eps file is not flushed or correctly terminated. * This closes the output stream too. */ g2.close(); }
public void exportGraphic(Picture picture, OutputStream out) throws IOException { /* Get component dimensions. */ int w = picture.getPictureWidth(); int h = picture.getPictureHeight(); /* Create a BufferedImage to draw it onto. */ BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); /* Clear the background. */ Graphics2D g = image.createGraphics(); Color color = g.getColor(); g.setColor(Color.WHITE); g.fillRect(0, 0, w, h); g.setColor(color); /* Draw the component onto the image. */ picture.paintPicture(g); g.dispose(); /* Count the number of colours represented in the resulting * image. */ Set colors = new HashSet(); for (int ix = 0; ix < w; ix++) { for (int iy = 0; iy < h; iy++) { colors.add(new Integer(image.getRGB(ix, iy))); } } /* If there are too many, redraw the image into an indexed image * instead. This is necessary since the GIF encoder we're using * here just gives up if there are too many. */ if (colors.size() > 254) { logger_.warning( "GIF export colour map filled up - " + "JPEG or PNG might do a better job"); /* Create an image with a suitable colour model. */ IndexColorModel gifColorModel = getGifColorModel(); image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, gifColorModel); /* Zero all pixels to the transparent colour. */ WritableRaster raster = image.getRaster(); int itrans = gifColorModel.getTransparentPixel(); if (itrans >= 0) { byte[] pixValue = new byte[] {(byte) itrans}; for (int ix = 0; ix < w; ix++) { for (int iy = 0; iy < h; iy++) { raster.setDataElements(ix, iy, pixValue); } } } /* Draw the component on it. */ Graphics2D gifG = image.createGraphics(); /* Set dithering false. But it still seems to dither on a * drawImage! Can't get to the bottom of it. */ gifG.setRenderingHint( RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE); picture.paintPicture(gifG); gifG.dispose(); } /* Write the image as a gif down the provided stream. */ new GifEncoder(image, out).encode(); }