List of usage examples for java.awt Graphics2D setRenderingHint
public abstract void setRenderingHint(Key hintKey, Object hintValue);
From source file:com.o2d.pkayjava.editor.proxy.ResolutionManager.java
public static BufferedImage imageResize(File file, float ratio) { BufferedImage destinationBufferedImage = null; try {/*from w w w . j a va 2 s. c o m*/ BufferedImage sourceBufferedImage = ImageIO.read(file); if (ratio == 1.0) { return null; } // 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:org.pentaho.reporting.engine.classic.core.layout.output.RenderUtility.java
public static DefaultImageReference createImageFromDrawable(final DrawableWrapper drawable, final StrictBounds rect, final StyleSheet box, final OutputProcessorMetaData metaData) { final int imageWidth = (int) StrictGeomUtility.toExternalValue(rect.getWidth()); final int imageHeight = (int) StrictGeomUtility.toExternalValue(rect.getHeight()); if (imageWidth == 0 || imageHeight == 0) { return null; }/*from w ww .ja v a 2 s.com*/ final double scale = RenderUtility.getNormalizationScale(metaData); final Image image = ImageUtils.createTransparentImage((int) (imageWidth * scale), (int) (imageHeight * scale)); final Graphics2D g2 = (Graphics2D) image.getGraphics(); final Object attribute = box.getStyleProperty(ElementStyleKeys.ANTI_ALIASING); if (attribute != null) { if (Boolean.TRUE.equals(attribute)) { g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } else if (Boolean.FALSE.equals(attribute)) { g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); } } if (RenderUtility.isFontSmooth(box, metaData)) { g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } else { g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } g2.scale(scale, scale); // the clipping bounds are a sub-area of the whole drawable // we only want to print a certain area ... final String fontName = (String) box.getStyleProperty(TextStyleKeys.FONT); final int fontSize = box.getIntStyleProperty(TextStyleKeys.FONTSIZE, 8); final boolean bold = box.getBooleanStyleProperty(TextStyleKeys.BOLD); final boolean italics = box.getBooleanStyleProperty(TextStyleKeys.ITALIC); if (bold && italics) { g2.setFont(new Font(fontName, Font.BOLD | Font.ITALIC, fontSize)); } else if (bold) { g2.setFont(new Font(fontName, Font.BOLD, fontSize)); } else if (italics) { g2.setFont(new Font(fontName, Font.ITALIC, fontSize)); } else { g2.setFont(new Font(fontName, Font.PLAIN, fontSize)); } g2.setStroke((Stroke) box.getStyleProperty(ElementStyleKeys.STROKE)); g2.setPaint((Paint) box.getStyleProperty(ElementStyleKeys.PAINT)); drawable.draw(g2, new Rectangle2D.Double(0, 0, imageWidth, imageHeight)); g2.dispose(); try { return new DefaultImageReference(image); } catch (final IOException e1) { logger.warn("Unable to fully load a given image. (It should not happen here.)", e1); return null; } }
From source file:nl.b3p.viewer.image.ImageTool.java
/** Combines GIF, TIFF or PNG images. Combining these images is different from the JPG image types since these * has to use an other imageType: BufferedImage.TYPE_INT_ARGB_PRE. * * @param images the referenced Images//ww w . j av a 2 s . c om * @param width the width of the result image (or null if the width of the first image must be used) * @param height the height of the result image (or null if the width of the first image must be used) * * @return BufferedImage */ private static BufferedImage combineOtherImages(List<ReferencedImage> images, Integer width, Integer height) { if (images.get(0) != null) { BufferedImage bi = images.get(0).getImage(); //if no height / width use the height/widht of the first image. if (height == null) { height = bi.getHeight(); } if (width == null) { width = bi.getWidth(); } } BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D gbi = newBufIm.createGraphics(); gbi.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); for (int i = 0; i < images.size(); i++) { ReferencedImage image = images.get(i); drawImage(gbi, image); } return newBufIm; }
From source file:org.madsonic.controller.CoverArtController.java
public static BufferedImage scale(BufferedImage image, int width, int height) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage thumb = image;// www. java 2 s . c o m // For optimal results, use step by step bilinear resampling - halfing the size at each step. do { w /= 2; h /= 2; if (w < width) { w = width; } if (h < height) { h = height; } double thumbRatio = (double) width / (double) height; double aspectRatio = (double) w / (double) h; // LOG.debug("## thumbsRatio: " + thumbRatio); // LOG.debug("## aspectRatio: " + aspectRatio); if (thumbRatio < aspectRatio) { h = (int) (w / aspectRatio); } else { w = (int) (h * aspectRatio); } BufferedImage temp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = temp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(thumb, 0, 0, w, h, null); g2.dispose(); thumb = temp; } while (w != width); //FIXME: check if (thumb.getHeight() > thumb.getWidth()) { thumb = thumb.getSubimage(0, 0, thumb.getWidth(), thumb.getWidth()); } return thumb; }
From source file:org.lmn.fc.frameworks.starbase.plugins.observatory.ui.tabs.charts.ChartUIHelper.java
/*********************************************************************************************** * Create the default Toolbar for a Chart. * * @param obsinstrument/*w ww.ja v a 2s. co m*/ * @param chart * @param title * @param fontdata * @param colourforeground * @param colourbackground * @param debug * * @return JToolBar */ public static JToolBar createDefaultToolbar(final ObservatoryInstrumentInterface obsinstrument, final ChartUIComponentPlugin chart, final String title, final FontInterface fontdata, final ColourInterface colourforeground, final ColourInterface colourbackground, final boolean debug) { final JToolBar toolbar; if ((obsinstrument != null) && (chart != null)) { final List<Component> listComponentsToAdd; final JLabel labelName; listComponentsToAdd = new ArrayList<Component>(10); //------------------------------------------------------------------------------------- // Initialise the Label labelName = new JLabel(title, RegistryModelUtilities.getAtomIcon(obsinstrument.getHostAtom(), ObservatoryInterface.FILENAME_ICON_CHART_VIEWER), SwingConstants.LEFT) { static final long serialVersionUID = 7580736117336162922L; // Enable Antialiasing in Java 1.5 protected void paintComponent(final Graphics graphics) { final Graphics2D graphics2D = (Graphics2D) graphics; // For antialiasing text graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); super.paintComponent(graphics2D); } }; labelName.setFont( fontdata.getFont().deriveFont(ReportTableHelper.SIZE_HEADER_FONT).deriveFont(Font.BOLD)); labelName.setForeground(colourforeground.getColor()); labelName.setIconTextGap(UIComponentPlugin.TOOLBAR_ICON_TEXT_GAP); listComponentsToAdd.add(new JToolBar.Separator(UIComponentPlugin.DIM_TOOLBAR_SEPARATOR_BUTTON)); listComponentsToAdd.add(labelName); listComponentsToAdd.add(new JToolBar.Separator(UIComponentPlugin.DIM_TOOLBAR_SEPARATOR)); listComponentsToAdd.add(Box.createHorizontalGlue()); UIComponentHelper.addToolbarPrintButtons(listComponentsToAdd, chart, chart.getChartPanel(), title, fontdata, colourforeground, colourbackground, debug); // Build the Toolbar using the Components, if any toolbar = UIComponentHelper.buildToolbar(listComponentsToAdd); } else { toolbar = new JToolBar(); } toolbar.setFloatable(false); toolbar.setMinimumSize(UIComponentPlugin.DIM_TOOLBAR_SIZE); toolbar.setPreferredSize(UIComponentPlugin.DIM_TOOLBAR_SIZE); toolbar.setMaximumSize(UIComponentPlugin.DIM_TOOLBAR_SIZE); toolbar.setBackground(colourbackground.getColor()); NavigationUtilities.updateComponentTreeUI(toolbar); return (toolbar); }
From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java
/** * Loads the given CLP or SVG file and creates a BufferedImage with the given dimensions. As CLP files contain Vector images, * they can be scaled to every size needed. The contents are scaled to the given width and height, <b>not</b> preserving any * ratio of the image.//ww w .ja v a 2s .c o m * * @param clpFile CLP or SVG file. * @param widthPixel The width, in pixels, the resulting image shall have. * @param heightPixel The height, in pixels, the resulting image shall have. * * @return An image displaying the contents of the loaded CLP file. * * @throws IOException If any I/O related problem occurs reading the file. */ public static BufferedImage loadClpFile(File clpFile, int widthPixel, int heightPixel) throws IOException { FileInputStream fis = new FileInputStream(clpFile); ClpInputStream cis = null; InputStream in = clpFile.getName().toLowerCase().endsWith(".clp") ? (cis = new ClpInputStream(fis)) : fis; UserAgentAdapter userAgentAdapter = new UserAgentAdapter(); BridgeContext bridgeContext = new BridgeContext(userAgentAdapter); SVGDocument svgDocument; GraphicsNode rootSvgNode; try { String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser); svgDocument = (SVGDocument) factory.createDocument(clpFile.toURI().toString(), new InputStreamReader(in, "ISO-8859-1")); rootSvgNode = getRootNode(svgDocument, bridgeContext); } finally { IOUtils.closeQuietly(cis); IOUtils.closeQuietly(fis); } float[] vb = ViewBox.parseViewBoxAttribute(svgDocument.getRootElement(), svgDocument.getRootElement().getAttribute("viewBox"), bridgeContext); AffineTransform usr2dev = ViewBox.getPreserveAspectRatioTransform(vb, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true, widthPixel, heightPixel); BufferedImage img = new BufferedImage(widthPixel, heightPixel, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setColor(new Color(0.0f, 0.0f, 0.0f, 0.0f)); g2d.fillRect(0, 0, widthPixel, heightPixel); g2d.transform(usr2dev); // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 1 final Object oldBufferedImage = g2d.getRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE); g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference<BufferedImage>(img)); rootSvgNode.paint(g2d); // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 2 if (oldBufferedImage != null) g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, oldBufferedImage); else g2d.getRenderingHints().remove(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE); g2d.dispose(); return img; }
From source file:weka.core.ChartUtils.java
/** * Render a combined histogram and pie chart from summary data * //from w w w. j a v a 2 s . c o m * @param width the width of the resulting image * @param height the height of the resulting image * @param values the values for the chart * @param freqs the corresponding frequencies * @param additionalArgs optional arguments to the renderer (may be null) * @return a buffered image * @throws Exception if a problem occurs */ public static BufferedImage renderCombinedPieAndHistogramFromSummaryData(int width, int height, List<String> values, List<Double> freqs, 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=distribution"); BufferedImage pie = renderPieChartFromSummaryData(width / 2, height, values, freqs, false, true, opts); opts.clear(); opts.add("-title=histogram"); BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, values, freqs, opts); 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); g2d.drawImage(pie, 0, 20, null); g2d.drawImage(hist, width / 2 + 1, 20, null); g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2); g2d.dispose(); return img; }
From source file:com.cmart.PageControllers.SellItemImagesController.java
private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int width, int height) { BufferedImage resizedImage = new BufferedImage(width, width, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, width, null); g.dispose();/* w w w .ja v a 2 s. c om*/ 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); return resizedImage; }
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//from w w w. j a v a 2 s. c o m * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static BufferedImage getBufferedImage(final ImageIcon icon) { Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); int w = icon.getIconWidth(); int h = icon.getIconHeight(); BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, w, h, 0, 0, w, h, null); graphics2D.dispose(); return bufferedImage; }
From source file:nl.b3p.imagetool.ImageTool.java
/** * Combines GIF, TIFF or PNG images. Combining these images is different * from the JPG image types since these has to use an other imageType: * BufferedImage.TYPE_INT_ARGB_PRE.// w w w .j a v a 2s.c om * * @param images the referenced Images * @param width the width of the result image (or null if the width of the * first image must be used) * @param height the height of the result image (or null if the width of the * first image must be used) * * @return BufferedImage */ private static BufferedImage combineOtherImages(List<ReferencedImage> images, Integer width, Integer height) throws Exception { if (images != null && images.get(0) != null) { BufferedImage bi = images.get(0).getImage(); //if no height / width use the height/widht of the first image. if (height == null) { height = bi.getHeight(); } if (width == null) { width = bi.getWidth(); } } BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D gbi = newBufIm.createGraphics(); gbi.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); if (images != null) { for (ReferencedImage image : images) { drawImage(gbi, image); image.dispose(); } } return newBufIm; }