List of usage examples for java.awt Graphics2D setRenderingHint
public abstract void setRenderingHint(Key hintKey, Object hintValue);
From source file:weka.core.ChartUtils.java
/** * Render a combined histogram and box plot chart from summary data * /*ww w . j av a 2s. co m*/ * @param width the width of the resulting image * @param height the height of the resulting image * @param bins the values for the chart * @param freqs the corresponding frequencies * @param summary the summary stats for the box plot * @param outliers an optional list of outliers for the box plot * @param additionalArgs optional arguments to the renderer (may be null) * @return a buffered image * @throws Exception if a problem occurs */ public static BufferedImage renderCombinedBoxPlotAndHistogramFromSummaryData(int width, int height, List<String> bins, List<Double> freqs, List<Double> summary, List<Double> outliers, List<String> additionalArgs) throws Exception { String plotTitle = "Combined Chart"; String userTitle = getOption(additionalArgs, "-title"); plotTitle = (userTitle != null) ? userTitle : plotTitle; List<String> opts = new ArrayList<String>(); opts.add("-title=histogram"); BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, bins, freqs, opts); opts.clear(); opts.add("-title=box plot"); BufferedImage box = null; try { box = renderBoxPlotFromSummaryData(width / 2, height, summary, outliers, opts); } catch (Exception ex) { // if we can't generate the box plot (i.e. probably because // data is 100% missing) then just return the histogram } if (box == null) { width /= 2; } BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g2d.setFont(new Font("SansSerif", Font.BOLD, 12)); g2d.setColor(Color.lightGray); g2d.fillRect(0, 0, width, height + 20); g2d.setColor(Color.black); FontMetrics fm = g2d.getFontMetrics(); int fh = fm.getHeight(); int sw = fm.stringWidth(plotTitle); if (box != null) { g2d.drawImage(box, 0, 20, null); g2d.drawImage(hist, width / 2 + 1, 20, null); } else { g2d.drawImage(hist, 0, 20, null); } g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2); g2d.dispose(); return img; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Convenience method that returns a scaled instance of the * provided {@code BufferedImage}.//ww w . ja va2 s .c om * * Code stolen from http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * * @param img the original image to be scaled * @param targetWidth the desired width of the scaled instance, * in pixels * @param targetHeight the desired height of the scaled instance, * in pixels * @param hint one of the rendering hints that corresponds to * {@code RenderingHints.KEY_INTERPOLATION} (e.g. * {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR}, * {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR}, * {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC}) * @param higherQuality if true, this method will use a multi-step * scaling technique that provides higher quality than the usual * one-step technique (only useful in down-scaling cases, where * {@code targetWidth} or {@code targetHeight} is * smaller than the original dimensions, and generally only when * the {@code BILINEAR} hint is specified) * @return a scaled version of the original {@code BufferedImage} */ public static BufferedImage getScaledInstance(final BufferedImage img, final int targetWidth, final int targetHeight, final boolean higherQuality) { int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage temp = img; BufferedImage result = new BufferedImage(targetWidth, targetHeight, type); Graphics2D g2 = result.createGraphics(); if (higherQuality) { g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g2.drawImage(temp, 0, 0, targetWidth, targetHeight, null); g2.dispose(); return result; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Gets a scaled icon and if it doesn't exist it creates one and scales it * @param icon image to be scaled/*w w w . j av a 2s . co m*/ * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio) { if (icon != null) { int dstWidth = newMaxWidth; int dstHeight = newMaxHeight; int srcWidth = icon.getIconWidth(); int srcHeight = icon.getIconHeight(); if ((dstWidth < 0) || (dstHeight < 0)) { //image is nonstd, revert to original size dstWidth = icon.getIconWidth(); dstHeight = icon.getIconHeight(); } if (maintainRatio) { double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = Math.max(dstWidth, dstHeight); double multiplier = longSideForDest / longSideForSource; dstWidth = (int) (srcWidth * multiplier); dstHeight = (int) (srcHeight * multiplier); } Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); BufferedImage thumbImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null); graphics2D.dispose(); return thumbImage; } return null; }
From source file:Hexagon.java
public static BufferedImage drawCodePoint(char codePoint, int width, int height, Font font, Color color) { BufferedImage img = createImage(width, height); Graphics2D g2 = img.createGraphics(); String text = "" + codePoint; g2.setColor(color);//from www. j ava 2 s. co m g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GlyphVector gv = font.createGlyphVector(g2.getFontRenderContext(), text); g2.drawGlyphVector(gv, 0f, (float) gv.getGlyphMetrics(0).getBounds2D().getHeight()); return img; }
From source file:org.vulpe.view.tags.Functions.java
/** * This method takes in an image as a byte array (currently supports GIF, * JPG, PNG and possibly other formats) and resizes it to have a width no * greater than the pMaxWidth parameter in pixels. It converts the image to * a standard quality JPG and returns the byte array of that JPG image. * * @param imageData//from w w w . j a v a 2 s .co m * the image data. * @param maxWidth * the max width in pixels, 0 means do not scale. * @return the resized JPG image. * @throws IOException * if the image could not be manipulated correctly. */ public static byte[] resizeImageAsJPG(final byte[] imageData, final int maxWidth) throws IOException { // Create an ImageIcon from the image data final ImageIcon imageIcon = new ImageIcon(imageData); int width = imageIcon.getIconWidth(); if (width == maxWidth) { return imageData; } int height = imageIcon.getIconHeight(); LOG.debug("imageIcon width: " + width + " height: " + height); // If the image is larger than the max width, we need to resize it if (maxWidth > 0 && width > maxWidth) { // Determine the shrink ratio final double ratio = (double) maxWidth / imageIcon.getIconWidth(); LOG.debug("resize ratio: " + ratio); height = (int) (imageIcon.getIconHeight() * ratio); width = maxWidth; LOG.debug("imageIcon post scale width: " + width + " height: " + height); } // Create a new empty image buffer to "draw" the resized image into final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Create a Graphics object to do the "drawing" final Graphics2D g2d = bufferedImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Draw the resized image g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null); g2d.dispose(); // Now our buffered image is ready // Encode it as a JPEG final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos); encoder.encode(bufferedImage); return baos.toByteArray(); }
From source file:processing.app.Theme.java
static public Graphics2D setupGraphics2D(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; if (PreferencesData.getBoolean("editor.antialias")) { g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); }/* w w w . jav a2s .c om*/ return g; }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
/** * This code is very inspired on Chris Campbells article "The Perils of Image.getScaledInstance()" * * The article can be found here:/* ww w . j a v a2 s .c o m*/ * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * * Note that the filter method is threadsafe */ private static BufferedImage scaleFastTo(BufferedImage img, Size scaledSize) { if (!scaledSize.isChanged()) return img; BufferedImage dest; if (img.getType() == BufferedImage.TYPE_CUSTOM) { dest = new BufferedImage(scaledSize.getWidth(), scaledSize.getHeight(), BufferedImage.TYPE_INT_ARGB); } else { dest = new BufferedImage(scaledSize.getWidth(), scaledSize.getHeight(), img.getType()); } int dstWidth = scaledSize.getWidth(); int dstHeight = scaledSize.getHeight(); BufferedImage ret = img; int w, h; // Use multi-step technique: start with original size, then // scale down in multiple passes with drawImage() // until the target size is reached w = img.getWidth(); h = img.getHeight(); int x = scaledSize.getXOffset(); int y = scaledSize.getYOffset(); //crop the image to see only the center of the image if (x > 0 || y > 0) { ret = img.getSubimage(x, y, w - (2 * x), h - (2 * y)); } do { if (w > dstWidth) { if (x > 0) { x /= 2; } w /= 2; if (w < dstWidth) { w = dstWidth; } } else { w = dstWidth; } if (h > dstHeight) { h /= 2; if (h < dstHeight) { h = dstHeight; } } else { h = dstHeight; } BufferedImage tmp; if (dest.getWidth() == w && dest.getHeight() == h && w == dstWidth && h == dstHeight) { tmp = dest; } else { tmp = new BufferedImage(w, h, dest.getType()); } Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w != dstWidth || h != dstHeight); return ret; }
From source file:com.t3.persistence.PersistenceUtil.java
public static void saveToken(Token token, File file, boolean doWait) throws IOException { // Thumbnail// ww w. ja v a 2 s . c om BufferedImage image = null; if (doWait) image = ImageManager.getImageAndWait(token.getImageAssetId()); else image = ImageManager.getImage(token.getImageAssetId()); Dimension sz = new Dimension(image.getWidth(), image.getHeight()); SwingUtil.constrainTo(sz, 50); BufferedImage thumb = new BufferedImage(sz.width, sz.height, BufferedImage.TRANSLUCENT); Graphics2D g = thumb.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(image, 0, 0, sz.width, sz.height, null); g.dispose(); PackedFile pakFile = null; try { pakFile = new PackedFile(file); saveAssets(token.getAllImageAssets(), pakFile); pakFile.putFile(Token.FILE_THUMBNAIL, ImageUtil.imageToBytes(thumb, "png")); pakFile.setContent(token); pakFile.save(); } finally { if (pakFile != null) pakFile.close(); } }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
public static BufferedImage scale(BufferedImage bi, int width, int height) { BufferedImage bi2;//from ww w . j a v a 2 s . c o m int scaleWidth = bi.getWidth(null); int scaleHeight = bi.getHeight(null); double scaleX = (double) width / scaleWidth; double scaleY = (double) height / scaleHeight; double scale = Math.min(scaleX, scaleY); scaleWidth = (int) ((double) scaleWidth * scale); scaleHeight = (int) ((double) scaleHeight * scale); Image scaledImage; if (HEADLESS) { // create a new buffered image, don't rely on a local graphics system (headless mode) final int type; if (bi.getType() != BufferedImage.TYPE_CUSTOM) { type = bi.getType(); } else if (bi.getAlphaRaster() != null) { // alpha channel available type = BufferedImage.TYPE_INT_ARGB; } else { type = BufferedImage.TYPE_INT_RGB; } bi2 = new BufferedImage(scaleWidth, scaleHeight, type); } else { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration(); bi2 = gc.createCompatibleImage(scaleWidth, scaleHeight, bi.getTransparency()); } Graphics2D g = bi2.createGraphics(); if (scale < 0.3 && Math.max(scaleWidth, scaleHeight) < 500) { scaledImage = bi.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH); new ImageIcon(scaledImage).getImage(); g.drawImage(scaledImage, 0, 0, scaleWidth, scaleHeight, null); } else { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.drawImage(bi, 0, 0, scaleWidth, scaleHeight, null); } g.dispose(); return bi2; }
From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.PlotInstanceLegendCreator.java
private static Paint createTransparentCheckeredPaint(Color color, int checkerSize) { int s = checkerSize; BufferedImage bufferedImage = new BufferedImage(2 * s, 2 * s, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias! RenderingHints.VALUE_ANTIALIAS_ON); Color c1 = DataStructureUtils.setColorAlpha(color, (int) (color.getAlpha() * .8)); Color c2 = DataStructureUtils.setColorAlpha(color, (int) (color.getAlpha() * .2)); g2.setStroke(new BasicStroke(0)); g2.setPaint(c2);/*from w w w . ja va 2 s . c o m*/ g2.setColor(c2); g2.fillRect(0, 0, s, s); g2.fillRect(s, s, s, s); g2.setPaint(c1); g2.setColor(c1); g2.fillRect(0, s, s, s); g2.fillRect(s, 0, s, s); // paint with the texturing brush Rectangle2D rect = new Rectangle2D.Double(0, 0, 2 * s, 2 * s); return new TexturePaint(bufferedImage, rect); }