List of usage examples for java.awt Graphics2D dispose
public abstract void dispose();
From source file:game.com.HandleUploadGameThumbServlet.java
public static BufferedImage resizeImage(final Image image, int width, int height) { final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setComposite(AlphaComposite.Src); //below three lines are for RenderingHints for better image quality at cost of higher processing time graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.drawImage(image, 0, 0, width, height, null); graphics2D.dispose(); return bufferedImage; }
From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java
/** * Scales an image to the desired dimensions. * * @param img Original image//from ww w . ja v a2s . co m * @param newW Target width * @param newH Target height * @return Scaled image */ public static BufferedImage scale(BufferedImage img, int newW, int newH) { int w = img.getWidth(); int h = img.getHeight(); final BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); final Graphics2D g = dimg.createGraphics(); // use provided rendering hint, default is bilinear switch (scalingHint) { case "n": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); break; case "b": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); break; } g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); g.dispose(); return dimg; }
From source file:com.silverpeas.thumbnail.control.ThumbnailController.java
protected static void createCropThumbnailFileOnServer(String pathOriginalFile, String pathCropdir, String pathCropFile, ThumbnailDetail thumbnail, int thumbnailWidth, int thumbnailHeight) { try {// w ww . ja va 2s . c o m // Creates folder if not exists File dir = new File(pathCropdir); if (!dir.exists()) { FileFolderManager.createFolder(pathCropdir); } // create empty file File cropFile = new File(pathCropFile); if (!cropFile.exists()) { cropFile.createNewFile(); } File originalFile = new File(pathOriginalFile); BufferedImage bufferOriginal = ImageIO.read(originalFile); // crop image BufferedImage cropPicture = bufferOriginal.getSubimage(thumbnail.getXStart(), thumbnail.getYStart(), thumbnail.getXLength(), thumbnail.getYLength()); BufferedImage cropPictureFinal = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB); // Redimensionnement de l'image Graphics2D g2 = cropPictureFinal.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(cropPicture, 0, 0, thumbnailWidth, thumbnailHeight, null); g2.dispose(); // save crop image String extension = FilenameUtils.getExtension(originalFile.getName()); ImageIO.write(cropPictureFinal, extension, cropFile); } catch (Exception e) { SilverTrace.warn("thumbnail", "ThumbnailController.createThumbnailFileOnServer()", "thumbnail_MSG_CREATE_CROP_FILE_KO", "originalFileName=" + thumbnail.getOriginalFileName() + " cropFileName = " + thumbnail.getCropFileName(), e); } }
From source file:Main.java
/** * Paints a set of {@link Rectangle} object out of a rendered {@link BufferedImage} * such that the resulting image is transparent except for a minimum bounding * rectangle of the selected elements./*from ww w. jav a 2s .c om*/ * * @param image the source image * @param rectangles the set of rectangles to copy * @param boundingBox the bounding rectangle of the set of rectangles to copy, can be * computed by {@link ImageUtils#getBoundingRectangle} * @param scale a scale factor to apply to the result, e.g. 0.5 to shrink the * destination down 50%, 1.0 to leave it alone and 2.0 to zoom in by * doubling the image size * @return a rendered image, or null */ public static BufferedImage drawRectangles(BufferedImage image, java.util.List<Rectangle> rectangles, Rectangle boundingBox, double scale) { // This code is not a test. When I implemented image cropping, I first implemented // it for BufferedImages (since it's easier; easy image painting, easy scaling, // easy transparency handling, etc). However, this meant that we would need to // convert the SWT images from the ImageOverlay to BufferedImages, crop and convert // back; not ideal, so I rewrote it in SWT (see SwtUtils). However, I // don't want to throw away the code in case we start keeping BufferedImages rather // than SWT images or need it for other purposes, but rather than place it in the // production codebase I'm leaving this utility here in the associated ImageUtils // test class. It was used like this: // @formatter:off // // BufferedImage wholeImage = SwtUtils.convertToAwt(image); // BufferedImage result = ImageUtils.cropSelection(wholeImage, // rectangles, boundingBox, scale); // e.image = SwtUtils.convertToSwt(image.getDevice(), result, true, // DRAG_TRANSPARENCY); // // @formatter:on if (boundingBox == null) { return null; } int destWidth = (int) (scale * boundingBox.width); int destHeight = (int) (scale * boundingBox.height); BufferedImage dest = new BufferedImage(destWidth, destHeight, image.getType()); Graphics2D g = dest.createGraphics(); for (Rectangle bounds : rectangles) { int dx1 = bounds.x - boundingBox.x; int dy1 = bounds.y - boundingBox.y; int dx2 = dx1 + bounds.width; int dy2 = dy1 + bounds.height; dx1 *= scale; dy1 *= scale; dx2 *= scale; dy2 *= scale; int sx1 = bounds.x; int sy1 = bounds.y; int sx2 = sx1 + bounds.width; int sy2 = sy1 + bounds.height; g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); } g.dispose(); return dest; }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
public static void updateImage(JLabel imageContainer, File imageFile) { if (!imageFile.exists()) { return;/* w w w .j ava 2 s . co m*/ } BufferedImage img = null; try { img = ImageIO.read(imageFile); } catch (IOException e) { e.printStackTrace(); } if (img == null) { return; } int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); int imageViewWidth = imageContainer.getWidth(); int imageViewHeight = imageContainer.getHeight(); double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), new Dimension(imageViewWidth, imageViewHeight)); factor = Math.min(factor, 1f); imageWidth = (int) (factor * imageWidth); imageHeight = (int) (factor * imageHeight); if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) { return; } BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); int x = (imageViewWidth - imageWidth) / 2; int y = (imageViewHeight - imageHeight) / 2; g2.drawImage(img, x, y, imageWidth, imageHeight, null); g2.dispose(); imageContainer.setIcon(new ImageIcon(tmp)); }
From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java
/** * Paints a chart with scaling options//from w ww .j av a2 s . c om * * @param chart * @param info * @param out * @param width * @param height * @param resolution * @return BufferedImage of a given chart with scaling to resolution * @throws IOException */ private static BufferedImage paintScaledChartToBufferedImage(JFreeChart chart, ChartRenderingInfo info, OutputStream out, int width, int height, int resolution, int bufferedIType) throws IOException { Args.nullNotPermitted(out, "out"); Args.nullNotPermitted(chart, "chart"); double scaleX = resolution / 72.0; double scaleY = resolution / 72.0; double desiredWidth = width * scaleX; double desiredHeight = height * scaleY; double defaultWidth = width; double defaultHeight = height; boolean scale = false; // get desired width and height from somewhere then... if ((scaleX != 1) || (scaleY != 1)) { scale = true; } BufferedImage image = new BufferedImage((int) desiredWidth, (int) desiredHeight, bufferedIType); Graphics2D g2 = image.createGraphics(); if (scale) { AffineTransform saved = g2.getTransform(); g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY)); chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info); g2.setTransform(saved); g2.dispose(); } else { chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info); } return image; }
From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java
/** * Writes a chart to an output stream in JPEG format. This method allows * you to pass in a {@link ChartRenderingInfo} object, to collect * information about the chart dimensions/entities. You will need this * info if you want to create an HTML image map. * * @param out the output stream (<code>null</code> not permitted). * @param chart the chart (<code>null</code> not permitted). * @param width the image width./* w w w .j ava 2 s. c om*/ * @param height the image height. * @param info the chart rendering info (<code>null</code> permitted). * @param shapeMap * * @throws IOException if there are any I/O errors. */ public static void writeChartAsJPEG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info, Rectangle2D imageArea, LinkedHashMap<AbstractMask, Color> maskList, LinkedHashMap<Shape, Color> shapeMap, LinkedHashMap<Rectangle2D, String> textContentMap) throws IOException { if (file == null) { throw new IllegalArgumentException("Null 'file' argument."); } if (chart == null) { throw new IllegalArgumentException("Null 'chart' argument."); } OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); BufferedImage image = chart.createBufferedImage(width, height, BufferedImage.TYPE_INT_RGB, info); Graphics2D g2 = image.createGraphics(); drawMasks(g2, imageArea, maskList, null, chart); drawShapes(g2, imageArea, shapeMap, chart); drawText(g2, imageArea, textContentMap, chart); g2.dispose(); try { EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out); } finally { out.close(); } }
From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java
/** * Writes a chart to an output stream in PNG format. This method allows * you to pass in a {@link ChartRenderingInfo} object, to collect * information about the chart dimensions/entities. You will need this * info if you want to create an HTML image map. * * @param out the output stream (<code>null</code> not permitted). * @param chart the chart (<code>null</code> not permitted). * @param width the image width./* ww w . java2 s . c o m*/ * @param height the image height. * @param info carries back chart rendering info (<code>null</code> * permitted). * @param shapeMap * @param encodeAlpha encode alpha? * @param compression the PNG compression level (0-9). * * @throws IOException if there are any I/O errors. */ public static void writeChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info, Rectangle2D imageArea, LinkedHashMap<AbstractMask, Color> maskList, LinkedHashMap<Shape, Color> shapeMap, LinkedHashMap<Rectangle2D, String> textContentMap) throws IOException { if (file == null) { throw new IllegalArgumentException("Null 'file' argument."); } if (chart == null) { throw new IllegalArgumentException("Null 'chart' argument."); } OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); BufferedImage chartImage = chart.createBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB, info); Graphics2D g2 = chartImage.createGraphics(); drawMasks(g2, imageArea, maskList, null, chart); drawShapes(g2, imageArea, shapeMap, chart); drawText(g2, imageArea, textContentMap, chart); g2.dispose(); try { ChartUtilities.writeBufferedImageAsPNG(out, chartImage); } finally { out.close(); } }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * @param bufImg//from w ww .j a va2 s . c o m * @param size * @return */ public static BufferedImage generateScaledImage(final BufferedImage bufImg, @SuppressWarnings("unused") final Object hintsArg, final int size) { BufferedImage sourceImage = bufImg; int srcWidth = sourceImage.getWidth(); int srcHeight = sourceImage.getHeight(); double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = size; double multiplier = longSideForDest / longSideForSource; int destWidth = (int) (srcWidth * multiplier); int destHeight = (int) (srcHeight * multiplier); BufferedImage destImage = null; destImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destImage.createGraphics(); graphics2D.drawImage(sourceImage, 0, 0, destWidth, destHeight, null); graphics2D.dispose(); return destImage; }
From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java
/** * //from w w w. j av a 2 s. c om * chart * * @param section chart section ? * @param e chart element * @throws Exception */ public static void setChart(PdfWriter writer, Section section, Element e) throws Exception { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Element e1 : e.getChildren()) { if (!e1.getChild("column").getText().equals(FileType.DIRECTORY.toString()) && !e1.getChild("column").getText().equals(FileType.SUM.toString())) { dataset.setValue(Integer.parseInt(e1.getChild("value").getText()), e.getAttributeValue("title"), e1.getChild("column").getText()); } } JFreeChart chart = ChartFactory.createBarChart3D(e.getAttributeValue("title"), "", "", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot plot = chart.getCategoryPlot(); java.awt.Font labelFont = chart.getCategoryPlot().getDomainAxis().getLabelFont(); plot.getDomainAxis().setLabelFont(new java.awt.Font(labelFont.getName(), Font.NORMAL, 6)); plot.getDomainAxis().setTickLabelFont(new java.awt.Font(labelFont.getName(), Font.NORMAL, 6)); PdfContentByte cb = writer.getDirectContent(); PdfTemplate bar = cb.createTemplate(500, 150); Graphics2D g2d2 = new PdfGraphics2D(bar, 500, 150); Rectangle2D r2d2 = new Rectangle2D.Double(0, 0, 500, 150); chart.draw(g2d2, r2d2); g2d2.dispose(); Image image = Image.getInstance(bar); image.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER); section.add(image); }