List of usage examples for java.awt Graphics2D dispose
public abstract void dispose();
From source file:Main.java
/** * Creates and returns image from the given text. * @param text input text/*from ww w. j a va 2 s . c om*/ * @param font text font * @return image with input text */ public static BufferedImage createImageFromText(String text, Font font) { //You may want to change these setting, or make them parameters boolean isAntiAliased = true; boolean usesFractionalMetrics = false; FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics); TextLayout layout = new TextLayout(text, font, frc); Rectangle2D bounds = layout.getBounds(); int w = (int) Math.ceil(bounds.getWidth()); int h = (int) Math.ceil(bounds.getHeight()) + 2; BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example; Graphics2D g = image.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, w, h); g.setColor(Color.BLACK); g.setFont(font); Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF; g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased); Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF; g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics); g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY()); g.dispose(); return image; }
From source file:net.rptools.lib.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) return;//from w ww.ja v a2 s.c om Graphics2D g = null; try { g = (Graphics2D) image.getGraphics(); Composite oldComposite = g.getComposite(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setComposite(oldComposite); } finally { if (g != null) { g.dispose(); } } }
From source file:com.t3.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) { return;// w w w . jav a 2 s .c o m } Graphics2D g = null; try { g = (Graphics2D) image.getGraphics(); Composite oldComposite = g.getComposite(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setComposite(oldComposite); } finally { if (g != null) { g.dispose(); } } }
From source file:edu.stanford.muse.webapp.HTMLToImage.java
/** * Convert an HTML page at the specified URL into an image who's data is * written to the provided output stream. * * @param url URL to the page that is to be imaged. * @param os An output stream that is to be opened for writing. Image * data will be written to the provided stream. The stream * will not be closed under any circumstances by this method. * @param width The desired width of the image that will be created. * @param height The desired height of the image that will be created. * * @returns true if the page at the provided URL was loaded, converted to an * image, and the image data has been written to the output stream, * false if an error has ocurred along the way. * * @throws HTMLImagerException if an error has ocurred. *//* w ww . j a v a 2 s. c om*/ public static boolean image(String url, OutputStream os, int width, int height) { if (log.isDebugEnabled()) log.debug("Imaging url '" + url + "'."); boolean successful = false; try { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); httpClient.executeMethod(getMethod); int httpStatus = getMethod.getStatusCode(); if (httpStatus == HttpServletResponse.SC_OK) { Tidy tidy = new Tidy(); tidy.setQuiet(true); tidy.setXHTML(true); tidy.setHideComments(true); tidy.setInputEncoding("UTF-8"); tidy.setOutputEncoding("UTF-8"); tidy.setShowErrors(0); tidy.setShowWarnings(false); Document doc = tidy.parseDOM(getMethod.getResponseBodyAsStream(), null); if (doc != null) { BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = (Graphics2D) buf.getGraphics(); Graphics2DRenderer renderer = new Graphics2DRenderer(); SharedContext context = renderer.getSharedContext(); UserAgentCallback userAgent = new HTMLImagerUserAgent(url); context.setUserAgentCallback(userAgent); context.setNamespaceHandler(new XhtmlNamespaceHandler()); renderer.setDocument(doc, url); renderer.layout(graphics, new Dimension(width, height)); renderer.render(graphics); graphics.dispose(); /* JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam( buf ); param.setQuality( (float)1.0, false ); JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder( os, param ); imageEncoder.encode( buf ); */ successful = true; } else { if (log.isDebugEnabled()) log.debug("Unable to image URL '" + url + "'. The HTML that was returned could not be tidied."); } } else { if (log.isDebugEnabled()) log.debug("Unable to image URL '" + url + "'. Server returned status code '" + httpStatus + "'."); } } catch (Exception e) { throw new RuntimeException("Unable to image URL '" + url + "'.", e); } return successful; }
From source file:com.android.hierarchyviewerlib.device.DeviceBridge.java
private static boolean readLayer(DataInputStream in, PsdFile psd) { try {/*w w w . ja v a 2s.c om*/ if (in.read() == 2) { return false; } String name = in.readUTF(); boolean visible = in.read() == 1; int x = in.readInt(); int y = in.readInt(); int dataSize = in.readInt(); byte[] data = new byte[dataSize]; int read = 0; while (read < dataSize) { read += in.read(data, read, dataSize - read); } ByteArrayInputStream arrayIn = new ByteArrayInputStream(data); BufferedImage chunk = ImageIO.read(arrayIn); // Ensure the image is in the right format BufferedImage image = new BufferedImage(chunk.getWidth(), chunk.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.drawImage(chunk, null, 0, 0); g.dispose(); psd.addLayer(name, image, new Point(x, y), visible); return true; } catch (Exception e) { return false; } }
From source file:com.piaoyou.util.ImageUtil.java
/** * This method write the image to a stream. * It auto detect the image is Image or BufferedImage. * This method close the output stream before it return. * * @param image Image/*from ww w .ja va2 s .co m*/ * @param outputStream OutputStream * @throws IOException */ public static void writeJpegImage_Sun(Image image, OutputStream outputStream) throws IOException { if (outputStream == null) { return; } try { BufferedImage bufferedImage = null; if (image instanceof BufferedImage) { bufferedImage = (BufferedImage) image; } else { // 30% cpu resource bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); // 7.5 cpu Graphics2D g = bufferedImage.createGraphics(); // 50% cpu g.drawImage(image, 0, 0, null); g.dispose(); // free resource } // write it to disk // 12% cpu //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream); //encoder.encode(bufferedImage); ImageIO.write(bufferedImage, "jpeg", outputStream); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
From source file:canreg.client.analysis.Tools.java
public static void exportChartAsPDF(JFreeChart chart, Rectangle bounds, File file) throws IOException, DocumentException { System.out.println(file.getPath()); PdfWriter writer = null;/*w w w . j av a 2s.com*/ com.itextpdf.text.Document document = new com.itextpdf.text.Document(); document.addCreator("CanReg5"); document.addCreationDate(); writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(bounds.width, bounds.height); Graphics2D graphics2d = template.createGraphics(bounds.width, bounds.height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, bounds.width, bounds.height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); document.close(); }
From source file:Main.java
/** * Draws a double headed arrow with arrow heads of a given width and height. * //from www . ja v a 2 s .c om * @param aG * the canvas to draw on; * @param aX1 * the starting X position of the arrow; * @param aY1 * the starting Y position of the arrow; * @param aX2 * the ending X position of the arrow; * @param aY2 * the ending Y position of the arrow; * @param aArrowWidth * the total width of the arrow head; * @param aArrowHeight * the total height of the arrow head. */ public static final void drawDoubleHeadedArrow(final Graphics aG, final int aX1, final int aY1, final int aX2, final int aY2, final int aArrowWidth, final int aArrowHeight) { final Graphics2D g2d = (Graphics2D) aG.create(); final int lineWidth = Math.abs(aX2 - aX1); final int threshold = (2 * aArrowWidth) + 2; try { int x1 = aX1; int x2 = aX2; if (lineWidth > threshold) { drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight); // why x2 needs to be shifted by one pixel is beyond me... drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight); x1 += aArrowWidth - 1; x2 -= aArrowWidth + 1; } g2d.drawLine(x1, aY1, x2, aY2); } finally { g2d.dispose(); } }
From source file:com.uwsoft.editor.proxy.ResolutionManager.java
public static BufferedImage imageResize(File file, float ratio) { BufferedImage destinationBufferedImage = null; try {// w ww . j a v a 2 s .c o m BufferedImage sourceBufferedImage = ImageIO.read(file); if (ratio == 1.0) { return sourceBufferedImage; } // When image has to be resized smaller then 3 pixels we should leave it as is, as to ResampleOP limitations // But it should also trigger a warning dialog at the and of the import, to notify the user of non resized images. if (sourceBufferedImage.getWidth() * ratio < 3 || sourceBufferedImage.getHeight() * ratio < 3) { return null; } int newWidth = Math.max(3, Math.round(sourceBufferedImage.getWidth() * ratio)); int newHeight = Math.max(3, Math.round(sourceBufferedImage.getHeight() * ratio)); String name = file.getName(); Integer[] patches = null; if (name.endsWith(EXTENSION_9PATCH)) { patches = NinePatchUtils.findPatches(sourceBufferedImage); sourceBufferedImage = NinePatchUtils.removePatches(sourceBufferedImage); newWidth = Math.round(sourceBufferedImage.getWidth() * ratio); newHeight = Math.round(sourceBufferedImage.getHeight() * ratio); System.out.println(sourceBufferedImage.getWidth()); destinationBufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = destinationBufferedImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); g2.drawImage(sourceBufferedImage, 0, 0, newWidth, newHeight, null); g2.dispose(); } else { // resize with bilinear filter ResampleOp resampleOp = new ResampleOp(newWidth, newHeight); destinationBufferedImage = resampleOp.filter(sourceBufferedImage, null); } if (patches != null) { destinationBufferedImage = NinePatchUtils.convertTo9Patch(destinationBufferedImage, patches, ratio); } } catch (IOException ignored) { } return destinationBufferedImage; }
From source file:edu.ku.brc.specify.plugins.ipadexporter.InstitutionConfigDlg.java
public static BufferedImage toBufferedImage(final Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; }//from ww w .j a v a 2 s . c o m // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Draw the image on to the buffered image Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Return the buffered image return bimage; }