List of usage examples for java.awt Graphics2D setRenderingHint
public abstract void setRenderingHint(Key hintKey, Object hintValue);
From source file:ec.util.chart.swing.Charts.java
private static String generateSVG(JFreeChart chart, int width, int height) throws IOException { try {//from w w w .j a va 2 s . c om Class<?> svgGraphics2d = Class.forName("org.jfree.graphics2d.svg.SVGGraphics2D"); Graphics2D g2 = (Graphics2D) svgGraphics2d.getConstructor(int.class, int.class).newInstance(width, height); // we suppress shadow generation, because SVG is a vector format and // the shadow effect is applied via bitmap effects... g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true); chart.draw(g2, new Rectangle2D.Double(0, 0, width, height)); return (String) g2.getClass().getMethod("getSVGElement").invoke(g2); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new IOException("Cannot generate SVG", ex); } }
From source file:org.jimcat.services.imagemanager.ImageUtil.java
/** * this methode will load given image using tiles (saving memory) * /*from ww w.ja va2s. c o m*/ * this strategie is spliting the original image up into smaller parts * called tiles. Those tiles are downscaled one by one using given quality. * This results int probably best possible quality but may cost a lot of * time. * * @param reader - * the reader to load image from * @param size - * the resulting image size * @param quality - * the quality used for necessary rendering * @return the image as buffered image * @throws IOException */ @SuppressWarnings("unused") private static BufferedImage loadImageWithTiles(ImageReader reader, Dimension size, ImageQuality quality) throws IOException { // the image buffer used to load tiles ImageTypeSpecifier imageSpec = reader.getImageTypes(0).next(); BufferedImage tile = imageSpec.createBufferedImage(IMAGE_TILE_SIZE.width, IMAGE_TILE_SIZE.height); // the image the result is rendered into BufferedImage result = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = result.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, quality.getHint()); // prepaire image reader parameter ImageReadParam param = reader.getDefaultReadParam(); param.setDestination(tile); // count tiles int width = reader.getWidth(0); int height = reader.getHeight(0); int numX = (int) Math.ceil(width / (float) IMAGE_TILE_SIZE.width); int numY = (int) Math.ceil(height / (float) IMAGE_TILE_SIZE.height); float aspectX = (float) IMAGE_TILE_SIZE.width / width; float aspectY = (float) IMAGE_TILE_SIZE.height / height; // target tile dimension int targetTileWidth = (int) (size.width * aspectX); int targetTileHeight = (int) (size.height * aspectY); // load tiles Rectangle sourceRegion = new Rectangle(); Rectangle targetRegion = new Rectangle(); for (int i = 0; i < numX; i++) { // line increment sourceRegion.x = i * IMAGE_TILE_SIZE.width; sourceRegion.width = Math.min(IMAGE_TILE_SIZE.width, width - sourceRegion.x); targetRegion.x = i * targetTileWidth; targetRegion.width = Math.min(targetTileWidth, size.width - targetRegion.x); for (int j = 0; j < numY; j++) { // row increment sourceRegion.y = j * IMAGE_TILE_SIZE.height; sourceRegion.height = Math.min(IMAGE_TILE_SIZE.height, height - sourceRegion.y); targetRegion.y = j * targetTileHeight; targetRegion.height = Math.min(targetTileHeight, size.height - targetRegion.y); // performe read param.setSourceRegion(sourceRegion); reader.read(0, param); // insert into resulting image int dx1 = targetRegion.x; int dx2 = targetRegion.x + targetRegion.width; int dy1 = targetRegion.y; int dy2 = targetRegion.y + targetRegion.height; g.drawImage(tile, dx1, dy1, dx2, dy2, 0, 0, sourceRegion.width, sourceRegion.height, null); } } // finish drawing g.dispose(); // return result return result; }
From source file:org.jimcat.services.imagemanager.ImageUtil.java
/** * This methode will scale the given Buffered image to the given dimension * using the given ImageQuality//w ww . ja v a 2s. co m * * If image dimension already maches, it will return the images itself. * * based on * * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * * * @param img - * the image to scale * @param dimension - * the destination dimension * @param quality - * the quality * @return a scaled instance of the buffered image */ public static BufferedImage getScaledInstance(BufferedImage img, Dimension dimension, ImageQuality quality) { // can't work with null values if (img == null) { return null; } // the resulting image BufferedImage ret = img; // extract target sizes int targetWidth = dimension.width; int targetHeight = dimension.height; // if size already fit => shortcut if (img.getWidth() == targetWidth && img.getHeight() == targetHeight) { return img; } // extract quality infos boolean higherQuality = quality.requiresIntermediateSteps(); // resulting image type int type = getImageType(img); // scale down int w, h; if (higherQuality) { // 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(); } else { // Use one-step technique: scale directly from original // size to target size with a single drawImage() call w = targetWidth; h = targetHeight; } do { // calculate intermediate steps if (higherQuality && w > targetWidth) { w /= 2; } if (higherQuality && h > targetHeight) { h /= 2; } // for upscaling, no intermedait steps are supported if (w < targetWidth) { w = targetWidth; } if (h < targetHeight) { h = targetHeight; } // calculate next scaling step BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, quality.getHint()); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w != targetWidth || h != targetHeight); // result finished return ret; }
From source file:ala.soils2sat.DrawingUtils.java
public static void drawRoundedRect(Graphics g, int left, int top, int right, int bottom) { Graphics2D g2d = (Graphics2D) g; g.drawLine(left + cornerSize, top, right - cornerSize, top); g.drawLine(left + cornerSize, bottom, right - cornerSize, bottom); g.drawLine(left, top + cornerSize, left, bottom - cornerSize); g.drawLine(right, top + cornerSize, right, bottom - cornerSize); final Object previousAntiAliasingHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); final Stroke previousStroke = g2d.getStroke(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); try {/*from w w w .jav a 2 s .c o m*/ g.drawLine(left, top + cornerSize, left + cornerSize, top); g.drawLine(left, bottom - cornerSize, left + cornerSize, bottom); g.drawLine(right, top + cornerSize, right - cornerSize, top); g.drawLine(right, bottom - cornerSize, right - cornerSize, bottom); } finally { g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, previousAntiAliasingHint); g2d.setStroke(previousStroke); } }
From source file:com.sketchy.image.ImageProcessingThread.java
public static BufferedImage resizeImage(BufferedImage image, int drawingWidth, int drawingHeight, CenterOption centerOption, ScaleOption scaleOption) { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double widthScale = drawingWidth / (double) imageWidth; double heightScale = drawingHeight / (double) imageHeight; double scale = Math.min(widthScale, heightScale); int scaleWidth = (int) (imageWidth * scale); int scaleHeight = (int) (imageHeight * scale); BufferedImage resizedImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); if (scaleOption == ScaleOption.SCALE_AREA_AVERAGING) { ImageProducer prod = new FilteredImageSource(image.getSource(), new AreaAveragingScaleFilter(scaleWidth, scaleHeight)); Image img = Toolkit.getDefaultToolkit().createImage(prod); g.drawImage(img, 0, 0, scaleWidth, scaleHeight, null); // it's already scaled } else {/* w ww . j a v a2 s.co m*/ if (scaleOption == ScaleOption.SCALE_NEAREST_NEIGHBOR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); } else if (scaleOption == ScaleOption.SCALE_BICUBIC) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } else if (scaleOption == ScaleOption.SCALE_BILINEAR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g.drawImage(image, 0, 0, scaleWidth, scaleHeight, 0, 0, imageWidth, imageHeight, null); } g.dispose(); int cropWidth = (int) Math.min(scaleWidth, drawingWidth); int cropHeight = (int) Math.min(scaleHeight, drawingHeight); int drawingLeft = 0; int drawingTop = 0; if ((centerOption == CenterOption.CENTER_HORIZONTAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingLeft = (int) ((drawingWidth - cropWidth) / 2.0); } if ((centerOption == CenterOption.CENTER_VERTICAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingTop = (int) ((drawingHeight - cropHeight) / 2.0); } BufferedImage croppedImage = resizedImage.getSubimage(0, 0, cropWidth, cropHeight); resizedImage = null; BufferedImage drawingImage = new BufferedImage(drawingWidth, drawingHeight, BufferedImage.TYPE_INT_ARGB); g = drawingImage.createGraphics(); g.drawImage(croppedImage, drawingLeft, drawingTop, drawingLeft + cropWidth, drawingTop + cropHeight, 0, 0, cropWidth, cropHeight, null); g.dispose(); croppedImage = null; return drawingImage; }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Creates the thumbnail.// w ww. j a va 2 s . c o m * * @param imageFile the image file * @param thumbFile the empty thumbnail file * @param thumbX the width of the thumbnail * @param thumbY the height of the thumbnail * @param imageName the image file name with extension * @param tolerance the percentage of tolerance before creating a thumbnail * @return true is the thumbnail has been created, false otherwise * @throws IOException if an error occurs */ private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName, double tolerance) throws IOException { FileInputStream fileis = null; ImageInputStream imageis = null; Iterator readers = null; try { readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1)); if (readers == null || (!readers.hasNext())) { throw new IOException("File not supported"); } ImageReader reader = (ImageReader) readers.next(); fileis = new FileInputStream(imageFile); imageis = ImageIO.createImageInputStream(fileis); reader.setInput(imageis, true); // Determines thumbnail height, width and quality int thumbWidth = thumbX; int thumbHeight = thumbY; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = reader.getWidth(0); int imageHeight = reader.getHeight(0); // // Don't create the thumbnail if the original file is smaller // than required size increased by % tolerance // if (imageWidth <= (thumbWidth * (1 + tolerance / 100)) && imageHeight <= (thumbHeight * (1 + tolerance / 100))) { return false; } double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 0, 0); BufferedImage bi = reader.read(0, param); Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null); FileOutputStream fileOutputStream = new FileOutputStream(thumbFile); ImageIO.write(thumbImage, "jpg", fileOutputStream); thumb.flush(); thumbImage.flush(); fileOutputStream.flush(); fileOutputStream.close(); graphics2D.dispose(); } finally { if (fileis != null) { fileis.close(); } if (imageis != null) { imageis.close(); } } return true; }
From source file:Main.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); }
From source file:util.ui.UiUtilities.java
/** * Convenience method that returns a scaled instance of the * provided {@code BufferedImage}./*from ww w .ja v a 2 s. c o m*/ * * @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 * @return a scaled version of the original {@code BufferedImage} */ public static BufferedImage scaleDown(final BufferedImage img, final int targetWidth, final int targetHeight) { if (targetWidth > img.getWidth() || targetHeight > img.getHeight()) { return scaleIconToBufferedImage(img, targetWidth, targetHeight); } int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage result = img; int w = img.getWidth(); int h = img.getHeight(); do { w /= 2; if (w < targetWidth) { w = targetWidth; } h /= 2; if (h < targetHeight) { h = targetHeight; } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(result, 0, 0, w, h, null); g2.dispose(); result = tmp; } while (w != targetWidth || h != targetHeight); return result; }
From source file:util.ui.UiUtilities.java
/** * Scales Icons to a specific size// w w w . j a v a 2 s. c om * * @param icon * Icon that should be scaled * @param width * scaled width * @param height * scaled height * @return Scaled Icon */ public static Icon scaleIcon(Icon icon, int width, int height) { if (icon == null) { return null; } int currentWidth = icon.getIconWidth(); int currentHeight = icon.getIconHeight(); if ((currentWidth == width) && (currentHeight == height)) { return icon; } try { // Create Image with Icon BufferedImage iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = iconImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); AffineTransform z = g2.getTransform(); g2.setTransform(z); icon.paintIcon(null, g2, 0, 0); g2.dispose(); BufferedImage scaled = scaleDown(iconImage, width, height); // Return new Icon return new ImageIcon(scaled); } catch (Exception ex) { ex.printStackTrace(); } return icon; }
From source file:BasicDraw.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); }