List of usage examples for java.awt Graphics2D dispose
public abstract void dispose();
From source file:Main.java
public static BufferedImage getTransparentImage(BufferedImage image, Color transparent) { BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = img.createGraphics(); for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { if (image.getRGB(x, y) != transparent.getRGB()) { img.setRGB(x, y, image.getRGB(x, y)); }/*from ww w . j a v a2s .c o m*/ } } g.dispose(); return img; }
From source file:Main.java
/** * Creates a shadow mask//from ww w . ja v a2 s. c o m * @param image * @param shadowColor * @param shadowOpacity * @return */ private static BufferedImage createShadowMask(BufferedImage image, Color shadowColor, float shadowOpacity) { BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = mask.createGraphics(); g2d.drawImage(image, 0, 0, null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity)); g2d.setColor(shadowColor); g2d.fillRect(0, 0, image.getWidth(), image.getHeight()); g2d.dispose(); return mask; }
From source file:jmbench.plots.UtilPlotPdf.java
public static void saveAsPdf(LegendTitle legend, String FILENAME, int width, int height) { Document document = new Document(new Rectangle(width, height)); try {/*from ww w.ja v a2 s . c o m*/ FileOutputStream file = new FileOutputStream(FILENAME); PdfWriter writer = PdfWriter.getInstance(document, file); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height); legend.draw(g2d, r2d); g2d.dispose(); cb.addTemplate(tp, 0, 0); document.close(); g2d.dispose(); } catch (Exception e) { e.printStackTrace(); } }
From source file:jmbench.plots.UtilPlotPdf.java
public static void saveAsPdf(JFreeChart chart, String FILENAME, int width, int height) { File parent = new File(new File(FILENAME).getParent()); if (!parent.exists()) { if (!parent.mkdirs()) throw new RuntimeException("Can't make directory path"); }// ww w. ja v a 2s.c om Document document = new Document(new Rectangle(width, height)); try { FileOutputStream file = new FileOutputStream(FILENAME); PdfWriter writer = PdfWriter.getInstance(document, file); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2d, r2d); g2d.dispose(); cb.addTemplate(tp, 0, 0); document.close(); g2d.dispose(); } catch (Exception e) { e.printStackTrace(); } }
From source file:kz.supershiny.core.services.ImageService.java
/** * Creates thumb for image./* ww w .j av a 2 s . c om*/ * * @param originalData original image in byte array * @param type original - 0, large - 1, small - 2 * @return resized image in byte array */ public static byte[] resizeImage(byte[] originalData, ImageSize type) { //if original flag, then return original if (type.equals(ImageSize.ORIGINAL)) { return originalData; } BufferedImage originalImage = null; BufferedImage resizedImage = null; byte[] result = null; //convert bytes to BufferedImage try (InputStream in = new ByteArrayInputStream(originalData)) { originalImage = ImageIO.read(in); } catch (IOException ex) { LOG.error("Cannot convert byte array to BufferedImage!", ex); return null; } //get original size int scaledHeight = originalImage.getHeight(); int scaledWidth = originalImage.getWidth(); switch (type) { case LARGE: scaledWidth = LARGE_WIDTH; scaledHeight = LARGE_HEIGHT; break; case SMALL: scaledWidth = SMALL_WIDTH; scaledHeight = SMALL_HEIGHT; break; default: break; } //calculate aspect ratio float ratio = 1.0F * originalImage.getWidth() / originalImage.getHeight(); if (ratio > 1.0F) { scaledHeight = (int) (scaledHeight / ratio); } else { scaledWidth = (int) (scaledWidth * ratio); } //resize with hints resizedImage = new BufferedImage(scaledWidth, scaledHeight, originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType()); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); g.dispose(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //convert BufferedImage to bytes try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(resizedImage, "png", baos); baos.flush(); result = baos.toByteArray(); } catch (IOException ex) { LOG.error("Cannot convert BufferedImage to byte array!", ex); } return result; }
From source file:ImageUtil.java
public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException { int x = src.getWidth() / 2 - width / 2; int y = src.getHeight() / 2 - height / 2; // System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y); BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType()); Graphics2D area = (Graphics2D) clipping.getGraphics().create(); area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(), y + clipping.getHeight(), null); area.dispose(); return clipping; }
From source file:by.bsuir.group172301.matskevich.tour.util.PDFCreator.java
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName, int col1, int col2, double perc) { PdfWriter writer = null;/* w w w . ja v a 2s .co m*/ Document document = new Document(); try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); Paragraph paragraph = new Paragraph(); // We add one empty line paragraph = new Paragraph("Results:"); paragraph.setAlignment(Element.ALIGN_CENTER); document.add(paragraph); paragraph = new Paragraph("Test before: " + col1); document.add(paragraph); paragraph = new Paragraph("Test after: " + col2); document.add(paragraph); paragraph = new Paragraph("Percentage: " + perc); document.add(paragraph); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 150, 250); } catch (Exception e) { e.printStackTrace(); } document.close(); }
From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java
public static BufferedImage removeAlphaChannel(BufferedImage src) { //if (src.getColorModel().hasAlpha()) { BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(src, 0, 0, null);// w w w . j a v a2s.c om g.dispose(); return image; //} //return src; /* * BufferedImage rgbImage = new BufferedImage(src.getWidth(), * src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x < * src.getWidth(); ++x) { for (int y = 0; y < src.getHeight(); ++y) { * rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); } } return * rgbImage; */ }
From source file:it.smartcommunitylab.parking.management.web.manager.MarkerIconStorage.java
private static BufferedImage changeColor(BufferedImage image, Color mask, Color replacement) { BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = destImage.createGraphics(); g.drawImage(image, null, 0, 0);/* ww w.j a v a 2 s .c o m*/ g.dispose(); for (int i = 0; i < destImage.getWidth(); i++) { for (int j = 0; j < destImage.getHeight(); j++) { int destRGB = destImage.getRGB(i, j); if (matches(mask.getRGB(), destRGB)) { int rgbnew = getNewPixelRGB(replacement.getRGB(), destRGB); destImage.setRGB(i, j, rgbnew); } } } return destImage; }
From source file:com.pureinfo.srm.common.ImageHelper.java
public static void drawImage(String _sString, OutputStream _os) throws PureException { int nWidth = 200; int nHeight = 50; String sText = _sString;/*from ww w .j av a 2 s.com*/ BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); for (int i = 0; i < sText.length(); i++) { draw(String.valueOf(sText.charAt(i)), g2, i * nWidth / sText.length(), 0, (i + 1) * nWidth / sText.length(), nHeight); } g2.dispose(); try { EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os); } catch (Exception ex) { throw new PureException(PureException.UNKNOWN, "", ex); } }